Hello very afternoon or evening ... my question is the following can you use the switches to compare objects ?? see it this way I have a window with 3 buttons, each button changes the background to a different color could the switch be implemented to know which button will execute the event?
package ClasesInterfaz;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CreandoMarcoConEventos {
public static void main(String[] args) {
MarcoEvento miMarco = new MarcoEvento();
miMarco.setVisible(true);
miMarco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MarcoEvento extends JFrame{
MarcoEvento(){
setSize(400,300);
setLocation(300,200);
LaminaEvento laminita = new LaminaEvento();
add(laminita);
}
}
class LaminaEvento extends JPanel implements ActionListener{
JButton botonazul = new JButton("Boton azul");
JButton botonRojo = new JButton("Boton Rojo");
JButton botonAmarillo = new JButton("Boton amarillo");
public void actionPerformed(ActionEvent e) {
Object objetoEvento = e.getSource();
if(objetoEvento==botonazul) {
setBackground(Color.BLUE);
}
else if(objetoEvento==botonRojo) {
setBackground(Color.RED);
}
else {
setBackground(Color.yellow);
}
}
LaminaEvento(){
add(botonRojo);
add(botonAmarillo);
add(botonazul);
botonazul.addActionListener(this);
botonAmarillo.addActionListener(this);
botonRojo.addActionListener(this);
}
}
I want to know if it is possible to change the if .. else by switch and if possible, how to do it? thanks in advance.