Examples The following program displays a message window types in Java. Window messages (message dialog), among others, a warning message type, message information, confirmation message, and also the input message. Class that is used is the JOptionPane class.
The following looks:
001 | import java.awt.*; |
002 |
003 | import java.awt.event.*; |
004 |
005 | import javax.swing.*; |
006 |
007 | public class MessageDialog extends JFrame { |
008 |
009 | private JButton tombol, btn2, btn3, btn4, btn5; |
010 |
011 | public MessageDialog() { |
012 |
013 | super ( "Event Handling" ); |
014 |
015 | Container container = getContentPane(); |
016 |
017 | container.setLayout( new FlowLayout()); |
018 |
019 | tombol = new JButton ( "Message Dialog" ); |
020 |
021 | tombol.addActionListener( |
022 |
023 | new ActionListener() { |
024 |
025 | public void actionPerformed (ActionEvent e) { |
026 |
027 | JOptionPane.showMessageDialog ( null , "Contoh Message Dialog" ); |
028 |
029 | } |
030 |
031 | } |
032 |
033 | ); |
034 |
035 | container.add(tombol); |
036 |
037 | btn2 = new JButton ( "Warning Message" ); |
038 |
039 | btn2.addActionListener( |
040 |
041 | new ActionListener() { |
042 |
043 | public void actionPerformed (ActionEvent e) { |
044 |
045 | JOptionPane.showConfirmDialog( null , "Contoh Warning Message" , "Peringatan" , |
046 |
047 | JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE); |
048 |
049 | } |
050 |
051 | } |
052 |
053 | ); |
054 |
055 | container.add(btn2); |
056 |
057 | btn3 = new JButton ( "Question Message" ); |
058 |
059 | btn3.addActionListener( |
060 |
061 | new ActionListener() { |
062 |
063 | public void actionPerformed (ActionEvent e) { |
064 |
065 | JOptionPane.showConfirmDialog( null , "Contoh Question Message" , "Pertanyaan" , |
066 |
067 | JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); |
068 |
069 | } |
070 |
071 | } |
072 |
073 | ); |
074 |
075 | container.add(btn3); |
076 |
077 | btn4 = new JButton ( "Information Message" ); |
078 |
079 | btn4.addActionListener( |
080 |
081 | new ActionListener() { |
082 |
083 | public void actionPerformed (ActionEvent e) { |
084 |
085 | JOptionPane.showConfirmDialog( null , "Contoh Information Message" , "Informasi" , |
086 |
087 | JOptionPane.NO_OPTION, JOptionPane.INFORMATION_MESSAGE); |
088 |
089 | } |
090 |
091 | } |
092 |
093 | ); |
094 |
095 | container.add(btn4); |
096 |
097 | btn5 = new JButton ( "Input Dialog" ); |
098 |
099 | btn5.addActionListener( |
100 |
101 | new ActionListener() { |
102 |
103 | public void actionPerformed (ActionEvent e) { |
104 |
105 | String a = JOptionPane.showInputDialog( "Input Nama : " ); |
106 |
107 | JOptionPane.showMessageDialog( null , a); |
108 |
109 | } |
110 |
111 | } |
112 |
113 | ); |
114 |
115 | container.add(btn5); |
116 |
117 | setSize ( 200 , 300 ); |
118 |
119 | setLocationRelativeTo( null ); |
120 |
121 | setVisible ( true ); |
122 |
123 | } |
124 |
125 | public static void main (String arg[]) { |
126 |
127 | MessageDialog test = new MessageDialog(); |
128 |
129 | test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
130 |
131 | } |
132 |
133 | } |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
0 komentar: