-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathBuggyButtonTest.java
More file actions
73 lines (62 loc) · 1.73 KB
/
Copy pathBuggyButtonTest.java
File metadata and controls
73 lines (62 loc) · 1.73 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
66
67
68
69
70
71
72
73
package debugger;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* @version 1.22 2007-05-14
* @author Cay Horstmann
*/
public class BuggyButtonTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new BuggyButtonFrame();
frame.setTitle("BuggyButtonTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class BuggyButtonFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
public BuggyButtonFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add panel to frame
BuggyButtonPanel panel = new BuggyButtonPanel();
add(panel);
}
}
class BuggyButtonPanel extends JPanel
{
public BuggyButtonPanel()
{
ActionListener listener = new ButtonListener();
JButton yellowButton = new JButton("Yellow");
add(yellowButton);
yellowButton.addActionListener(listener);
JButton blueButton = new JButton("Blue");
add(blueButton);
blueButton.addActionListener(listener);
JButton redButton = new JButton("Red");
add(redButton);
redButton.addActionListener(listener);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String arg = event.getActionCommand();
if (arg.equals("yellow")) setBackground(Color.yellow);
else if (arg.equals("blue")) setBackground(Color.blue);
else if (arg.equals("red")) setBackground(Color.red);
}
}
}