1 36 37 40 41 42 import javax.swing.*; 43 import javax.swing.event.*; 44 import javax.swing.text.*; 45 import javax.swing.border.*; 46 import javax.swing.colorchooser.*; 47 import javax.swing.filechooser.*; 48 import javax.accessibility.*; 49 50 import java.awt.*; 51 import java.awt.event.*; 52 import java.beans.*; 53 import java.util.*; 54 import java.io.*; 55 import java.applet.*; 56 import java.net.*; 57 58 64 public class OptionPaneDemo extends DemoModule { 65 66 69 public static void main(String [] args) { 70 OptionPaneDemo demo = new OptionPaneDemo(null); 71 demo.mainImpl(); 72 } 73 74 77 public OptionPaneDemo(SwingSet2 swingset) { 78 super(swingset, "OptionPaneDemo", "toolbar/JOptionPane.gif"); 81 82 JPanel demo = getDemoPanel(); 83 84 demo.setLayout(new BoxLayout(demo, BoxLayout.X_AXIS)); 85 86 JPanel bp = new JPanel() { 87 public Dimension getMaximumSize() { 88 return new Dimension(getPreferredSize().width, super.getMaximumSize().height); 89 } 90 }; 91 bp.setLayout(new BoxLayout(bp, BoxLayout.Y_AXIS)); 92 93 bp.add(Box.createRigidArea(VGAP30)); 94 bp.add(Box.createRigidArea(VGAP30)); 95 96 bp.add(createInputDialogButton()); bp.add(Box.createRigidArea(VGAP15)); 97 bp.add(createWarningDialogButton()); bp.add(Box.createRigidArea(VGAP15)); 98 bp.add(createMessageDialogButton()); bp.add(Box.createRigidArea(VGAP15)); 99 bp.add(createComponentDialogButton()); bp.add(Box.createRigidArea(VGAP15)); 100 bp.add(createConfirmDialogButton()); bp.add(Box.createVerticalGlue()); 101 102 demo.add(Box.createHorizontalGlue()); 103 demo.add(bp); 104 demo.add(Box.createHorizontalGlue()); 105 } 106 107 public JButton createWarningDialogButton() { 108 Action a = new AbstractAction(getString("OptionPaneDemo.warningbutton")) { 109 public void actionPerformed(ActionEvent e) { 110 JOptionPane.showMessageDialog( 111 getDemoPanel(), 112 getString("OptionPaneDemo.warningtext"), 113 getString("OptionPaneDemo.warningtitle"), 114 JOptionPane.WARNING_MESSAGE 115 ); 116 } 117 }; 118 return createButton(a); 119 } 120 121 public JButton createMessageDialogButton() { 122 Action a = new AbstractAction(getString("OptionPaneDemo.messagebutton")) { 123 URL img = getClass().getResource("/resources/images/optionpane/bottle.gif"); 124 String imagesrc = "<img SRC=\"" + img + "\" width=\"284\" height=\"100\">"; 125 String message = getString("OptionPaneDemo.messagetext"); 126 public void actionPerformed(ActionEvent e) { 127 JOptionPane.showMessageDialog( 128 getDemoPanel(), 129 "<html>" + imagesrc + "<br><center>" + message + "</center><br></html>" 130 ); 131 } 132 }; 133 return createButton(a); 134 } 135 136 public JButton createConfirmDialogButton() { 137 Action a = new AbstractAction(getString("OptionPaneDemo.confirmbutton")) { 138 public void actionPerformed(ActionEvent e) { 139 int result = JOptionPane.showConfirmDialog(getDemoPanel(), getString("OptionPaneDemo.confirmquestion")); 140 if(result == JOptionPane.YES_OPTION) { 141 JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmyes")); 142 } else if(result == JOptionPane.NO_OPTION) { 143 JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmno")); 144 } 145 } 146 }; 147 return createButton(a); 148 } 149 150 public JButton createInputDialogButton() { 151 Action a = new AbstractAction(getString("OptionPaneDemo.inputbutton")) { 152 public void actionPerformed(ActionEvent e) { 153 String result = JOptionPane.showInputDialog(getDemoPanel(), getString("OptionPaneDemo.inputquestion")); 154 if ((result != null) && (result.length() > 0)) { 155 JOptionPane.showMessageDialog(getDemoPanel(), 156 result + ": " + 157 getString("OptionPaneDemo.inputresponse")); 158 } 159 } 160 }; 161 return createButton(a); 162 } 163 164 public JButton createComponentDialogButton() { 165 Action a = new AbstractAction(getString("OptionPaneDemo.componentbutton")) { 166 public void actionPerformed(ActionEvent e) { 167 170 Object [] message = new Object [4]; 172 message[0] = getString("OptionPaneDemo.componentmessage"); 173 message[1] = new JTextField(getString("OptionPaneDemo.componenttextfield")); 174 175 JComboBox cb = new JComboBox(); 176 cb.addItem(getString("OptionPaneDemo.component_cb1")); 177 cb.addItem(getString("OptionPaneDemo.component_cb2")); 178 cb.addItem(getString("OptionPaneDemo.component_cb3")); 179 message[2] = cb; 180 181 message[3] = getString("OptionPaneDemo.componentmessage2"); 182 183 String [] options = { 185 getString("OptionPaneDemo.component_op1"), 186 getString("OptionPaneDemo.component_op2"), 187 getString("OptionPaneDemo.component_op3"), 188 getString("OptionPaneDemo.component_op4"), 189 getString("OptionPaneDemo.component_op5") 190 }; 191 int result = JOptionPane.showOptionDialog( 192 getDemoPanel(), message, getString("OptionPaneDemo.componenttitle"), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[3] ); 201 switch(result) { 202 case 0: JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r1")); 204 break; 205 case 1: JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r2")); 207 break; 208 case 2: JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r3")); 210 break; 211 case 3: JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r4")); 213 break; 214 default: 215 break; 216 } 217 218 } 219 }; 220 return createButton(a); 221 } 222 223 public JButton createButton(Action a) { 224 JButton b = new JButton() { 225 public Dimension getMaximumSize() { 226 int width = Short.MAX_VALUE; 227 int height = super.getMaximumSize().height; 228 return new Dimension(width, height); 229 } 230 }; 231 b.putClientProperty("displayActionText", Boolean.TRUE); 235 b.setAction(a); 236 return b; 238 } 239 240 } 241 | Popular Tags |