Click event pada java

import java.awt.*;
   
import java.awt.event.*;
   
    import javax.swing.*;
   
public class ClickMe2 extends JFrame {
   
    private JButton tombol, btnExit;
   
        public ClickMe2() {
   
            super ("Event Handling");
   
            Container container = getContentPane();

            container.setLayout(new FlowLayout());
   
            ClickListener cl = new ClickListener ();
   
            tombol = new JButton ("Click Me!");
   
        tombol.addActionListener(cl);
   
        container.add(tombol);
   
            btnExit = new JButton ("Exit");
   
            btnExit.addActionListener(cl);
   
            container.add(btnExit);

        setSize (200,100);

       setVisible (true);

    }

    public static void main (String arg[]) {

        ClickMe2 test = new ClickMe2();

       test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    //inner class

    private class ClickListener implements ActionListener {

        public void actionPerformed (ActionEvent e) {

            if (e.getSource() == tombol) {

                    JOptionPane.showMessageDialog(null, "You click me again, guys !!!");

            } else if (e.getSource() == btnExit){
   
                JOptionPane.showMessageDialog(null, "See you, guys !");

                System.exit(0);

           }

        }

    }
   
}

0 comments: