我试图弄清楚我对动作听众做错了什么.我正在关注多个教程,但是当我尝试使用动作监听器时,netbeans和eclipse会给我错误.
下面是一个简单的程序,我试图让按钮工作.
我究竟做错了什么?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class calc extends JFrame implements ActionListener {
public static void main(String[] args) {
JFrame calcFrame = new JFrame();
calcFrame.setSize(100, 100);
calcFrame.setVisible(true);
JButton button1 = new JButton("1");
button1.addActionListener(this);
calcFrame.add(button1);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1)
}
}
Run Code Online (Sandbox Code Playgroud)
动作监听器永远不会注册,因为if(e.getSource() == button1)它无法看到button1,错误说无法找到符号.