forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounterGrid.java
More file actions
42 lines (32 loc) · 1.1 KB
/
CounterGrid.java
File metadata and controls
42 lines (32 loc) · 1.1 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
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class CounterGrid extends JFrame {
private class CountListener implements ActionListener {
int count = 0;
public void actionPerformed(ActionEvent e) {
System.out.println(count++);
}
}
public CounterGrid() {
super("Counter");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(3,1)); // 3 rows, 1 column
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ExitListener());
JLabel counterLabel = new JLabel("Count: 0");
JButton counterButton = new JButton("Increment count");
counterButton.addActionListener(new CountListener());
add(counterButton);
add(counterLabel);
add(exitButton);
setSize(200, 300);
}
public static void main(String[] args) {
CounterGrid cf = new CounterGrid();
cf.setVisible(true);
}
}