Desarrollar un programa que muestre el tablero de un ascensor:
El funcionamiento es el siguiente:
Descargar el ejecutable
Descargar código fuente
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class Ascensor extends JFrame implements ActionListener{
private static final long serialVersionUID = -6298610548027787167L;
private JPanel contentPane;
private JButton btn4;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JLabel direccion;
private JLabel piso;
private int pisoActual;
public static void main(String[] args) {
Ascensor frame = new Ascensor();
frame.setVisible(true);
}
public Ascensor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 444, 387);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
pisoActual = 1;
JLabel lblPiso = new JLabel("PISO:");
lblPiso.setHorizontalAlignment(SwingConstants.RIGHT);
lblPiso.setName("lblPiso");
lblPiso.setFont(new Font("Arial", Font.BOLD, 18));
lblPiso.setBounds(136, 32, 127, 41);
contentPane.add(lblPiso);
JLabel lblDireccin = new JLabel("DIRECCI\u00D3N:");
lblDireccin.setName("lblPiso");
lblDireccin.setHorizontalAlignment(SwingConstants.RIGHT);
lblDireccin.setFont(new Font("Arial", Font.BOLD, 18));
lblDireccin.setBounds(136, 84, 127, 41);
contentPane.add(lblDireccin);
piso = new JLabel("1");
piso.setName("piso");
piso.setHorizontalAlignment(SwingConstants.LEFT);
piso.setFont(new Font("Arial", Font.BOLD, 18));
piso.setBounds(276, 32, 127, 41);
contentPane.add(piso);
direccion = new JLabel("PISO ACTUAL");
direccion.setName("direccion");
direccion.setHorizontalAlignment(SwingConstants.LEFT);
direccion.setFont(new Font("Arial", Font.BOLD, 18));
direccion.setBounds(276, 84, 127, 41);
contentPane.add(direccion);
JPanel panel = new JPanel();
panel.setBackground(Color.GREEN);
panel.setBounds(32, 263, 66, 66);
contentPane.add(panel);
panel.setLayout(null);
btn1 = new JButton("1");
btn1.setContentAreaFilled(false);
btn1.setBounds(0, 0, 66, 66);
panel.add(btn1);
btn1.setSelected(true);
btn1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn1.setForeground(Color.RED);
btn1.setFont(new Font("Arial", Font.BOLD, 20));
btn1.setName("btn1");
btn1.addActionListener(this);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.GREEN);
panel_1.setBounds(32, 186, 66, 66);
contentPane.add(panel_1);
panel_1.setLayout(null);
btn2 = new JButton("2");
btn2.setOpaque(false);
btn2.setBounds(0, 0, 66, 66);
panel_1.add(btn2);
btn2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn2.setFont(new Font("Arial", Font.BOLD, 20));
btn2.setName("btn2");
btn2.addActionListener(this);
JPanel panel_2 = new JPanel();
panel_2.setBackground(Color.GREEN);
panel_2.setBounds(32, 109, 66, 66);
contentPane.add(panel_2);
panel_2.setLayout(null);
btn3 = new JButton("3");
btn3.setOpaque(false);
btn3.setBounds(0, 0, 66, 66);
panel_2.add(btn3);
btn3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn3.setFont(new Font("Arial", Font.BOLD, 20));
btn3.setName("btn3");
btn3.addActionListener(this);
JPanel panel_3 = new JPanel();
panel_3.setBackground(Color.GREEN);
panel_3.setBounds(32, 32, 66, 66);
contentPane.add(panel_3);
panel_3.setLayout(null);
btn4 = new JButton("4");
btn4.setOpaque(false);
btn4.setBounds(0, 0, 66, 66);
panel_3.add(btn4);
btn4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn4.setFont(new Font("Arial", Font.BOLD, 20));
btn4.setName("btn4");
btn4.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ev) {
if (ev.getSource() instanceof JButton){
switch (((JButton) ev.getSource()).getName()){
case "btn1":
irAlPiso(1);
break;
case "btn2":
irAlPiso(2);
break;
case "btn3":
irAlPiso(3);
break;
case "btn4":
irAlPiso(4);
break;
}
}
}
private void activarBoton(int piso){
switch (piso){
case 1:
btn1.setContentAreaFilled(false);
btn1.setSelected(true);
btn1.setForeground(Color.RED);
btn2.setContentAreaFilled(true);
btn2.setSelected(false);
btn2.setForeground(Color.BLACK);
btn3.setContentAreaFilled(true);
btn3.setSelected(false);
btn3.setForeground(Color.BLACK);
btn4.setContentAreaFilled(true);
btn4.setSelected(false);
btn4.setForeground(Color.BLACK);
break;
case 2:
btn1.setContentAreaFilled(true);
btn1.setSelected(false);
btn1.setForeground(Color.BLACK);
btn2.setContentAreaFilled(false);
btn2.setSelected(true);
btn2.setForeground(Color.RED);
btn3.setContentAreaFilled(true);
btn3.setSelected(false);
btn3.setForeground(Color.BLACK);
btn4.setContentAreaFilled(true);
btn4.setSelected(false);
btn4.setForeground(Color.BLACK);
break;
case 3:
btn1.setContentAreaFilled(true);
btn1.setSelected(false);
btn1.setForeground(Color.BLACK);
btn2.setContentAreaFilled(true);
btn2.setSelected(false);
btn2.setForeground(Color.BLACK);
btn3.setContentAreaFilled(false);
btn3.setSelected(true);
btn3.setForeground(Color.RED);
btn4.setContentAreaFilled(true);
btn4.setSelected(false);
btn4.setForeground(Color.BLACK);
break;
case 4:
btn1.setContentAreaFilled(true);
btn1.setSelected(false);
btn1.setForeground(Color.BLACK);
btn2.setContentAreaFilled(true);
btn2.setSelected(false);
btn2.setForeground(Color.BLACK);
btn3.setContentAreaFilled(true);
btn3.setSelected(false);
btn3.setForeground(Color.BLACK);
btn4.setContentAreaFilled(false);
btn4.setSelected(true);
btn4.setForeground(Color.RED);
break;
}
}
private void irAlPiso(int piso){
if (piso>pisoActual){
this.direccion.setText("SUBE");
}else if (piso<pisoActual){
this.direccion.setText("BAJA");
} else {
this.direccion.setText("PISO ACTUAL");
}
this.piso.setText(String.valueOf(piso));
pisoActual = piso;
activarBoton(piso);
}
}
- Inicialmente el ascensor está en el piso 1.
- Por ejemplo: si se presiona el botón 3 se muestra en un JLabel el piso número 3 y en otra JLabel la dirección. La cadena "Sube", en caso de presionar un piso superior al actual. Mostramos la cadena "Baja" en el JLabel si se presiona un piso inferior. y si el piso donde se encuentra actualmente coincide con el presionado luego mostrar el mensaje "Piso actual".
Descargar el ejecutable
Descargar código fuente
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class Ascensor extends JFrame implements ActionListener{
private static final long serialVersionUID = -6298610548027787167L;
private JPanel contentPane;
private JButton btn4;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JLabel direccion;
private JLabel piso;
private int pisoActual;
public static void main(String[] args) {
Ascensor frame = new Ascensor();
frame.setVisible(true);
}
public Ascensor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 444, 387);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
pisoActual = 1;
JLabel lblPiso = new JLabel("PISO:");
lblPiso.setHorizontalAlignment(SwingConstants.RIGHT);
lblPiso.setName("lblPiso");
lblPiso.setFont(new Font("Arial", Font.BOLD, 18));
lblPiso.setBounds(136, 32, 127, 41);
contentPane.add(lblPiso);
JLabel lblDireccin = new JLabel("DIRECCI\u00D3N:");
lblDireccin.setName("lblPiso");
lblDireccin.setHorizontalAlignment(SwingConstants.RIGHT);
lblDireccin.setFont(new Font("Arial", Font.BOLD, 18));
lblDireccin.setBounds(136, 84, 127, 41);
contentPane.add(lblDireccin);
piso = new JLabel("1");
piso.setName("piso");
piso.setHorizontalAlignment(SwingConstants.LEFT);
piso.setFont(new Font("Arial", Font.BOLD, 18));
piso.setBounds(276, 32, 127, 41);
contentPane.add(piso);
direccion = new JLabel("PISO ACTUAL");
direccion.setName("direccion");
direccion.setHorizontalAlignment(SwingConstants.LEFT);
direccion.setFont(new Font("Arial", Font.BOLD, 18));
direccion.setBounds(276, 84, 127, 41);
contentPane.add(direccion);
JPanel panel = new JPanel();
panel.setBackground(Color.GREEN);
panel.setBounds(32, 263, 66, 66);
contentPane.add(panel);
panel.setLayout(null);
btn1 = new JButton("1");
btn1.setContentAreaFilled(false);
btn1.setBounds(0, 0, 66, 66);
panel.add(btn1);
btn1.setSelected(true);
btn1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn1.setForeground(Color.RED);
btn1.setFont(new Font("Arial", Font.BOLD, 20));
btn1.setName("btn1");
btn1.addActionListener(this);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.GREEN);
panel_1.setBounds(32, 186, 66, 66);
contentPane.add(panel_1);
panel_1.setLayout(null);
btn2 = new JButton("2");
btn2.setOpaque(false);
btn2.setBounds(0, 0, 66, 66);
panel_1.add(btn2);
btn2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn2.setFont(new Font("Arial", Font.BOLD, 20));
btn2.setName("btn2");
btn2.addActionListener(this);
JPanel panel_2 = new JPanel();
panel_2.setBackground(Color.GREEN);
panel_2.setBounds(32, 109, 66, 66);
contentPane.add(panel_2);
panel_2.setLayout(null);
btn3 = new JButton("3");
btn3.setOpaque(false);
btn3.setBounds(0, 0, 66, 66);
panel_2.add(btn3);
btn3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn3.setFont(new Font("Arial", Font.BOLD, 20));
btn3.setName("btn3");
btn3.addActionListener(this);
JPanel panel_3 = new JPanel();
panel_3.setBackground(Color.GREEN);
panel_3.setBounds(32, 32, 66, 66);
contentPane.add(panel_3);
panel_3.setLayout(null);
btn4 = new JButton("4");
btn4.setOpaque(false);
btn4.setBounds(0, 0, 66, 66);
panel_3.add(btn4);
btn4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn4.setFont(new Font("Arial", Font.BOLD, 20));
btn4.setName("btn4");
btn4.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ev) {
if (ev.getSource() instanceof JButton){
switch (((JButton) ev.getSource()).getName()){
case "btn1":
irAlPiso(1);
break;
case "btn2":
irAlPiso(2);
break;
case "btn3":
irAlPiso(3);
break;
case "btn4":
irAlPiso(4);
break;
}
}
}
private void activarBoton(int piso){
switch (piso){
case 1:
btn1.setContentAreaFilled(false);
btn1.setSelected(true);
btn1.setForeground(Color.RED);
btn2.setContentAreaFilled(true);
btn2.setSelected(false);
btn2.setForeground(Color.BLACK);
btn3.setContentAreaFilled(true);
btn3.setSelected(false);
btn3.setForeground(Color.BLACK);
btn4.setContentAreaFilled(true);
btn4.setSelected(false);
btn4.setForeground(Color.BLACK);
break;
case 2:
btn1.setContentAreaFilled(true);
btn1.setSelected(false);
btn1.setForeground(Color.BLACK);
btn2.setContentAreaFilled(false);
btn2.setSelected(true);
btn2.setForeground(Color.RED);
btn3.setContentAreaFilled(true);
btn3.setSelected(false);
btn3.setForeground(Color.BLACK);
btn4.setContentAreaFilled(true);
btn4.setSelected(false);
btn4.setForeground(Color.BLACK);
break;
case 3:
btn1.setContentAreaFilled(true);
btn1.setSelected(false);
btn1.setForeground(Color.BLACK);
btn2.setContentAreaFilled(true);
btn2.setSelected(false);
btn2.setForeground(Color.BLACK);
btn3.setContentAreaFilled(false);
btn3.setSelected(true);
btn3.setForeground(Color.RED);
btn4.setContentAreaFilled(true);
btn4.setSelected(false);
btn4.setForeground(Color.BLACK);
break;
case 4:
btn1.setContentAreaFilled(true);
btn1.setSelected(false);
btn1.setForeground(Color.BLACK);
btn2.setContentAreaFilled(true);
btn2.setSelected(false);
btn2.setForeground(Color.BLACK);
btn3.setContentAreaFilled(true);
btn3.setSelected(false);
btn3.setForeground(Color.BLACK);
btn4.setContentAreaFilled(false);
btn4.setSelected(true);
btn4.setForeground(Color.RED);
break;
}
}
private void irAlPiso(int piso){
if (piso>pisoActual){
this.direccion.setText("SUBE");
}else if (piso<pisoActual){
this.direccion.setText("BAJA");
} else {
this.direccion.setText("PISO ACTUAL");
}
this.piso.setText(String.valueOf(piso));
pisoActual = piso;
activarBoton(piso);
}
}
No hay comentarios:
Publicar un comentario