-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUIDemo.java
79 lines (71 loc) · 1.99 KB
/
GUIDemo.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
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.*;
/**
* Minimal Java Swing application.
*
* @author Nathan Sprague
*
*/
public class GUIDemo extends JFrame
{
private JPanel panel;
private JButton biggerButton;
private JButton smallerButton;
private JButton changeName;
/**
* Set up the application.
*/
public GUIDemo()
{
setTitle("Bigger/Smaller");
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
biggerButton = new JButton("BIGGER");
smallerButton = new JButton("SMALLER");
changeName = new JButton("Change Name");
biggerButton.addActionListener(new ButtonHandler());
smallerButton.addActionListener(new ButtonHandler());
changeName.addActionListener(new ButtonHandler());
add(panel);
panel.add(biggerButton);
panel.add(smallerButton);
panel.add(changeName);
setVisible(true);
}
/**
* This inner class exists to handle button events. There are other ways
* this could have been done:
*
* 1. GUIDemo could implement ActionListener itself.
* 2. Anonymous inner classes could be used to hand the events.
*/
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Dimension size = getSize();
if (e.getSource().equals(biggerButton))
{
setSize(size.width + 10, size.height + 10);
}
if (e.getSource().equals(changeName)) {
setTitle("New Name");
}
else
{
setSize(size.width - 10, size.height - 10);
}
}
}
/**
* Start the app by creating a GUIDemo object.
*/
public static void main(String[] args)
{
GUIDemo app = new GUIDemo();
}
}