JOptionPane可以轻松弹出一个标准对话框,提示用户输入值或通知他们某些内容。
有关使用JOptionPane信息,请参阅“Java教程”中的 How to Make Dialogs部分。
虽然JOptionPane类可能因为大量方法而显得复杂,但此类的几乎所有用法都是对以下所示的静态showXxxDialog方法之一的单行调用:
Common JOptionPane method names and their descriptions Method Name Description showConfirmDialog Asks a confirming question, like yes/no/cancel. showInputDialog Prompt for some input. showMessageDialog Tell the user about something that has happened. showOptionDialog The Grand Unification of the above three.
这些方法中的每一种showInternalXXX一个showInternalXXX风格,它使用内部框架来保存对话框(参见JInternalFrame )。
还定义了多种便捷方法 - 使用不同参数列表的基本方法的重载版本。
所有对话都是模态的。 每个showXxxDialog方法都会阻止调用方,直到用户的交互完成。
Common dialog icon message input value option buttons
其中一个对话框的基本外观与上图类似,尽管各种外观最终都是最终结果的原因。
特别是,外观将调整布局以适应选项窗格的ComponentOrientation属性。
参数: 这些方法的参数遵循一致的模式:
parentComponent
Defines the
Component that is to be the parent of this dialog box. It is used in two ways: the
Frame that contains it is used as the
Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be
null, in which case a default
Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).
message
A descriptive message to be placed in the dialog box. In the most common usage, message is just a
String or
String constant. However, the type of this parameter is actually
Object. Its interpretation depends on its type:
Object[]
An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack. The interpretation is recursive -- each object in the array is interpreted according to its type.
Component
The
Component is displayed in the dialog.
Icon
The
Icon is wrapped in a
JLabel and displayed in the dialog.
others
The object is converted to a
String by calling its
toString method. The result is wrapped in a
JLabel and displayed.
messageType
Defines the style of the message. The Look and Feel manager may lay out the dialog differently depending on this value, and will often provide a default icon. The possible values are:
ERROR_MESSAGE
INFORMATION_MESSAGE
WARNING_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE
optionType
Defines the set of option buttons that appear at the bottom of the dialog box:
DEFAULT_OPTION
YES_NO_OPTION
YES_NO_CANCEL_OPTION
OK_CANCEL_OPTION
You aren't limited to this set of option buttons. You can provide any buttons you want using the options parameter.
options
A more detailed description of the set of option buttons that will appear at the bottom of the dialog box. The usual value for the options parameter is an array of
Strings. But the parameter type is an array of
Objects. A button is created for each object depending on its type:
Component
The component is added to the button row directly.
Icon
A
JButton is created with this as its label.
other
The
Object is converted to a string using its
toString method and the result is used to label a
JButton.
icon
A decorative icon to be placed in the dialog box. A default value for this is determined by the
messageType parameter.
title
The title for the dialog box.
initialValue
The default selection (input value).
更改选择后,将调用setValue ,生成PropertyChangeEvent 。
如果JOptionPane已配置为所有输入setWantsInput ,则还可以侦听绑定属性JOptionPane.INPUT_VALUE_PROPERTY ,以确定用户何时输入或选择了值。
当其中一个showXxxDialog方法返回一个整数时,可能的值为:
YES_OPTION
NO_OPTION
CANCEL_OPTION
OK_OPTION
CLOSED_OPTION
例子:
显示错误对话框,显示消息“alert”:
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
显示内容信息对话框,其中包含“信息”消息:
JOptionPane.showInternalMessageDialog(frame, "information",
"information", JOptionPane.INFORMATION_MESSAGE);
显示一个信息面板,其中包含选项yes / no和消息'choose one':
JOptionPane.showConfirmDialog(null,
"choose one", "choose one", JOptionPane.YES_NO_OPTION);
显示内部信息对话框,其中包含选项yes / no / cancel和消息'please choose one'和标题信息:
JOptionPane.showInternalConfirmDialog(frame,
"please choose one", "information",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
显示一个警告对话框,其中包含选项OK,CANCEL,标题'Warning'和消息'单击OK继续':
Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
显示一个对话框,要求用户输入字符串:
String inputValue = JOptionPane.showInputDialog("Please input a value");
显示一个对话框,要求用户选择一个字符串:
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
直接使用:
要直接创建和使用JOptionPane ,标准模式大致如下:
JOptionPane pane = new JOptionPane(arguments);
pane.set.Xxxx(...); // Configure
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.show();
Object selectedValue = pane.getValue();
if(selectedValue == null)
return CLOSED_OPTION;
//If there is not an array of option buttons:
if(options == null) {
if(selectedValue instanceof Integer)
return ((Integer)selectedValue).intValue();
return CLOSED_OPTION;
}
//If there is an array of option buttons:
for(int counter = 0, maxCounter = options.length;
counter < maxCounter; counter++) {
if(options[counter].equals(selectedValue))
return counter;
}
return CLOSED_OPTION;
警告: Swing不是线程安全的。 有关更多信息,请参阅Swing's Threading Policy 。
警告:此类的序列化对象与以后的Swing版本不兼容。 当前的序列化支持适用于运行相同版本Swing的应用程序之间的短期存储或RMI。 从1.4开始, java.beans软件包中添加了对所有JavaBeans java.beans长期存储的支持。 请参阅XMLEncoder 。