-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleEvent.java
More file actions
64 lines (49 loc) · 1.28 KB
/
Copy pathSimpleEvent.java
File metadata and controls
64 lines (49 loc) · 1.28 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package threadbook.ch09;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleEvent extends Object {
private static void print(String msg) {
String name = Thread.currentThread().getName();
System.out.println(name + ": " + msg);
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
runIt();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
static JLabel label = new JLabel("--------");
public static void main(String[] args) {
JButton button = new JButton("Click Here");
JPanel panel = new JPanel(new FlowLayout());
panel.add(button);
panel.add(label);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
print("in actionPerformed()");
label.setText("CLICKED!");
}
});
JFrame f = new JFrame("SimpleEvent");
f.setContentPane(panel);
f.setSize(300, 100);
f.setVisible(true);
}
private static String key = "CLICKED!";
private static String key2 = "CLICKED!!!!!!!!!!!!!!!";
private static void runIt() {
if (label.getText().equals(key)) {
label.setText(key2);
}
else label.setText(key);
}
}