确实很简单,但是为了5分写个程序,一是划不来,二是对你毫无益处,这个小作业还算是我做的。你可以去查阅thinking in java中文版(java编程思想)的swing等章节,你这个题目几乎和上面的例子一样,书在网上很容易下载;或者可以在google或百度搜索swing,event和listener的相关词条和例子,应该不出一个小时你就会学会自己编写
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class FormInit {
protected int count=0;
public FormInit(){
JFrame mainFramebox=new JFrame();
mainFramebox.setTitle("测试点击");
mainFramebox.setBounds(0, 0, 300, 100);
mainFramebox.setLayout(null);
final JLabel JL1=new JLabel();
JL1.setBounds(70, 0, 160, 30);
JL1.setText("尚未点击");
final JButton JB1=new JButton();
JB1.setBounds(10,30, 80, 30);
JB1.setText("CLick Me");
final JButton JB2=new JButton();
JB2.setBounds(100,30, 80, 30);
JB2.setText("Reset");
final JButton JB3=new JButton();
JB3.setBounds(190,30, 80, 30);
JB3.setText("Eixt");
mainFramebox.add(JL1);
mainFramebox.add(JB1);
mainFramebox.add(JB2);
mainFramebox.add(JB3);
mainFramebox.setVisible(true);
JB1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
count++;
if(e.getSource()== JB1){
String JL1String="这是第"+count+"次点击!";
JL1.setText(JL1String);
}
}
});
JB2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
count=0;
if(e.getSource()== JB2){
String JL1String="重置点击次数";
JL1.setText(JL1String);
}
}
});
JB3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(1);
}
});
}
}
顺手写的,发给你吧