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 | importjava.awt.*; | 
| 002 | 
| 003 | importjava.awt.event.*; | 
| 004 | 
| 005 | importjavax.swing.*; | 
| 006 | 
| 007 | publicclassMessageDialog extendsJFrame { | 
| 008 | 
| 009 |     privateJButton tombol, btn2, btn3, btn4, btn5; | 
| 010 | 
| 011 |     publicMessageDialog() { | 
| 012 | 
| 013 |         super("Event Handling"); | 
| 014 | 
| 015 |         Container container = getContentPane(); | 
| 016 | 
| 017 |         container.setLayout(newFlowLayout()); | 
| 018 | 
| 019 |         tombol = newJButton ("Message Dialog"); | 
| 020 | 
| 021 |         tombol.addActionListener( | 
| 022 | 
| 023 |             newActionListener() { | 
| 024 | 
| 025 |                 publicvoidactionPerformed (ActionEvent e) { | 
| 026 | 
| 027 |                     JOptionPane.showMessageDialog (null,"Contoh Message Dialog"); | 
| 028 | 
| 029 |                 } | 
| 030 | 
| 031 |             } | 
| 032 | 
| 033 |         ); | 
| 034 | 
| 035 |         container.add(tombol); | 
| 036 | 
| 037 |         btn2 = newJButton ("Warning Message"); | 
| 038 | 
| 039 |         btn2.addActionListener( | 
| 040 | 
| 041 |             newActionListener() { | 
| 042 | 
| 043 |                 publicvoidactionPerformed (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 = newJButton ("Question Message"); | 
| 058 | 
| 059 |         btn3.addActionListener( | 
| 060 | 
| 061 |             newActionListener() { | 
| 062 | 
| 063 |                 publicvoidactionPerformed (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 = newJButton ("Information Message"); | 
| 078 | 
| 079 |         btn4.addActionListener( | 
| 080 | 
| 081 |             newActionListener() { | 
| 082 | 
| 083 |                 publicvoidactionPerformed (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 = newJButton ("Input Dialog"); | 
| 098 | 
| 099 |         btn5.addActionListener( | 
| 100 | 
| 101 |             newActionListener() { | 
| 102 | 
| 103 |                 publicvoidactionPerformed (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 |     publicstaticvoidmain (String arg[]) { | 
| 126 | 
| 127 |         MessageDialog test = newMessageDialog(); | 
| 128 | 
| 129 |         test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 
| 130 | 
| 131 |     } | 
| 132 | 
| 133 | } | 
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   | 

 
 
  
0 komentar: