|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JOptionPane
public class JOptionPane
JOptionPane 有助于方便地弹出要求用户提供值或向其发出通知的标准对话框。有关使用 JOptionPane 的信息,请参见 The Java Tutorial 中的 How to Make Dialogs 一节。
虽然由于方法数多使 JOptionPane 类可能显得复杂,但几乎所有此类的使用都是对下列静态 showXxxDialog 方法之一的单行调用:
所有这些方法还可能以
方法名 描述 showConfirmDialog 询问一个确认问题,如 yes/no/cancel。 showInputDialog 提示要求某些输入。 showMessageDialog 告知用户某事已发生。 showOptionDialog 上述三项的大统一 (Grand Unification)。
showInternalXXX 风格出现,该风格使用内部窗体来保存对话框(请参见 JInternalFrame)。此外还定义了多种便捷方法,这些方法重载那些基本方法,使用不同的参数列表。
所有对话框都是有模式的。在用户交互完成之前,每个 showXxxDialog 方法都一直阻塞调用者。
| 图标 | 消息 |
| 输入值 | |
| 选项按钮 | |
ComponentOrientation 属性。
参数:
这些方法的参数遵守一致的模式:
- parentComponent
- 定义作为此对话框的父对话框的
Component。通过两种方式使用此参数:包含它的Frame可以用作对话框的父Frame,在对话框的位置使用其屏幕坐标。一般情况下,将对话框紧靠组件置于其之下。此参数可以为null,在这种情况下,默认的Frame用作父级,并且对话框将居中位于屏幕上(取决于 L&F)。- message
- 要置于对话框中的描述消息。在最常见的应用中,message 就是一个
String或String常量。不过,此参数的类型实际上是Object。其解释依赖于其类型:
- Object[]
- 对象数组被解释为在纵向堆栈中排列的一系列 message(每个对象一个)。解释是递归式的,即根据其类型解释数组中的每个对象。
- Component
- 该
Component在对话框中显示。- Icon
- 该
Icon被包装在JLabel中并在对话框中显示。- 其他
- 该对象通过调用其
toString方法被转换为String。结果被包装在JLabel中显示。- messageType
- 定义 message 的样式。外观管理器根据此值对对话框进行不同地布置,并且通常提供默认图标。可能的值为:
ERROR_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGEPLAIN_MESSAGE- optionType
- 定义在对话框的底部显示的选项按钮的集合:
用户并非仅限于使用此选项按钮集合。使用 options 参数可以提供想使用的任何按钮。
DEFAULT_OPTIONYES_NO_OPTIONYES_NO_CANCEL_OPTIONOK_CANCEL_OPTION- options
- 将在对话框底部显示的选项按钮集合的更详细描述。options 参数的常规值是
String数组,但是参数类型是Object数组。根据对象的以下类型为每个对象创建一个按钮:
- Component
- 该组件被直接添加到按钮行中。
- Icon
- 创建的
JButton以此图标作为其标签。- 其他
- 该
Object通过使用其toString方法转换为字符串,并使用该结果作为JButton的标签。- icon
- 要置于对话框中的装饰性图标。图标的默认值由
messageType参数确定。- title
- 对话框的标题。
- initialValue
- 默认选择(输入值)。
当选择更改时,调用生成 PropertyChangeEvent 的 setValue 方法。
如果已为所有输入 setWantsInput 配置了 JOptionPane,则还可以侦听绑定属性 JOptionPane.INPUT_VALUE_PROPERTY,以确定何时用户输入或选择了值。
当其中一个 showXxxDialog 方法返回整数时,可能的值为:
YES_OPTION
NO_OPTION
CANCEL_OPTION
OK_OPTION
CLOSED_OPTION
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
JOptionPane.showInternalMessageDialog(frame, "information",
"information", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showConfirmDialog(null,
"choose one", "choose one", JOptionPane.YES_NO_OPTION);
JOptionPane.showInternalConfirmDialog(frame,
"please choose one", "information",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
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(Remote Method Invocation,远程方法调用)。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder。
JInternalFrame| 嵌套类摘要 | |
|---|---|
protected class |
JOptionPane.AccessibleJOptionPane
此类实现对 JOptionPane 类的可访问性支持。 |
| 从类 javax.swing.JComponent 继承的嵌套类/接口 |
|---|
JComponent.AccessibleJComponent |
| 从类 java.awt.Container 继承的嵌套类/接口 |
|---|
Container.AccessibleAWTContainer |
| 从类 java.awt.Component 继承的嵌套类/接口 |
|---|
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy |
| 字段摘要 | |
|---|---|
static int |
CANCEL_OPTION
选择 CANCEL 时从类方法返回的值。 |
static int |
CLOSED_OPTION
用户没有做出任何选择而关闭了窗口时从类方法返回的值,很可能将此值视为 CANCEL_OPTION 或 NO_OPTION。 |
static int |
DEFAULT_OPTION
意味着外观不应该提供任何选项的类型,即仅使用取自 JOptionPane 的选项。 |
static int |
ERROR_MESSAGE
用于错误消息。 |
protected Icon |
icon
在窗格中使用的图标。 |
static String |
ICON_PROPERTY
icon 的绑定属性名。 |
static int |
INFORMATION_MESSAGE
用于信息消息。 |
static String |
INITIAL_SELECTION_VALUE_PROPERTY
initialSelectionValue 的绑定属性名。 |
static String |
INITIAL_VALUE_PROPERTY
initialValue 的绑定属性名。 |
protected Object |
initialSelectionValue
要在 selectionValues 中选择的初始值。 |
protected Object |
initialValue
应该在 options 中最初选择的值。 |
static String |
INPUT_VALUE_PROPERTY
inputValue 的绑定属性名。 |
protected Object |
inputValue
用户已输入的值。 |
protected Object |
message
要显示的消息。 |
static String |
MESSAGE_PROPERTY
message 的绑定属性名。 |
static String |
MESSAGE_TYPE_PROPERTY
type 的绑定属性名。 |
protected int |
messageType
消息类型。 |
static int |
NO_OPTION
选择 NO 时从类方法返回的值。 |
static int |
OK_CANCEL_OPTION
用于 showConfirmDialog 的类型。 |
static int |
OK_OPTION
选择 OK 时从类方法返回的值。 |
static String |
OPTION_TYPE_PROPERTY
optionType 的绑定属性名。 |
protected Object[] |
options
要向用户显示的选项。 |
static String |
OPTIONS_PROPERTY
option 的绑定属性名。 |
protected int |
optionType
选项类型, DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION 或 OK_CANCEL_OPTION 之一。 |
static int |
PLAIN_MESSAGE
未使用图标。 |
static int |
QUESTION_MESSAGE
用于问题。 |
static String |
SELECTION_VALUES_PROPERTY
selectionValues 的绑定属性名。 |
protected Object[] |
selectionValues
用户可以从中选择值的数组。 |
static Object |
UNINITIALIZED_VALUE
指示用户尚未选择值。 |
protected Object |
value
当前选择的值,该值将为有效选项或者 UNINITIALIZED_VALUE 或 null。 |
static String |
VALUE_PROPERTY
value 的绑定属性名。 |
static String |
WANTS_INPUT_PROPERTY
wantsInput 的绑定属性名。 |
protected boolean |
wantsInput
如果为 true,则向用户提供 UI 窗口小部件以获取输入。 |
static int |
WARNING_MESSAGE
用于警告消息。 |
static int |
YES_NO_CANCEL_OPTION
用于 showConfirmDialog 的类型。 |
static int |
YES_NO_OPTION
用于 showConfirmDialog 的类型。 |
static int |
YES_OPTION
选择 YES 时从类方法返回的值。 |
| 从类 javax.swing.JComponent 继承的字段 |
|---|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
| 从类 java.awt.Component 继承的字段 |
|---|
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
| 从接口 java.awt.image.ImageObserver 继承的字段 |
|---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
| 构造方法摘要 | |
|---|---|
JOptionPane()
创建一个带有测试消息的 JOptionPane。 |
|
JOptionPane(Object message)
创建一个显示消息的 JOptionPane 的实例,使其使用 UI 提供的普通消息消息类型和默认选项。 |
|
JOptionPane(Object message,
int messageType)
创建一个显示消息的 JOptionPane 的实例,使其具有指定的消息类型和默认选项。 |
|
JOptionPane(Object message,
int messageType,
int optionType)
创建一个显示消息的 JOptionPane 的实例,使其具有指定的消息类型和选项。 |
|
JOptionPane(Object message,
int messageType,
int optionType,
Icon icon)
创建一个显示消息的 JOptionPane 的实例,使其具有指定的消息类型、选项和图标。 |
|
JOptionPane(Object message,
int messageType,
int optionType,
Icon icon,
Object[] options)
创建一个显示消息的 JOptionPane 的实例,使其具有指定的消息类型、图标和选项。 |
|
JOptionPane(Object message,
int messageType,
int optionType,
Icon icon,
Object[] options,
Object initialValue)
在指定最初选择的选项的前提下,创建一个显示消息的 JOptionPane 的实例,使其具有指定的消息类型、图标和选项。 |
|
| 方法摘要 | |
|---|---|
JDialog |
createDialog(Component parentComponent,
String title)
创建并返回一个新 JDialog,它在 parentComponent 窗体中的 parentComponent 中央包装 this。 |
JDialog |
createDialog(String title)
创建并返回一个新的带有指定标题的无父 JDialog。 |
JInternalFrame |
createInternalFrame(Component parentComponent,
String title)
创建并返回 JInternalFrame 的实例。 |
AccessibleContext |
getAccessibleContext()
返回与此 JOptionPane 相关联的 AccessibleContext。 |
static JDesktopPane |
getDesktopPaneForComponent(Component parentComponent)
返回指定组件的桌面窗格。 |
static Frame |
getFrameForComponent(Component parentComponent)
返回指定组件的 Frame。 |
Icon |
getIcon()
返回此窗格显示的图标。 |
Object |
getInitialSelectionValue()
返回(根据最初选择)向用户显示的输入值。 |
Object |
getInitialValue()
返回初始值。 |
Object |
getInputValue()
如果 wantsInput 为 true,则返回用户已输入的值。 |
int |
getMaxCharactersPerLineCount()
返回要置于消息的行中的最大字符数。 |
Object |
getMessage()
返回此窗格显示的消息对象。 |
int |
getMessageType()
返回消息类型。 |
Object[] |
getOptions()
返回用户可以作出的选择。 |
int |
getOptionType()
返回显示的选项类型。 |
static Frame |
getRootFrame()
返回用于不提供窗体的类方法中的 Frame。 |
Object[] |
getSelectionValues()
返回输入选择值。 |
OptionPaneUI |
getUI()
返回实现此组件 L&F 的 UI 对象。 |
String |
getUIClassID()
返回实现此组件 L&F 的 UI 类的名称。 |
Object |
getValue()
返回用户所选值。 |
boolean |
getWantsInput()
返回 wantsInput 属性的值。 |
protected String |
paramString()
返回此 JOptionPane 的字符串表示形式。 |
void |
selectInitialValue()
请求选择初始值,该请求将焦点设置为初始值。 |
void |
setIcon(Icon newIcon)
设置要显示的图标。 |
void |
setInitialSelectionValue(Object newValue)
设置(根据选择)最初向用户显示的输入值。 |
void |
setInitialValue(Object newInitialValue)
设置要启用的初始值,即最初显示窗格时处于焦点状态的 Component。 |
void |
setInputValue(Object newValue)
设置由用户选择或输入的输入值。 |
void |
setMessage(Object newMessage)
设置选项窗格的消息对象。 |
void |
setMessageType(int newType)
设置选项窗格的消息类型。 |
void |
setOptions(Object[] newOptions)
设置此窗格显示的选项。 |
void |
setOptionType(int newType)
设置要显示的选项。 |
static void |
setRootFrame(Frame newRootFrame)
设置窗体,以用于不提供窗体的类方法。 |
void |
setSelectionValues(Object[] newValues)
设置窗格的输入选择值,该窗格向用户提供可以从中进行选择的项列表。 |
void |
setUI(OptionPaneUI ui)
设置实现此组件 L&F 的 UI 对象。 |
void |
setValue(Object newValue)
设置用户所选值。 |
void |
setWantsInput(boolean newValue)
设置 wantsInput 属性。 |
static int |
showConfirmDialog(Component parentComponent,
Object message)
调出带有选项 Yes、No 和 Cancel 的对话框;标题为 Select an Option。 |
static int |
showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType)
调出一个由 optionType 参数确定其中选项数的对话框。 |
static int |
showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType,
int messageType)
调用一个由 optionType 参数确定其中选项数的对话框,messageType 参数确定要显示的图标。 |
static int |
showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType,
int messageType,
Icon icon)
调出一个带有指定图标的对话框,其中的选项数由 optionType 参数确定。 |
static String |
showInputDialog(Component parentComponent,
Object message)
显示请求用户输入内容的问题消息对话框,它以 parentComponent 为父级。 |
static String |
showInputDialog(Component parentComponent,
Object message,
Object initialSelectionValue)
显示请求用户输入内容的问题消息对话框,它以 parentComponent 为父级。 |
static String |
showInputDialog(Component parentComponent,
Object message,
String title,
int messageType)
显示请求用户输入内容的对话框,它以 parentComponent 为父级,该对话框的标题为 title,消息类型为 messageType。 |
static Object |
showInputDialog(Component parentComponent,
Object message,
String title,
int messageType,
Icon icon,
Object[] selectionValues,
Object initialSelectionValue)
提示用户在可以指定初始选择、可能选择及其他所有选项的模块化的对话框中输入内容。 |
static String |
showInputDialog(Object message)
显示请求用户输入的问题消息对话框。 |
static String |
showInputDialog(Object message,
Object initialSelectionValue)
显示请求用户输入的问题消息对话框,它带有已初始化为 initialSelectionValue 的输入值。 |
static int |
showInternalConfirmDialog(Component parentComponent,
Object message)
调出带有选项 Yes、No 和 Cancel 的内部对话框面板;标题为 Select an Option。 |
static int |
showInternalConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType)
调出一个由 optionType 参数确定其中选项数的内部对话框面板。 |
static int |
showInternalConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType,
int messageType)
调出一个由 optionType 参数确定其中选项数的内部对话框面板,messageType 参数确定要显示的图标。 |
static int |
showInternalConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType,
int messageType,
Icon icon)
调出一个带有指定图标的内部对话框面板,其中的选项数由 optionType 参数确定。 |
static String |
showInternalInputDialog(Component parentComponent,
Object message)
显示请求用户输入内容的内部问题消息对话框,它以 parentComponent 为父级。 |
static String |
showInternalInputDialog(Component parentComponent,
Object message,
String title,
int messageType)
显示请求用户输入内容的内部对话框,它以 parentComponent 为父级。 |
static Object |
showInternalInputDialog(Component parentComponent,
Object message,
String title,
int messageType,
Icon icon,
Object[] selectionValues,
Object initialSelectionValue)
提示用户在可以指定初始选择、可能选择及其他所有选项的模块化的内部对话框中输入内容。 |
static void |
showInternalMessageDialog(Component parentComponent,
Object message)
调出内部确认对话框面板。 |
static void |
showInternalMessageDialog(Component parentComponent,
Object message,
String title,
int messageType)
调出一个显示消息的内部对话框面板,它使用由 messageType 参数确定的默认图标。 |
static void |
showInternalMessageDialog(Component parentComponent,
Object message,
String title,
int messageType,
Icon icon)
调出一个显示消息的内部对话框面板,为其指定了所有参数。 |
static int |
showInternalOptionDialog(Component parentComponent,
Object message,
String title,
int optionType,
int messageType,
Icon icon,
Object[] options,
Object initialValue)
调出一个带有指定图标的内部对话框面板,其中的初始选择由 initialValue 参数确定,选项数由 optionType 参数确定。 |
static void |
showMessageDialog(Component parentComponent,
Object message)
调出标题为 "Message" 的信息消息对话框。 |
static void |
showMessageDialog(Component parentComponent,
Object message,
String title,
int messageType)
调出对话框,它显示使用由 messageType 参数确定的默认图标的 message。 |
static void |
showMessageDialog(Component parentComponent,
Object message,
String title,
int messageType,
Icon icon)
调出一个显示信息的对话框,为其指定了所有参数。 |
static int |
showOptionDialog(Component parentComponent,
Object message,
String title,
int optionType,
int messageType,
Icon icon,
Object[] options,
Object initialValue)
调出一个带有指定图标的对话框,其中的初始选择由 initialValue 参数确定,选项数由 optionType 参数确定。 |
void |
updateUI()
UIManager 发出的关于 L&F 已改变的通知。 |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 字段详细信息 |
|---|
public static final Object UNINITIALIZED_VALUE
public static final int DEFAULT_OPTION
JOptionPane 的选项。
public static final int YES_NO_OPTION
showConfirmDialog 的类型。
public static final int YES_NO_CANCEL_OPTION
showConfirmDialog 的类型。
public static final int OK_CANCEL_OPTION
showConfirmDialog 的类型。
public static final int YES_OPTION
public static final int NO_OPTION
public static final int CANCEL_OPTION
public static final int OK_OPTION
public static final int CLOSED_OPTION
CANCEL_OPTION 或 NO_OPTION。
public static final int ERROR_MESSAGE
public static final int INFORMATION_MESSAGE
public static final int WARNING_MESSAGE
public static final int QUESTION_MESSAGE
public static final int PLAIN_MESSAGE
public static final String ICON_PROPERTY
icon 的绑定属性名。
public static final String MESSAGE_PROPERTY
message 的绑定属性名。
public static final String VALUE_PROPERTY
value 的绑定属性名。
public static final String OPTIONS_PROPERTY
option 的绑定属性名。
public static final String INITIAL_VALUE_PROPERTY
initialValue 的绑定属性名。
public static final String MESSAGE_TYPE_PROPERTY
type 的绑定属性名。
public static final String OPTION_TYPE_PROPERTY
optionType 的绑定属性名。
public static final String SELECTION_VALUES_PROPERTY
selectionValues 的绑定属性名。
public static final String INITIAL_SELECTION_VALUE_PROPERTY
initialSelectionValue 的绑定属性名。
public static final String INPUT_VALUE_PROPERTY
inputValue 的绑定属性名。
public static final String WANTS_INPUT_PROPERTY
wantsInput 的绑定属性名。
protected transient Icon icon
protected transient Object message
protected transient Object[] options
protected transient Object initialValue
options 中最初选择的值。
protected int messageType
protected int optionType
DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION 或 OK_CANCEL_OPTION 之一。
protected transient Object value
UNINITIALIZED_VALUE 或 null。
protected transient Object[] selectionValues
protected transient Object inputValue
protected transient Object initialSelectionValue
selectionValues 中选择的初始值。
protected boolean wantsInput
| 构造方法详细信息 |
|---|
public JOptionPane()
JOptionPane。
public JOptionPane(Object message)
JOptionPane 的实例,使其使用 UI 提供的普通消息消息类型和默认选项。
message - 要显示的 Objectpublic JOptionPane(Object message, int messageType)
JOptionPane 的实例,使其具有指定的消息类型和默认选项。
message - 要显示的 ObjectmessageType - 要显示的消息类型:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGEpublic JOptionPane(Object message, int messageType, int optionType)
JOptionPane 的实例,使其具有指定的消息类型和选项。
message - 要显示的 ObjectmessageType - 要显示的消息类型:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE 或 PLAIN_MESSAGEoptionType - 要在窗格中显示的选项:DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION、OK_CANCEL_OPTIONpublic JOptionPane(Object message, int messageType, int&nb