Minggu, 04 Desember 2011

Contoh Program Event Handling di Java

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 tampilannya:
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

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by I Gede Putra - Premium Blogger Themes | Bluehost