About Me

Followers

Labels

Wednesday, November 9, 2011

Contoh Program Event Handling di Java

Pada contoh berikut ini ditambahkan window konfirmasi saat tombol exit ditekan.
Berikut ini tampilannya:

import java.awt.event.*;

import javax.swing.*;

public class ClickMe3 extends JFrame {

private JButton tombol, btnExit;

public ClickMe3() {

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[]) {

ClickMe3 test = new ClickMe3();

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){

if ( JOptionPane.showConfirmDialog(null, "Apakah Anda yakin akan keluar ?","Konfirmasi",

JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {

System.exit(0);

}

}

}

}

}

selamat mencoba.

0 comments

Post a Comment