62,623
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SwingApplication {
private static int numClicks=0;
private static JLabel l;
public static void main(String args[]) {
JFrame f = new JFrame("Java10-[C.R.S.M]K01");
JButton b = new JButton("Swing Button");
l = new JLabel("You have click: " + "0 " + "次");
JPanel p = new JPanel();
b.addActionListener(new ButtonHandler());
p.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
p.setLayout(new GridLayout(0, 1));
f.add(p);
p.add(b);
p.add(l);
f.setSize(80, 80);
f.setVisible(true);
}
static class ButtonHandler implements ActionListener { // 加上 public 也不对。
public void actionPerformed(ActionEvent e) {
numClicks++;
l.setText("You have click: " + numClicks + "次");// 输出l后面那个点时没有弹出对应的方法。
}
}
}