-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButtonTest.java
66 lines (50 loc) · 1.6 KB
/
ButtonTest.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
62
63
64
65
66
package button;
//ButtonTest.java
//Creating JButtons.
//Java core packages
import java.awt.*;
import java.awt.event.*;
//Java extension packages
import javax.swing.*;
public class ButtonTest extends JFrame {
private JButton plainButton, fancyButton;
// set up GUI
public ButtonTest()
{
super("Testing Buttons");
// get content pane and set its layout
Container container = getContentPane();
container.setLayout(new FlowLayout());
// create buttons
plainButton = new JButton("Plain Button");
container.add(plainButton);
Icon bug1 = new ImageIcon("bug1.gif");
Icon bug2 = new ImageIcon("bug2.gif");
fancyButton = new JButton("Fancy Button", bug1);
fancyButton.setRolloverIcon(bug2);
container.add(fancyButton);
// create an instance of inner class ButtonHandler
// to use for button event handling
ButtonHandler handler = new ButtonHandler();
fancyButton.addActionListener(handler);
plainButton.addActionListener(handler);
setSize(275, 100);
setVisible(true);
}
// execute application
public static void main(String args[])
{
ButtonTest application = new ButtonTest();
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
// inner class for button event handling
private class ButtonHandler implements ActionListener {
// handle button event
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null,
"You pressed: " + event.getActionCommand());
}
} // end private inner class ButtonHandler
} // end class ButtonTest