给你个完整代码你看看
import java.applet.Applet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class p107_6 extends Applet implements ActionListener {
JButton b;
JFrame f;
int n = 0;
public void init() {
f = new JFrame("计数");
f.setVisible(true);
b = new JButton("点我有次数哦");
f.getContentPane().add(b);
b.setVisible(true);
b.addActionListener(this);
f.pack();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
n++;
b.setLabel(String.valueOf(n));
}
}
}
1、ActionListener接口没有导入 import java.awt.event.ActionListener;
2、int n=0; 应该在方法外定义