“public void actionPerformed(ActionEvent event)”中的变量delclared是无法传递的

时间:2017-04-10 18:54:14

标签: java swing events event-handling

我正在创建一个WindowBuilder GUI,需要将使用单选按钮创建的变量传递给EventHandler类以用于进一步处理。单选按钮事件的输出成功;但是,变量“df”,在actionPerformed方法中声明,未在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);
    }
}                               

4 个答案:

答案 0 :(得分:0)

rdbtnNo.addActionListener(new EventHandler());
你的代码中的

。 你也有其他的错误,但是当你在下面声明了那个内部类时,你必须在addActionListener方法中实例化它以利用它。

答案 1 :(得分:0)

它无法在其他类中解析,因为该变量的范围仅在该方法中:

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

答案 2 :(得分:0)

  

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

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

解决此问题的第一步是在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
            }
        }
    }
    
  3. 旁注:不要使用null布局!见Why is it frowned upon to use a null layout in Swing?

答案 3 :(得分:-1)

首先要摆脱匿名内部类并使用lambdas。它使您的代码更容易理解。

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);
    }
}