blueButton.addActionListener(new blueButtonListner());
输入此代码后会发生什么?
我认为 Java 编译器会创建一个名为 blueButtonListner()
的对象,它成为 addActionListener
的(参数)的输入
如果正如我猜的那样是正确的,那么这段代码也应该有效:
redButton.addActionListener(rr);
redButtonListener rr =new redButtonListener();
但是显示错误。谁能给我解释一下?
最佳答案
监听器对象需要在使用之前声明:
redButtonListener rr = new redButtonListener();
redButton.addActionListener(rr);
关于 blueButton.addActionListener(new blueButtonListner());
,你是正确的。此语句创建类 blueButtonListener
的实例,它会立即传递给 addActionListener
。
关于java - 需要 ActionListener 代码解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11945305/