-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path程序8_4.java
46 lines (40 loc) · 1.15 KB
/
程序8_4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class 程序8_4 extends WindowAdapter implements ActionListener{
JFrame f;
JButton b;
JTextField tf;
int tag = 0;
public static void main(String[] args) {
程序8_4 be = new 程序8_4();
be.go();
}
public void go() {
f = new JFrame("JButton Example");
b = new JButton("Sample");
b.addActionListener(this);
f.getContentPane().add(b, "South");
tf = new JTextField();
f.getContentPane().add(tf, "Center");
f.addWindowListener(this);
f.setSize(300,150);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) { //实现接口中的actionPerformed()方法
String s1 = "You have pressed the Button!";
String s2 = "You do another time!";
if(tag == 0) {
tf.setText(s1);;
tag = 1;
} else {
tf.setText(s2);
tag = 0;
}
}
// 覆盖WindowAdapter类中的windowClosing()方法
public void windowClosing(WindowEvent e) {
System.exit(0);
// 结束程序运行
}
}