import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
public class GUI implements ActionListener {
int count = 0;
private JFrame frame;
private JPanel panel;
private JLabel label;
//this public class is constructor.
public GUI () {
frame = new JFrame();
JButton button = new JButton("Click me");
button.addActionListener(this);
label = new JLabel("Number of clicks: 0");
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new GridLayout(0, 1));
panel.add(button);
panel.add(label);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(new Color(0, 20, 100));
frame.setTitle("First GUI");
frame.pack();
frame.setVisible(true);
button.setBackground(new Color (25, 100, 100));
button.setForeground(new Color (100, 25, 255));
label.setForeground(new Color (0, 20, 25));
}
public static void main(String[] args) {
new GUI();
}
//we use this to increase count number.
@Override
public void actionPerformed(ActionEvent e) {
count ++;
label.setText("Number of clicks: " + count);
}
}