-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrafficLight1.java
66 lines (51 loc) · 1.42 KB
/
TrafficLight1.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.awt.*;
//import java.awt.event.*;
import javax.swing.*;
class App implements ActionListener{
JFrame jfrm;
JLabel message;
ButtonGroup btngroup;
JRadioButton rbred, rbyellow, rbgreen;
App() {
jfrm = new JFrame("Traffic Lights");
message = new JLabel("Select Light");
btngroup = new ButtonGroup();
rbred = new JRadioButton("Red");
rbyellow = new JRadioButton("Yellow");
rbgreen = new JRadioButton("Green");
jfrm.setLayout(new FlowLayout());
rbred.setForeground(Color.RED);
rbyellow.setForeground(Color.YELLOW);
rbgreen.setForeground(Color.GREEN);
btngroup.add(rbred);
btngroup.add(rbyellow);
btngroup.add(rbgreen);
rbred.addActionListener(this);
rbyellow.addActionListener(this);
rbgreen.addActionListener(this);
jfrm.add(message);
jfrm.add(rbred);
jfrm.add(rbyellow);
jfrm.add(rbgreen);
jfrm.setSize(300, 200);
jfrm.setVisible(true);
}
public void actionPerformed(ActionEvent ie) {
String txt = ie.getActionCommand();
if(txt.equals("Red")) {
message.setForeground(Color.RED);
message.setText("STOP");
} else if(txt.equals("Yellow")) {
message.setForeground(Color.YELLOW);
message.setText("READY");
} else {
message.setForeground(Color.GREEN);
message.setText("GO");
}
}
}
public class TrafficLight1 {
public static void main(String[] args) {
new App();
}
}