import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ExpFrame extends Frame {
int count = 0;
public ExpFrame() {
super("单击按钮实验");
this.setLayout(new FlowLayout());
Button b1 = new Button("退出");
Button b2 = new Button("点击");
TextField tf = new TextField("您单击了0次", 20);
this.add(b1);
this.add(b2);
this.add(tf);
this.pack();
this.setLocation(300, 300);
this.setVisible(true);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ExpFrame.this.setVisible(false);
System.exit(0);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
count ++;
tf.setText("您单击了" + count + "次");
}
});
}
public static void main(String[] args) {
new ExpFrame();
}
}
温馨提示:答案为网友推荐,仅供参考