-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrafficLight.java
More file actions
45 lines (35 loc) · 1.33 KB
/
TrafficLight.java
File metadata and controls
45 lines (35 loc) · 1.33 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
import javax.swing.*;
import java.awt.*;
public class TrafficLight extends JFrame {
private JLabel messageLabel;
public TrafficLight(){
setTitle("Traffic Light Simulator");
setSize(400,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
messageLabel = new JLabel("");
messageLabel.setFont(new Font("Arial", Font.PLAIN, 24));
add(messageLabel);
JRadioButton redButton = new JRadioButton("Red");
JRadioButton yellowButton = new JRadioButton("Yellow");
JRadioButton greenButton = new JRadioButton("Green");
ButtonGroup group = new ButtonGroup();
group.add(redButton);
group.add(yellowButton);
group.add(greenButton);
add(redButton);
add(yellowButton);
add(greenButton);
redButton.addActionListener(e -> updateMessage("Stop", Color.RED));
yellowButton.addActionListener(e -> updateMessage("Ready", Color.YELLOW));
greenButton.addActionListener(e -> updateMessage("Go", Color.GREEN));
setVisible(true);
}
public void updateMessage(String message, Color color){
messageLabel.setText(message);
messageLabel.setForeground(color);
}
public static void main(String[] args) {
new TrafficLight();
}
}