В моем коде есть разные файлы для разных панелей, и я хочу добавить файл для прослушивателя действий. Я объявил мои переменные как статические, чтобы слушатель действия мог видеть их, но не видит их.
import java.awt.*;
import javax.swing.*;
class Respuestas extends JPanel{
static JRadioButton cb1 =new JRadioButton("1");
static JRadioButton cb2 =new JRadioButton("2");
static JRadioButton cb3 =new JRadioButton("3");
static JRadioButton cb4 =new JRadioButton("4");
static JRadioButton cb5 =new JRadioButton("5");
public Respuestas(){
setLayout(new GridLayout(1,5));
this.add(cb1);
this.add(cb2);
this.add(cb3);
this.add(cb4);
this.add(cb5);
Manejador manejador = new Manejador();
cb1.addActionListener(manejador);
cb2.addActionListener(manejador);
cb3.addActionListener(manejador);
cb4.addActionListener(manejador);
cb5.addActionListener(manejador);
}
}
import javax.swing.*;
class Botones extends JPanel{
public static JButton sig = new JButton("Siguiente");
public Botones(){
this.add(sig);
Manejador manejador = new Manejador();
sig.addActionListener(manejador);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Manejador implements ActionListener{
public void actionPerformed (ActionEvent evento) {
if(evento.getSource()==sig) { //Error
System.out.println("Siguiente");
}
else if(evento.getSource()==cb1) { //Error
System.out.println("1");
}
else if(evento.getSource()==cb2) { //Error
System.out.println("2");
}
else if(evento.getSource()==cb3) { //Error
System.out.println("3");
}
else if(evento.getSource()==cb4) { //Error
System.out.println("4");
}
else if(evento.getSource()==cb5) { //Error
System.out.println("5");
}
}
}