


import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class NumFrame extends JFrame implements ActionListener {
JButton jb1, jb2;
int num = 50;
JLabel jl;
public NumFrame() {
jl = new JLabel("数字:" + num, JLabel.CENTER);
jb1 = new JButton("数字+1");
jb1.setActionCommand("+");
jb1.addActionListener(this);
jb2 = new JButton("数字-1");
jb2.setActionCommand("-");
jb2.addActionListener(this);
JPanel jp = new JPanel();
jp.add(jb1);
jp.add(jb2);
add(jl);
add(jp, BorderLayout.SOUTH);
setSize(220, 120);//窗口大小
setLocationRelativeTo(null);//居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭后退出
}
public static void main(String[] args) {
new NumFrame().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("+")) {//如果点击的是按钮一,那么数字+1
num++;
} else {//如果点击的是按钮二,那么数字-1
num--;
}
jl.setText("数字:" + num);
}
}