How to add action listener that listens to multiple buttons

后端 未结 11 1137
温柔的废话
温柔的废话 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:45

    The first problem is that button1 is a local variable of the main method, so the actionPerformed method doesn't have access to it.

    The second problem is that the ActionListener interface is implemented by the class calc, but no instance of this class is created in the main method.

    The usual way to do what you want is to create an instance of calc and make button1 a field of the calc class.

提交回复
热议问题