Contoh program berikut ini pada dasarnya sama dengan contoh program sebelumnya. Hanya saja pada contoh kali ini, ditambahkan tombol Exit yang jika diklik program akan keluar.
Berikut ini source codingnya :
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 komentar:
Posting Komentar