Advertisement
Akbari21

Class Main

Apr 9th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. public class Main implements ActionListener {
  4.  
  5.  private static void createAndShowGUI() {
  6.  // make frame..
  7.  JFrame frame = new JFrame("I am a JFrame");
  8. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9.  frame.setBounds(20,30,300,100);
  10.  frame.getContentPane().setLayout(null);
  11.  // make a button
  12.  JButton butt=new JButton("Click me");
  13.  frame.getContentPane().add(butt);
  14.  butt.setBounds(20, 20, 200,20);
  15. //instantiate an application object
  16. Main app = new Main();
  17. // make the label
  18. app.label = new JLabel("0 clicks");
  19. app.label.setBounds(20,40, 200,20);
  20.  
  21. frame.getContentPane().add(app.label);
  22.  
  23. // set the application object to be the thing which
  24. // listens to the button
  25. butt.addActionListener(app);
  26. frame.setVisible(true);
  27. }
  28.  public void actionPerformed(ActionEvent e)
  29.  {
  30.   // Ini akan dieksekusi ketika button diklik
  31.   clickCount++;
  32.   label.setText("Clicks = "+clickCount);
  33.  }
  34.   public static void main(String[] args) {
  35.   // Memulai Swing GUI
  36.   SwingUtilities.invokeLater(new Runnable() {
  37.   public void run() {
  38.   createAndShowGUI();
  39.   }
  40.   });
  41.   }
  42.  
  43.  // application object fields
  44.  int clickCount=0;
  45.  JLabel label;
  46.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement