📄 s14.htm
字号:
selectionValues, // selectionValues<br> selectionValues[3]); // initialValue</p> <p> if(s == null)<br> showStatus("cancel button activated");<br> else<br> showStatus(s);<br> }<br> });<br> }<br> }</p> <hr size="1" noshade> <p> </p> <p> 14.3.6 选项对话框</p> <p> </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("show dialog ...");<br> private JButton applyButton = new JButton("Apply");<br> private RotatePanel rotatePanel = new RotatePanel();<br> private String title = "Rotate";<br> private Object[] buttonRowObjects = new Object[] {<br> "Ok", <br> applyButton,<br> "Cancel",<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> " degrees");<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> "Dialog closed with close box");<br> break;<br> case JOptionPane.OK_OPTION:<br> showStatus("Ok button activated: " +<br> rotatePanel.getSelectedAngle() +<br> " degrees");<br> break;<br> case JOptionPane.CANCEL_OPTION:<br> showStatus("Cancel button activated");<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("0"),<br> new JRadioButton("90"),<br> new JRadioButton("180"),<br> new JRadioButton("270"),<br> };<br> public RotatePanel() {<br> setBorder(BorderFactory.createTitledBorder("Angle:"));</p> <p> for(int i=0; i < 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 < 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> </p> <p> 14.3.8 JOptionPane事件</p> <p> </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("show dialog ...");</p> <p> private String title = "Update References";<br> private JPanel messagePanel = new JPanel();<br> private JCheckBox[] checkBoxes = {<br> new JCheckBox("All Cross-References"),<br> new JCheckBox("Text Insets Marked for Manual Update"),<br> new JCheckBox("Text Insets Marked for Automatic Update"),<br> new JCheckBox("OLE Links Marked for Manual Update"),<br> new JCheckBox("OLE Links Marked for Automatic Update"),<br> };</p> <p> public Test() {<br> Container contentPane = getContentPane();</p> <p> messagePanel.setBorder(<br> BorderFactory.createTitledBorder("Update:"));</p> <p> messagePanel.setLayout(new BoxLayout(messagePanel, <br> BoxLayout.Y_AXIS));</p> <p> for(int i=0; i < 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("dialog canceled");<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 + ":" + e.getNewValue());<br> }<br> });<br> }<br> private void updateReferences() {<br> showStatus("updating references");<br> }<br> }</p> <hr size="1" noshade> <p> 14.3.9 JOptionPane类总结</p> <p> </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("JOptionPane(Object message)",<br> new ImageIcon("beach_umbrella.gif"),<br> JLabel.LEFT),<br> new JCheckBox("check me out"),<br> new VerboseObject(),<br> };<br> Object[] objects2 = new Object[] {<br> "JOptionPane(Object message, int messageType)",<br> "messageType = JOptionPane.INFORMATION_MESSAGE",<br> };<br> Object[] objects3 = new Object[] {<br> "JOptionPane(Object message, " +<br> "int messageType, int optionsType)",<br> "messageType = JOptionPane.QUESTION_MESSAGE",<br> "optionType = JOptionPane.YES_NO_CANCEL_OPTION",<br> };<br> Object[] objects4 = new Object[] {<br> "JOptionPane(Object message, " +<br> "int messageType, int optionsType, Icon icon)",<br> "messageType = JOptionPane.WARNING_MESSAGE",<br> "optionType = JOptionPane.OK_CANCEL_OPTION",<br> "icon = new ImageIcon(\"ballot_box.gif\")",<br> };<br> Object[] objects5 = new Object[] {<br> "JOptionPane(Object message, " +<br> "int messageType, int optionsType, Icon icon)",<br> "messageType = JOptionPane.ERROR_MESSAGE",<br> "optionType = JOptionPane.OK_CANCEL_OPTION",<br> "icon = null",<br> "options = \"button 1\", \"button 2\", " +<br> "new JButton(\"button 3\")",<br> };</p> <p> JOptionPane defaultPane = new JOptionPane();</p> <p> JOptionPane messagePane = new JOptionPane(<br> "JOptionPane(Object message)");</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("ballot_box.gif"));</p> <p> Object[] options = {<br> "button 1", "button 2", new JButton("button 3"),<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 "This is what you'll see in the option pane";<br> }<br> }</p> <hr size="1" noshade> <p> 14.3.10 AWT兼容</p> <p> </p> <p> 14.4 本章回顾</p> <p> </p> <p> [<a href="index.html" target="_self">目录</a>][<a href="s13.htm">上一页</a>][<a href="s15.htm">下一页</a>](飒龙收藏/2002.5.18) </p> <p> </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 + -