javax.swing类 JOptionPane

javax.swing类 JOptionPane

java.lang.Object

java.awt.Component

java.awt.Container

javax.swing.JComponent

javax.swing.JOptionPane

所有已实现的接口:

ImageObserver,

MenuContainer,

Serializable,

Accessible

public class JOptionPane

extends

JComponent

implements

Accessible

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_MESSAGE

INFORMATION_MESSAGE

WARNING_MESSAGE

QUESTION_MESSAGE

PLAIN_MESSAGE

optionType

定义在对话框的底部显示的选项按钮的集合:

DEFAULT_OPTION

YES_NO_OPTION

YES_NO_CANCEL_OPTION

OK_CANCEL_OPTION

用户并非仅限于使用此选项按钮集合。使用 options 参数可以提供想使用的任何按钮。

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

示例:

显示一个错误对话框,该对话框显示的 message 为 'alert':

JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);

显示一个内部信息对话框,其 message 为 'information':

JOptionPane.showInternalMessageDialog(frame, "information",

"information", JOptionPane.INFORMATION_MESSAGE);

显示一个信息面板,其 options 为 "yes/no",message 为 'choose one':

JOptionPane.showConfirmDialog(null,

"choose one", "choose one", JOptionPane.YES_NO_OPTION);

显示一个内部信息对话框,其 options 为 "yes/no/cancel",message 为 'please choose one',并具有 title 信息:

JOptionPane.showInternalConfirmDialog(frame,

"please choose one", "information",

JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

显示一个警告对话框,其 options 为 OK、CANCEL,title 为 'Warning',message 为 'Click OK to continue':

Object[] options = { "OK", "CANCEL" }; JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",

JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,

null, options, options[0]);

显示一个要求用户键入 String 的对话框:

String inputValue = JOptionPane.showInputDialog("Please input a value");

显示一个要求用户选择 String 的对话框:

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) 提示用户在可以指定初始选

JDK 1.6 中文手册

相关阅读

如何选择并正确使用隔离霜?
BET3365手机下载

如何选择并正确使用隔离霜?

⌚ 01-03 👁️ 4788
超好玩的情怀游戏有哪些
跨境365软件

超好玩的情怀游戏有哪些

⌚ 08-31 👁️ 2957
邮件发送失败原因服务器连接超时,我在发送邮件时经常出现发送失败或是连接超时的情况?...