我正在创建WindowBuilder GUI,并且需要将使用单选按钮创建的变量传递给EventHandler类以用于进一步处理。单选按钮事件的输出成功;但是,在EventHanler类中未解析actionPerformed方法中声明的变量“df”。任何帮助将不胜感激。“public void actionPerformed(ActionEvent event)”中声明的变量无法传递
public TestClass() {
/* INSERT RADIOBUTTON INTO FRAME. */
JRadioButton rdbtnNo = new JRadioButton("No");
rdbtnNo.setFont(new Font("Tahoma", Font.BOLD, 12));
rdbtnNo.setBounds(332, 509, 63, 23);
frame.getContentPane().add(rdbtnNo);
/* LISTEN FOR RADIOBUTTON BUTTON. */
rdbtnNo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
System.out.println(command);
int df = 20;
}
});
rdbtn.setActionCommand("event");
rdbtn.addActionListener(new EventHandler());
}
public class EventHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println(df);
}
}
TestClass()方法返回什么? – Omore
阅读关于变量范围:http://www.java-made-easy.com/variable-scope.html – ControlAltDel