-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushCounter.java
More file actions
85 lines (66 loc) · 2.35 KB
/
PushCounter.java
File metadata and controls
85 lines (66 loc) · 2.35 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
/**
* PushCounter.java
*
* This class is a subclass of JFrame that implements the main
* functionality for the program. It provides a simple GUI with
* a button and label. The label displays the number of times
* that the button has been pressed.
*
*/
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PushCounter extends JFrame {
// The "Push Me" button
private JButton pushButton;
// Label that shows the number of pushes
private JLabel pushCountLabel;
// Counter that counts the number of pushes of the button.
private int pushCount;
/**
* Constructs the GUI components for this frame and makes
* the window visible on the screen.
*/
public PushCounter() {
super("Push Counter");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the main panel
JPanel mainPanel = new JPanel();
// Create the necessary components, and place them within
// the panel.
pushButton = new JButton( "Push Me!");
pushCountLabel = new JLabel( "Pushes: " + pushCount );
// Add an instance of the inner class to be the action listener
// for the pushButton.
pushButton.addActionListener(new PushCounterButtonListener());
mainPanel.add(pushButton);
mainPanel.add(pushCountLabel);
// Add the panel to this JFrame
this.add(mainPanel);
// Size this JFrame so that it is just big enough to hold the components.
this.setSize(300,70);
// Make this JFrame visible on the screen
this.setVisible(true);
}
/**
* This is a private inner class that is responsible for handling events
* from the button in this GUI.
*/
private class PushCounterButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
pushCount++;
pushCountLabel.setText("Pushes: " + pushCount);
}
}
/**
* main method to instantiate the GUI frame
*/
public static void main( String[] args )
{
PushCounter frame = new PushCounter();
}
}