"public void actionPerformed(ActionEvent event)"中声明的变量无法传递



我正在创建一个 WindowBuilder GUI,需要将使用单选按钮创建的变量传递给 EventHandler 类以用于进一步处理。 单选按钮事件的输出成功;但是,在 actionPerforming 方法中声明的变量 "df" 在 EventHanler 类中未解析。 任何帮助将不胜感激。

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);
}
}                               
rdbtnNo.addActionListener(new EventHandler());

在您的代码中。 您还有其他错误,但是当您在下面声明该内部类时,您必须在 addActionListener 方法中实例化它才能利用它。

它不能在另一个类中解析,因为该变量的作用域仅在该方法中:

public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
System.out.println(command);
int df = 20;                    
} 

变量"df",该变量在 actionPerforming 方法中声明,未在 EventHanler 类中解析。

这是因为范围可变。在您的示例中,您将df声明为要传递给addActionListener(ActionListener)的匿名内部类的actionPerformed(ActionEvent)方法中的局部变量。局部变量只能在创建它们的代码块内访问。这意味着除了actionPerformed(ActionEvent)方法之外,无法在其他任何地方访问您的df变量。

解决此问题的第一步是将df作为Test类中的实例变量,以便可以在actionPerformed(ActionEvent)方法内部和外部访问它。

从这里开始,有两种可能的方法:

  1. 对两个按钮使用第二个匿名内部类

    public class Test {
    private int df;
    public Test() {
    // ...
    final JButton button = new JButton("Click Me");
    button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    df = 20;
    }
    });
    final JButton button2 = new JButton("Click Me Again");
    button2.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    if(df == 20) { // got instance
    // TODO do a thing
    }
    }
    });
    // ...
    }
    }
    
  2. df传递给EventHandler的构造函数

    public class Test {
    private int df;
    public Test() {
    // ... button1 ...
    final JButton button2 = new JButton("Click Me Again");
    button2.addActionListener(new EventHandler());
    // ...
    }
    }
    // different file
    public class EventHandler implements ActionListener {
    private int df;
    public EventHandler(int df) {
    this.df = df; // got instance
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    if(df == 20) {
    // TODO do a thing
    }
    }
    }
    

旁注:不要使用null布局!请参阅为什么在 Swing 中使用空布局不受欢迎?

第一件事是摆脱匿名的内部类,改用lambda。 它使您的代码更易于理解。

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(event -> pressedTheButton(event));
rdbtn.setActionCommand("event");
rdbtn.addActionListener(new EventHandler());
}
public void pressedTheButton(ActionEvent event) {
String command = event.getActionCommand();
System.out.println(command);
int df = 20;
printStuff(df);
}
public void printStuff(int input) {
System.out.println(input);
}

///DELETE THIS.  This is unneeded, use Java 8 stuff, it's awesome////
public class EventHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println(df);
}
}       

最新更新