How to add action listener that listens to multiple buttons

后端 未结 11 1109
温柔的废话
温柔的废话 2020-11-27 04:25

I\'m trying to figure out what i am doing wrong with action listeners. I\'m following multiple tutorials and yet netbeans and eclipse are giving me errors when im trying to

11条回答
  •  春和景丽
    2020-11-27 04:37

    The problem is that button1 is a local variable. You could do it by just change the way you add the actionListener.

    button.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    //button is pressed
                    System.out.println("You clicked the button");
                }});
    

    Or you make button1 a global variable.

提交回复
热议问题