-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
54 lines (44 loc) · 1.49 KB
/
GUI.java
File metadata and controls
54 lines (44 loc) · 1.49 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
/*************************************************************************
* Compilation: javac GUI.java
* Execution: java GUI
*
* A minimal Java program with a graphical user interface. The
* GUI prints out the number of times the user clicks a button.
*
* % java GUI
*
*************************************************************************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUI implements ActionListener {
private int clicks = 0;
private JLabel label = new JLabel("Number of clicks: 0 ");
private JFrame frame = new JFrame();
public GUI() {
// the clickable button
JButton button = new JButton("Click Me");
button.addActionListener(this);
// the panel with the button and text
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new GridLayout(0, 1));
panel.add(button);
panel.add(label);
// set up the frame and display it
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("GUI");
frame.pack();
frame.setVisible(true);
}
// process the button clicks
public void actionPerformed(ActionEvent e) {
clicks++;
label.setText("Number of clicks: " + clicks);
};
// create one Frame
public static void main(String[] args) {
new GUI();
}
}