Selamat Datang Di Website Informasi & Teknologi Komputer

Saturday, November 17, 2018

Contoh Program Event Handling Pada Java

01import java.awt.*;
02
03import java.awt.event.*;
04
05import javax.swing.*;
06
07
08
09public class ClickMe3 extends JFrame {
10
11    private JButton tombol, btnExit;
12
13    
14
15    public ClickMe3() {
16
17        super ("Event Handling");
18
19        
20
21        Container container = getContentPane();
22
23        container.setLayout(new FlowLayout());
24
25        ClickListener cl = new ClickListener ();
26
27        
28
29        tombol = new JButton ("Click Me!");
30
31        tombol.addActionListener(cl);
32
33        container.add(tombol);
34
35        
36
37        btnExit = new JButton ("Exit");
38
39        btnExit.addActionListener(cl);
40
41        container.add(btnExit);
42
43        
44
45        setSize (200,100);
46
47        setVisible (true);
48
49    }
50
51    
52
53    public static void main (String arg[]) {
54
55        ClickMe3 test = new ClickMe3();
56
57        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
58
59    }
60
61    
62
63    //inner class
64
65    private class ClickListener implements ActionListener {
66
67        public void actionPerformed (ActionEvent e) {
68
69            if (e.getSource() == tombol) {
70
71                JOptionPane.showMessageDialog(null"You click me again, guys !!!");
72
73            else if (e.getSource() == btnExit){
74
75                if ( JOptionPane.showConfirmDialog(null"Apakah Anda yakin akan keluar ?","Konfirmasi",
76
77                        JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
78
79                        System.exit(0);
80
81                    }
82
83            }
84
85        }
86
87    }
88
89}
Semoga Bermanfaat....

0 komentar:

Post a Comment