-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColorChangeDemoAWT.java
More file actions
53 lines (40 loc) · 1.48 KB
/
ColorChangeDemoAWT.java
File metadata and controls
53 lines (40 loc) · 1.48 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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ColorChangeDemoAWT extends Frame {
private Button redButton, greenButton, blueButton;
private Panel colorPanel;
public ColorChangeDemoAWT() {
setTitle("Color Change Demo");
setSize(300, 200);
setLayout(new BorderLayout());
colorPanel = new Panel();
colorPanel.setBackground(Color.WHITE);
redButton = new Button("Red");
greenButton = new Button("Green");
blueButton = new Button("Blue");
redButton.addActionListener(new ColorButtonListener(Color.RED));
greenButton.addActionListener(new ColorButtonListener(Color.GREEN));
blueButton.addActionListener(new ColorButtonListener(Color.BLUE));
Panel buttonPanel = new Panel();
buttonPanel.add(redButton);
buttonPanel.add(greenButton);
buttonPanel.add(blueButton);
add(colorPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
private class ColorButtonListener implements ActionListener {
private Color color;
public ColorButtonListener(Color color) {
this.color = color;
}
@Override
public void actionPerformed(ActionEvent e) {
colorPanel.setBackground(color);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new ColorChangeDemoAWT());
}
}