⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s14.htm

📁 提供给JAVA编程者图形界面卷
💻 HTM
📖 第 1 页 / 共 3 页
字号:
              selectionValues, // selectionValues<br>              selectionValues[3]); // initialValue</p>            <p> if(s == null)<br>              showStatus(&quot;cancel button activated&quot;);<br>              else<br>              showStatus(s);<br>              }<br>              });<br>              }<br>              }</p>            <hr size="1" noshade>            <p>&nbsp;</p>            <p> 14.3.6 选项对话框</p>            <p>&nbsp;</p>            <p align="center"><b>例14-12 使用一个选项对话框</b></p>            <hr noshade size="1">            import java.awt.*;<br>            import java.awt.event.*;<br>            import javax.swing.*;            <p>public class Test extends JApplet {<br>              private JButton button = new JButton(&quot;show dialog ...&quot;);<br>              private JButton applyButton = new JButton(&quot;Apply&quot;);<br>              private RotatePanel rotatePanel = new RotatePanel();<br>              private String title = &quot;Rotate&quot;;<br>              private Object[] buttonRowObjects = new Object[] {<br>              &quot;Ok&quot;, <br>              applyButton,<br>              &quot;Cancel&quot;,<br>              };</p>            <p> public Test() {<br>              Container contentPane = getContentPane();</p>            <p> contentPane.setLayout(new FlowLayout());<br>              contentPane.add(button);</p>            <p> applyButton.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              showStatus(rotatePanel.getSelectedAngle() + <br>              &quot; degrees&quot;);<br>              }<br>              });<br>              button.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              int value = JOptionPane.showOptionDialog(<br>              button, // parentComponent<br>              rotatePanel, // message<br>              title, // title<br>              JOptionPane.DEFAULT_OPTION, // optionType<br>              JOptionPane.PLAIN_MESSAGE, // messageType<br>              null, // icon<br>              buttonRowObjects, // options<br>              applyButton); // initialValue </p>            <p> switch(value) {<br>              case JOptionPane.CLOSED_OPTION:<br>              showStatus(<br>              &quot;Dialog closed with close box&quot;);<br>              break;<br>              case JOptionPane.OK_OPTION:<br>              showStatus(&quot;Ok button activated: &quot; +<br>              rotatePanel.getSelectedAngle() +<br>              &quot; degrees&quot;);<br>              break;<br>              case JOptionPane.CANCEL_OPTION:<br>              showStatus(&quot;Cancel button activated&quot;);<br>              break;<br>              }<br>              }<br>              });<br>              }<br>              }<br>              class RotatePanel extends JPanel { <br>              private ButtonGroup group = new ButtonGroup();</p>            <p> private JRadioButton[] buttons = {<br>              new JRadioButton(&quot;0&quot;),<br>              new JRadioButton(&quot;90&quot;),<br>              new JRadioButton(&quot;180&quot;),<br>              new JRadioButton(&quot;270&quot;),<br>              };<br>              public RotatePanel() {<br>              setBorder(BorderFactory.createTitledBorder(&quot;Angle:&quot;));</p>            <p> for(int i=0; i &lt; buttons.length; ++i) {<br>              if(i ==0)<br>              buttons[i].setSelected(true);</p>            <p> add(buttons[i]);<br>              group.add(buttons[i]);<br>              }<br>              }<br>              public String getSelectedAngle() {<br>              String rv = null; // rv = return value</p>            <p> for(int i=0; i &lt; buttons.length; ++i) {<br>              if(buttons[i].isSelected())<br>              rv = buttons[i].getText();<br>              }<br>              return rv;<br>              }<br>              }</p>            <hr size="1" noshade>            <p> 14.3.7 JOptionPane属性</p>            <p>&nbsp;</p>            <p> 14.3.8 JOptionPane事件</p>            <p>&nbsp;</p>            <p align="center"><b>例14-13 监听从选项窗格激发的 PropertyChangeEvents</b></p>            <hr noshade size="1">            import java.awt.*;<br>            import java.awt.event.*;<br>            import java.beans.*;<br>            import javax.swing.*;            <p>public class Test extends JApplet {<br>              private JButton button = new JButton(&quot;show dialog ...&quot;);</p>            <p> private String title = &quot;Update References&quot;;<br>              private JPanel messagePanel = new JPanel();<br>              private JCheckBox[] checkBoxes = {<br>              new JCheckBox(&quot;All Cross-References&quot;),<br>              new JCheckBox(&quot;Text Insets Marked for Manual Update&quot;),<br>              new JCheckBox(&quot;Text Insets Marked for Automatic Update&quot;),<br>              new JCheckBox(&quot;OLE Links Marked for Manual Update&quot;),<br>              new JCheckBox(&quot;OLE Links Marked for Automatic Update&quot;),<br>              };</p>            <p> public Test() {<br>              Container contentPane = getContentPane();</p>            <p> messagePanel.setBorder(<br>              BorderFactory.createTitledBorder(&quot;Update:&quot;));</p>            <p> messagePanel.setLayout(new BoxLayout(messagePanel, <br>              BoxLayout.Y_AXIS));</p>            <p> for(int i=0; i &lt; checkBoxes.length; ++i)<br>              messagePanel.add(checkBoxes[i]);</p>            <p> final JOptionPane pane = new JOptionPane(<br>              messagePanel, // message<br>              JOptionPane.QUESTION_MESSAGE, // messageType<br>              JOptionPane.OK_CANCEL_OPTION); // optionType</p>            <p> contentPane.setLayout(new FlowLayout());<br>              contentPane.add(button);</p>            <p> button.addActionListener(new ActionListener() {<br>              public void actionPerformed(ActionEvent e) {<br>              JDialog dialog = pane.createDialog(<br>              Test.this, // parentComponent<br>              title); // title</p>            <p> dialog.show(); // blocks</p>            <p> Integer value = (Integer)pane.getValue();</p>            <p> if(value.intValue() == JOptionPane.OK_OPTION)<br>              updateReferences();<br>              else<br>              showStatus(&quot;dialog canceled&quot;);<br>              }<br>              });<br>              pane.addPropertyChangeListener(<br>              new PropertyChangeListener() {<br>              public void propertyChange(PropertyChangeEvent e) {<br>              String name = e.getPropertyName();</p>            <p> if(name.equals(JOptionPane.VALUE_PROPERTY))<br>              System.out.println(name + &quot;:&quot; + e.getNewValue());<br>              }<br>              });<br>              }<br>              private void updateReferences() {<br>              showStatus(&quot;updating references&quot;);<br>              }<br>              }</p>            <hr size="1" noshade>            <p> 14.3.9 JOptionPane类总结</p>            <p>&nbsp;</p>            <p align="center"><b>例14-14 构造各种配置的选项窗格</b></p>            <hr noshade size="1">            import java.awt.*;<br>            import java.awt.event.*;<br>            import javax.swing.*;            <p>public class Test extends JApplet {<br>              public Test() {<br>              Container contentPane = getContentPane();<br>              Object[] objects = new Object[] {<br>              new JLabel(&quot;JOptionPane(Object message)&quot;,<br>              new ImageIcon(&quot;beach_umbrella.gif&quot;),<br>              JLabel.LEFT),<br>              new JCheckBox(&quot;check me out&quot;),<br>              new VerboseObject(),<br>              };<br>              Object[] objects2 = new Object[] {<br>              &quot;JOptionPane(Object message, int messageType)&quot;,<br>              &quot;messageType = JOptionPane.INFORMATION_MESSAGE&quot;,<br>              };<br>              Object[] objects3 = new Object[] {<br>              &quot;JOptionPane(Object message, &quot; +<br>              &quot;int messageType, int optionsType)&quot;,<br>              &quot;messageType = JOptionPane.QUESTION_MESSAGE&quot;,<br>              &quot;optionType = JOptionPane.YES_NO_CANCEL_OPTION&quot;,<br>              };<br>              Object[] objects4 = new Object[] {<br>              &quot;JOptionPane(Object message, &quot; +<br>              &quot;int messageType, int optionsType, Icon icon)&quot;,<br>              &quot;messageType = JOptionPane.WARNING_MESSAGE&quot;,<br>              &quot;optionType = JOptionPane.OK_CANCEL_OPTION&quot;,<br>              &quot;icon = new ImageIcon(\&quot;ballot_box.gif\&quot;)&quot;,<br>              };<br>              Object[] objects5 = new Object[] {<br>              &quot;JOptionPane(Object message, &quot; +<br>              &quot;int messageType, int optionsType, Icon icon)&quot;,<br>              &quot;messageType = JOptionPane.ERROR_MESSAGE&quot;,<br>              &quot;optionType = JOptionPane.OK_CANCEL_OPTION&quot;,<br>              &quot;icon = null&quot;,<br>              &quot;options = \&quot;button 1\&quot;, \&quot;button 2\&quot;,               &quot; +<br>              &quot;new JButton(\&quot;button 3\&quot;)&quot;,<br>              };</p>            <p> JOptionPane defaultPane = new JOptionPane();</p>            <p> JOptionPane messagePane = new JOptionPane(<br>              &quot;JOptionPane(Object message)&quot;);</p>            <p> JOptionPane objectPane = new JOptionPane(objects);</p>            <p> JOptionPane messageTypePane = new JOptionPane(<br>              objects2, JOptionPane.INFORMATION_MESSAGE);</p>            <p> JOptionPane messageAndOptionTypePane = new JOptionPane(<br>              objects3, JOptionPane.QUESTION_MESSAGE,<br>              JOptionPane.YES_NO_CANCEL_OPTION);</p>            <p> JOptionPane messageOptionAndIconPane = new JOptionPane(<br>              objects4, JOptionPane.WARNING_MESSAGE,<br>              JOptionPane.OK_CANCEL_OPTION,<br>              new ImageIcon(&quot;ballot_box.gif&quot;));</p>            <p> Object[] options = {<br>              &quot;button 1&quot;, &quot;button 2&quot;, new JButton(&quot;button               3&quot;),<br>              };<br>              JOptionPane messageOptionIconAndOptionsPane = <br>              new JOptionPane(<br>              objects5, JOptionPane.ERROR_MESSAGE,<br>              JOptionPane.OK_CANCEL_OPTION,<br>              null,<br>              options,<br>              options[2]);</p>            <p> contentPane.setLayout(new BoxLayout(contentPane, <br>              BoxLayout.Y_AXIS));<br>              contentPane.add(defaultPane);<br>              contentPane.add(new JSeparator());<br>              contentPane.add(messagePane);<br>              contentPane.add(new JSeparator());<br>              contentPane.add(objectPane);<br>              contentPane.add(new JSeparator());<br>              contentPane.add(messageTypePane);<br>              contentPane.add(new JSeparator());<br>              contentPane.add(messageAndOptionTypePane);<br>              contentPane.add(new JSeparator());<br>              contentPane.add(messageOptionAndIconPane);<br>              contentPane.add(new JSeparator());<br>              contentPane.add(messageOptionIconAndOptionsPane);<br>              }<br>              }<br>              class VerboseObject extends Object { <br>              public String toString() {<br>              return &quot;This is what you'll see in the option pane&quot;;<br>              }<br>              }</p>            <hr size="1" noshade>            <p> 14.3.10 AWT兼容</p>            <p>&nbsp;</p>            <p> 14.4 本章回顾</p>            <p>&nbsp;</p>            <p> [<a href="index.html" target="_self">目录</a>][<a href="s13.htm">上一页</a>][<a href="s15.htm">下一页</a>](飒龙收藏/2002.5.18)             </p>            <p>&nbsp; </p>            </td>          </tr>        </tbody>      </table>    </td>  </tr></tbody></table><script language="javascript">bottomprint()</script></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -