-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_ended_1.java
More file actions
55 lines (45 loc) · 1.51 KB
/
open_ended_1.java
File metadata and controls
55 lines (45 loc) · 1.51 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class open_ended_1 extends JFrame implements ActionListener {
private JLabel messageLabel;
private JButton redButton, yellowButton, greenButton;
public open_ended_1() {
setTitle("Traffic Light Simulator");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
messageLabel = new JLabel("Select a light:");
redButton = new JButton("Red");
yellowButton = new JButton("Yellow");
greenButton = new JButton("Green");
redButton.addActionListener(this);
yellowButton.addActionListener(this);
greenButton.addActionListener(this);
add(messageLabel);
add(redButton);
add(yellowButton);
add(greenButton);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String light = "";
String message = "";
if (e.getSource() == redButton) {
light = "Red";
message = "Stop";
} else if (e.getSource() == yellowButton) {
light = "Yellow";
message = "Ready";
} else if (e.getSource() == greenButton) {
light = "Green";
message = "Go";
}
messageLabel.setText("The traffic light is " + light + ". " + message);
}
public static void main(String[] args) {
new open_ended_1();
}
}