public class TestButtonAction extends JFrame {
private JPanel p;
private JButton ok;
private JTextField text;
private int sum = 0;
public TestButtonAction() {
p = new JPanel(new FlowLayout(FlowLayout.LEFT));
ok = new JButton("点击");
text = new JTextField(10);
p.add(ok);
p.add(text);
ButtonListener listen = new ButtonListener();
ok.addActionListener(listen);
this.add(p);
this.setSize(200,100);
this.setLocation(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
sum++;
text.setText(sum + "次");
}
}
public static void main(String args[]) {
TestButtonAction a = new TestButtonAction();
a.setVisible(true);
}
}
******************************************************按钮**************************************************************
public class Test extends JFrame {
private JLabel username, userPassword, userRePassword, userSex, userLike,
userAdder, userdegree;
private JTextField userNameText;
private JPasswordField userPasswordText, userRePasswordText;
private JRadioButton male, fmale;
private JCheckBox read, internet, swing, tourist;
private JTextArea userAdderText;
private JComboBox userdegreebox;
private JButton ok, no;
private JPanel p;
public Test() {
super("用户注册");
p = new JPanel(new GridLayout(8, 1));
username = new JLabel("用户名:");
userPassword = new JLabel("密码:");
userRePassword = new JLabel("确认密码");
userSex = new JLabel("性别");
userLike = new JLabel("爱好");
userAdder = new JLabel("地址");
userdegree = new JLabel("学历");
userNameText = new JTextField(10);
userPasswordText = new JPasswordField(10);
userRePasswordText = new JPasswordField(10);
male = new JRadioButton("男");
fmale = new JRadioButton("女");
read = new JCheckBox("读书");
internet = new JCheckBox("上网");
swing = new JCheckBox("游泳");
tourist = new JCheckBox("旅游");
userAdderText = new JTextArea(1, 10);
String[] degreeStr = { "小学", "初中", "高中", "本科", "研究生", "博士" };
userdegreebox = new JComboBox(degreeStr);
ok = new JButton("确定");
no = new JButton("取消");
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p1.add(username);
p1.add(userNameText);
p.add(p1);
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p2.add(userPassword);
p2.add(userPasswordText);
p.add(p2);
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p3.add(userRePassword);
p3.add(userRePasswordText);
p.add(p3);
JPanel p4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p4.add(userSex);
ButtonGroup bk = new ButtonGroup();
bk.add(male);
bk.add(fmale);
p4.add(male);
p4.add(fmale);
p.add(p4);
JPanel p5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p5.add(userLike);
p5.add(read);
p5.add(internet);
p5.add(swing);
p5.add(tourist);
p.add(p5);
JPanel p6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p6.add(userAdder);
p6.add(userAdderText);
p.add(p6);
JPanel p7 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p7.add(userdegree);
p7.add(userdegreebox);
p.add(p7);
JPanel p8 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p8.add(ok);
p8.add(no);
p.add(p8);
this.add(p);
}
public static void main(String args[]) {
Test MyFrame = new Test();
MyFrame.setEnabled(true);
MyFrame.setSize(300, 400);
MyFrame.setLocation(200, 300);
MyFrame.setResizable(false);
MyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyFrame.setVisible(true);
}
}
****************************************************可以考虑升级*********************************************************