Pengikut

Sabtu, 29 Juni 2013

Contoh Program Message Dialog di Java

Contoh program berikut ini menampilkan jenis-jenis window pesan di Java. Window pesan (message dialog) antara lain bertipe warning message, information message, confirmation message, dan juga input message. Class yang digunakan adalah class JOptionPane.
Berikut ini tampilannya:
contoh-program-message-dialog-java

Berikut ini contoh programnya:
001import java.awt.*;
002 
003import java.awt.event.*;
004 
005import javax.swing.*;
006 
007 
008 
009public class MessageDialog extends JFrame {
010 
011    private JButton tombol, btn2, btn3, btn4, btn5;
012 
013     
014 
015    public MessageDialog() {
016 
017        super ("Event Handling");
018 
019         
020 
021        Container container = getContentPane();
022 
023        container.setLayout(new FlowLayout());
024 
025                 
026 
027        tombol = new JButton ("Message Dialog");
028 
029        tombol.addActionListener(
030 
031            new ActionListener() {
032 
033                public void actionPerformed (ActionEvent e) {
034 
035                    JOptionPane.showMessageDialog (null,"Contoh Message Dialog");
036 
037                }
038 
039            }
040 
041        );
042 
043        container.add(tombol);
044 
045         
046 
047        btn2 = new JButton ("Warning Message");
048 
049        btn2.addActionListener(
050 
051            new ActionListener() {
052 
053                public void actionPerformed (ActionEvent e) {
054 
055                    JOptionPane.showConfirmDialog(null, "Contoh Warning Message","Peringatan",
056 
057                        JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
058 
059                }
060 
061            }
062 
063        );
064 
065        container.add(btn2);
066 
067         
068 
069        btn3 = new JButton ("Question Message");
070 
071        btn3.addActionListener(
072 
073            new ActionListener() {
074 
075                public void actionPerformed (ActionEvent e) {
076 
077                    JOptionPane.showConfirmDialog(null, "Contoh Question Message","Pertanyaan",
078 
079                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
080 
081                }
082 
083            }
084 
085        );
086 
087        container.add(btn3);
088 
089         
090 
091        btn4 = new JButton ("Information Message");
092 
093        btn4.addActionListener(
094 
095            new ActionListener() {
096 
097                public void actionPerformed (ActionEvent e) {
098 
099                    JOptionPane.showConfirmDialog(null, "Contoh Information Message","Informasi",
100 
101                        JOptionPane.NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
102 
103                }
104 
105            }
106 
107        );
108 
109        container.add(btn4);
110 
111         
112 
113        btn5 = new JButton ("Input Dialog");
114 
115        btn5.addActionListener(
116 
117            new ActionListener() {
118 
119                public void actionPerformed (ActionEvent e) {
120 
121                    String a = JOptionPane.showInputDialog("Input Nama : ");
122 
123                    JOptionPane.showMessageDialog(null, a);
124 
125                }
126 
127            }
128 
129        );
130 
131        container.add(btn5);
132 
133         
134 
135        setSize (200,300);
136 
137        setLocationRelativeTo(null);
138 
139        setVisible (true);
140 
141    }
142 
143     
144 
145    public static void main (String arg[]) {
146 
147        MessageDialog test = new MessageDialog();
148 
149        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
150 
151    }
152 
153     
154 
155}
Semoga bermanfaat dan maju terus ilmu pengetahuan Indonesia

Tidak ada komentar:

Posting Komentar