-
Notifications
You must be signed in to change notification settings - Fork 110
/
traffic.java
83 lines (74 loc) · 2.21 KB
/
traffic.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class traffic implements ItemListener {
JFrame f;
JRadioButton rb1;
JRadioButton rb2;
JRadioButton rb3;
JPanel p1;
JPanel p2;
JPanel p3;
JPanel p4;
ButtonGroup bg;
traffic() {
f = new JFrame("Traffic Light");
f.setSize(500,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
rb1 = new JRadioButton("RED");
rb2 = new JRadioButton("YELLOW");
rb3 = new JRadioButton("GREEN");
rb1.setBounds(50, 230,100,50);
rb2.setBounds(200, 230,100,50);
rb3.setBounds(350, 230,100,50);
bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
// p4 = new JPanel();
// p4.add(rb1);
// p4.add(rb2);
// p4.add(rb3);
rb1.addItemListener(this);
rb2.addItemListener(this);
rb3.addItemListener(this);
p1 = new JPanel();
p1.setBackground(Color.WHITE);
p1.setBounds(50, 50, 100, 80);
p2 = new JPanel();
p2.setBackground(Color.WHITE);
p2.setBounds(200, 50, 100, 80);
p3 = new JPanel();
p3.setBackground(Color.WHITE);
p3.setBounds(350, 50, 100, 80);
f.add(p1);
f.add(p2);
f.add(p3);
f.add(rb1);
f.add(rb2);
f.add(rb3);
f.setVisible(true);
}
public static void main(String[] args) {
traffic ob = new traffic();
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == rb1) {
p1.setBackground(Color.RED);
p2.setBackground(Color.WHITE);
p3.setBackground(Color.WHITE);
}
if(e.getSource() == rb2) {
p1.setBackground(Color.WHITE);
p2.setBackground(Color.YELLOW);
p3.setBackground(Color.WHITE);
}
if(e.getSource() == rb3) {
p1.setBackground(Color.WHITE);
p2.setBackground(Color.WHITE);
p3.setBackground(Color.GREEN);
//pull by GUDSKULL
}
}
}