Advertisement
Guest User

Simple GUI

a guest
Jan 24th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8.  
  9. public class GUI {
  10.  
  11.     public static void main(String[] args) {
  12.         GUI gui = new GUI();
  13.     }
  14.  
  15.     private int count = 0;
  16.    
  17.     public GUI() {
  18.        
  19.         JFrame frame = new JFrame();
  20.         frame.setSize(200, 100);
  21.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         frame.setLocationRelativeTo(null);
  23.         frame.setTitle("Test GUI");
  24.        
  25.         JLabel label = new JLabel("0");
  26.        
  27.         JButton button = new JButton("Click me");
  28.         button.addActionListener(new ActionListener() {
  29.             @Override
  30.             public void actionPerformed(ActionEvent e) {
  31.                 count++;
  32.                 label.setText(count + "");
  33.             }
  34.         });
  35.        
  36.         frame.add(label, BorderLayout.CENTER);
  37.         frame.add(button, BorderLayout.SOUTH);
  38.        
  39.         frame.setVisible(true);
  40.     }
  41.    
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement