-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultipleEvents2.java
61 lines (54 loc) · 1.95 KB
/
MultipleEvents2.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
/* MultipleEvents2.java
*
* This program illustrates one way to handle multiple events in a
* single program - by having a different listener for each event
* source. Each listeners is an instance of an anonymous inner class
* declared a the point where the listener is created, whose
* actionPerformed() method changes to the appropriate color. The GUI
* consists of a window that can be drawn in one of three colors, and
* three buttons that can be used to change the color
*
* Copyright (c) 2000, 2004, 2010 - Russell C. Bjork
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MultipleEvents2 extends JFrame {
// Constructor - create the GUI
public MultipleEvents2() {
super("MultipleEvents Demo #2");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add the buttons - in their own panel
JPanel buttonPanel = new JPanel();
JButton redButton = new JButton("Red");
redButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
getContentPane().setBackground(Color.red);
repaint();
}
});
buttonPanel.add(redButton);
JButton greenButton = new JButton("Green");
greenButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
getContentPane().setBackground(Color.green);
repaint();
}
});
buttonPanel.add(greenButton);
JButton blueButton = new JButton("Blue");
blueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
getContentPane().setBackground(Color.blue);
repaint();
}
});
buttonPanel.add(blueButton);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
setSize(300, 200);
}
// Main program - Create the GUI and let it do the work
public static void main(String [] args) {
new MultipleEvents2().setVisible(true);
}
}