Event Handling merupakan konsep penanganan suatu action yang terjadi.
 Jadi suatu program akan berjalan saat sesuatu terjadi, misalnya saat 
tombol diklik, saat combo box dipilih dan sebagainya. Java memiliki 
beberapa jenis Event Handling, salah satunya adalah class ActionListener
 yang menangani aksi terhadap tombol. Berikut ini contoh programnya:
Tampilan:
 
Program:
| 02 | importjava.awt.event.*; | 
 
| 05 | publicclassClickMe extendsJFrame implementsActionListener { | 
 
| 06 |     privateJButton tombol; | 
 
| 09 |         super("Event Handling");    | 
 
| 11 |         Container container = getContentPane(); | 
 
| 12 |         container.setLayout(newFlowLayout());       | 
 
| 14 |         tombol = newJButton ("Click Me!"); | 
 
| 15 |         tombol.addActionListener(this); | 
 
| 16 |         container.add(tombol);       | 
 
| 22 |     publicstaticvoidmain (String arg[]) { | 
 
| 23 |         ClickMe test = newClickMe(); | 
 
| 24 |         test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 
 
| 27 |     publicvoidactionPerformed (ActionEvent e) { | 
 
| 28 |         if(e.getSource() == tombol) { | 
 
| 29 |             JOptionPane.showMessageDialog(null, "You click me, guys !!!"); | 
 
 
 
Selamat mencoba
Tidak ada komentar:
Posting Komentar