-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCounter.java
More file actions
126 lines (111 loc) · 4.98 KB
/
Counter.java
File metadata and controls
126 lines (111 loc) · 4.98 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
* A simple Swing component that counts button presses — the "Hello World"
* of event-driven GUI programming.
* <p>
* This example demonstrates:
* <ul>
* <li>Custom JPanel subclass with internal layout</li>
* <li>Event listeners — shown in three evolutionary styles:
* local class (old), anonymous class (less old), and lambda (modern)</li>
* <li>How nested code can access outer fields and effectively final locals</li>
* <li>GridLayout for arranging multiple independent components</li>
* </ul>
*
* Updated for modern Java with lambda event handling and Swing best practices.
*
* @author Ilkka Kokkarinen
*/
public class Counter extends JPanel {
private static final Font BUTTON_FONT = new Font("SansSerif", Font.PLAIN, 28);
private static final Font LABEL_FONT = new Font("Monospaced", Font.BOLD, 50);
private int count = 0;
// -----------------------------------------------------------------------
// Construction — wire up the label, button, and event listener.
// -----------------------------------------------------------------------
public Counter() {
setPreferredSize(new Dimension(250, 80));
// Every Swing component can have an arbitrarily festive border.
setBorder(BorderFactory.createLoweredSoftBevelBorder());
// BoxLayout arranges children along a single axis (here, left to right).
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
// The count display label.
var countLabel = new JLabel("0");
countLabel.setBorder(BorderFactory.createEtchedBorder());
countLabel.setFont(LABEL_FONT);
add(countLabel);
// The increment button.
var incrementButton = new JButton("Press me");
incrementButton.setFont(BUTTON_FONT);
incrementButton.setToolTipText("Click to increase the count");
add(incrementButton);
// --- THREE WAYS TO ATTACH AN EVENT LISTENER ---
//
// Style 1 (Java 1.1+): A local class that implements ActionListener.
// Verbose, but makes the mechanism explicit. The local class can access
// the enclosing method's effectively final local variables (countLabel)
// and the outer object's fields (count).
//
// class IncrementListener implements ActionListener {
// @Override
// public void actionPerformed(ActionEvent event) {
// count++;
// countLabel.setText(Integer.toString(count));
// }
// }
// incrementButton.addActionListener(new IncrementListener());
//
// Style 2 (also Java 1.1+): An anonymous class — same thing, but
// without bothering to name the class.
//
// incrementButton.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent event) {
// count++;
// countLabel.setText(Integer.toString(count));
// }
// });
//
// Style 3 (Java 8+): A lambda expression. Since ActionListener is a
// functional interface (exactly one abstract method), the compiler can
// infer everything from this compact syntax. This is the modern idiom.
incrementButton.addActionListener(event -> {
count++;
countLabel.setText(Integer.toString(count));
});
}
// -----------------------------------------------------------------------
// Main — create a grid of independent Counter instances.
// -----------------------------------------------------------------------
public static void main(String[] args) {
// Swing components must be created on the Event Dispatch Thread.
SwingUtilities.invokeLater(() -> {
var frame = new JFrame("Counter Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// GridLayout arranges components in a uniform grid. Every cell
// gets the same size, regardless of its content — which is why
// the text field in the last cell will be stretched to match.
int gridSize = 4;
frame.setLayout(new GridLayout(gridSize, gridSize));
for (int i = 0; i < gridSize * gridSize - 1; i++) {
frame.add(new Counter());
}
// Make the bottom-right cell something different, to demonstrate
// that GridLayout forces uniform sizing on all its children.
var textField = new JTextField("Look at me, I am a text field");
frame.add(textField);
frame.pack();
frame.setVisible(true);
});
}
}