Posted at November 5, 2010
Pada contoh berikut ini ditambahkan window konfirmasi saat tombol exit ditekan.
Berikut ini tampilannya:
Contoh program:
03 | import java.awt.event.*; |
07 | public class ClickMe3 extends JFrame { |
09 | private JButton tombol, btnExit; |
13 | super ( "Event Handling" ); |
15 | Container container = getContentPane(); |
17 | container.setLayout( new FlowLayout()); |
19 | ClickListener cl = new ClickListener (); |
21 | tombol = new JButton ( "Click Me!" ); |
23 | tombol.addActionListener(cl); |
25 | container.add(tombol); |
27 | btnExit = new JButton ( "Exit" ); |
29 | btnExit.addActionListener(cl); |
31 | container.add(btnExit); |
39 | public static void main (String arg[]) { |
41 | ClickMe3 test = new ClickMe3(); |
43 | test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
49 | private class ClickListener implements ActionListener { |
51 | public void actionPerformed (ActionEvent e) { |
53 | if (e.getSource() == tombol) { |
55 | JOptionPane.showMessageDialog( null , "You click me again, guys !!!" ); |
57 | } else if (e.getSource() == btnExit){ |
59 | if ( JOptionPane.showConfirmDialog( null , "Apakah Anda yakin akan keluar ?" , "Konfirmasi" , |
61 | JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { |
Semoga bermanfaat
Tidak ada komentar:
Posting Komentar