The following looks:
 
 Examples of programs:
| 01 | importjava.awt.*; | 
| 02 | 
| 03 | importjava.awt.event.*; | 
| 04 | 
| 05 | importjavax.swing.*; | 
| 06 | 
| 07 | publicclassClickMe3 extendsJFrame { | 
| 08 | 
| 09 |     privateJButton tombol, btnExit; | 
| 10 | 
| 11 |     publicClickMe3() { | 
| 12 | 
| 13 |         super("Event Handling"); | 
| 14 | 
| 15 |         Container container = getContentPane(); | 
| 16 | 
| 17 |         container.setLayout(newFlowLayout()); | 
| 18 | 
| 19 |         ClickListener cl = newClickListener (); | 
| 20 | 
| 21 |         tombol = newJButton ("Click Me!"); | 
| 22 | 
| 23 |         tombol.addActionListener(cl); | 
| 24 | 
| 25 |         container.add(tombol); | 
| 26 | 
| 27 |         btnExit = newJButton ("Exit"); | 
| 28 | 
| 29 |         btnExit.addActionListener(cl); | 
| 30 | 
| 31 |         container.add(btnExit); | 
| 32 | 
| 33 |         setSize (200,100); | 
| 34 | 
| 35 |         setVisible (true); | 
| 36 | 
| 37 |     } | 
| 38 | 
| 39 |     publicstaticvoidmain (String arg[]) { | 
| 40 | 
| 41 |         ClickMe3 test = newClickMe3(); | 
| 42 | 
| 43 |         test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 
| 44 | 
| 45 |     } | 
| 46 | 
| 47 |     //inner class | 
| 48 | 
| 49 |     privateclassClickListener implementsActionListener { | 
| 50 | 
| 51 |         publicvoidactionPerformed (ActionEvent e) { | 
| 52 | 
| 53 |             if(e.getSource() == tombol) { | 
| 54 | 
| 55 |                 JOptionPane.showMessageDialog(null, "You click me again, guys !!!"); | 
| 56 | 
| 57 |             } elseif(e.getSource() == btnExit){ | 
| 58 | 
| 59 |                 if( JOptionPane.showConfirmDialog(null, "Apakah Anda yakin akan keluar ?","Konfirmasi", | 
| 60 | 
| 61 |                         JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { | 
| 62 | 
| 63 |                         System.exit(0); | 
| 64 | 
| 65 |                     } | 
| 66 | 
| 67 |             } | 
| 68 | 
| 69 |         } | 
| 70 | 
| 71 |     } | 
| 72 | 
| 73 | } | 

 
 
  
0 komentar: