-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic light
More file actions
65 lines (64 loc) · 1.45 KB
/
traffic light
File metadata and controls
65 lines (64 loc) · 1.45 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
65
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class trafflight extends JFrame implements ActionListener{
JRadioButton bg,br,by;
JTextField lg,lr,ly,text;
trafflight()
{ setVisible(true);
setLayout(null);
setSize(600,600);
bg=new JRadioButton("Green");
by=new JRadioButton("Yellow");
br=new JRadioButton("Red");
bg.setBounds(200, 50, 100, 50);
br.setBounds(300, 50, 100, 50);
by.setBounds(400, 50, 100, 50);
add(bg);
add(br);
add(by);
lg=new JTextField();
lr=new JTextField();
ly=new JTextField();
text=new JTextField();
lg.setBounds(300, 200, 50, 50);
lr.setBounds(300, 300, 50, 50);
ly.setBounds(300, 400, 50, 50);
text.setBounds(250,150,200,50);
add(lg);
add(ly);
add(lr);
add(text);
bg.addActionListener((ActionListener) this);
br.addActionListener((ActionListener) this);
by.addActionListener((ActionListener) this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bg)
{
lg.setBackground(Color.GREEN);
ly.setBackground(null);
lr.setBackground(null);
text.setText("Go");
}
else if(e.getSource()==br)
{
lg.setBackground(null);
ly.setBackground(null);
lr.setBackground(Color.red);
text.setText("Stop");
}
else if(e.getSource()==by)
{
lg.setBackground(null);
ly.setBackground(Color.YELLOW);
lr.setBackground(null);
text.setText("Go slow");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new trafflight();
}
}