JavaTM 2 Platform
Standard Ed. 5.0

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 就是一个 StringString 常量。不过,此参数的类型实际上是 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
默认选择(输入值)。

当选择更改时,调用生成 PropertyChangeEventsetValue 方法。

如果已为所有输入 setWantsInput 配置了 JOptionPane,则还可以侦听 bound 属性 JOptionPane.INPUT_VALUE_PROPERTY,以确定何时用户输入或选择了值。

当其中一个 showXxxDialog 方法返回整数时,可能的值为:

示例:
显示一个错误对话框,该对话框显示的 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 版本的应用程序之间的 RMI。从 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.BltBufferStrategy, Component.FlipBufferStrategy
 
字段摘要
static int CANCEL_OPTION
          选择 CANCEL 时从类方法返回的值。
static int CLOSED_OPTION
          用户没有做出任何选择而关闭了窗口时从类方法返回的值,很可能将此值视为 CANCEL_OPTIONNO_OPTION
static int DEFAULT_OPTION
          用于 showConfirmDialog 的类型。
static int ERROR_MESSAGE
          用于错误消息。
protected  Icon icon
          在窗格中使用的图标。
static String ICON_PROPERTY
          icon 的 bound 属性名。
static int INFORMATION_MESSAGE
          用于信息消息。
static String INITIAL_SELECTION_VALUE_PROPERTY
          initialSelectionValue 的 bound 属性名。
static String INITIAL_VALUE_PROPERTY
          initialValue 的 bound 属性名。
protected  Object initialSelectionValue
          要在 selectionValues 中选择的初始值。
protected  Object initialValue
          应该在 options 中最初选择的值。
static String INPUT_VALUE_PROPERTY
          inputValue 的 bound 属性名。
protected  Object inputValue
          用户已输入的值。
protected  Object message
          要显示的消息。
static String MESSAGE_PROPERTY
          message 的 bound 属性名。
static String MESSAGE_TYPE_PROPERTY
          type 的 bound 属性名。
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 的 bound 属性名。
protected  Object[] options
          要向用户显示的选项。
static String OPTIONS_PROPERTY
          option 的 bound 属性名。
protected  int optionType
          选项类型,DEFAULT_OPTIONYES_NO_OPTIONYES_NO_CANCEL_OPTIONOK_CANCEL_OPTION 之一。
static int PLAIN_MESSAGE
          未使用图标。
static int QUESTION_MESSAGE
          用于问题。
static String SELECTION_VALUES_PROPERTY
          selectionValues 的 bound 属性名。
protected  Object[] selectionValues
          用户可以从中选择值的数组。
static Object UNINITIALIZED_VALUE
          指示用户尚未选择值。
protected  Object value
          当前选择的值,该值将为有效选项或者 UNINITIALIZED_VALUEnull
static String VALUE_PROPERTY
          value 的 bound 属性名。
static String WANTS_INPUT_PROPERTY
          wantsInput 的 bound 属性名。
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)
          创建并在 parentComponent 窗体中的 parentComponent 中央返回一个包装 this 的新 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)
          调出带有选项 YesNoCancel 的对话框;标题为 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)
          调出带有选项 YesNoCancel 的内部对话框面板;标题为 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)
          显示请求用户提供输入的内部对话框,该对话框的标题为 title,消息类型为 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 已改变的通知。
 
从类 javax.swing.JComponent 继承的方法
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
从类 java.awt.Container 继承的方法
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
从类 java.awt.Component 继承的方法
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

UNINITIALIZED_VALUE

public static final Object UNINITIALIZED_VALUE
指示用户尚未选择值。


DEFAULT_OPTION

public static final int DEFAULT_OPTION
用于 showConfirmDialog 的类型。

另请参见:
常量字段值

YES_NO_OPTION

public static final int YES_NO_OPTION
用于 showConfirmDialog 的类型。

另请参见:
常量字段值

YES_NO_CANCEL_OPTION

public static final int YES_NO_CANCEL_OPTION
用于 showConfirmDialog 的类型。

另请参见:
常量字段值

OK_CANCEL_OPTION

public static final int OK_CANCEL_OPTION
用于 showConfirmDialog 的类型。

另请参见:
常量字段值

YES_OPTION

public static final int YES_OPTION
选择 YES 时从类方法返回的值。

另请参见:
常量字段值

NO_OPTION

public static final int NO_OPTION
选择 NO 时从类方法返回的值。

另请参见:
常量字段值

CANCEL_OPTION

public static final int CANCEL_OPTION
选择 CANCEL 时从类方法返回的值。

另请参见:
常量字段值

OK_OPTION

public static final int OK_OPTION
选择 OK 时从类方法返回的值。

另请参见:
常量字段值

CLOSED_OPTION

public static final int CLOSED_OPTION
用户没有做出任何选择而关闭了窗口时从类方法返回的值,很可能将此值视为 CANCEL_OPTIONNO_OPTION

另请参见:
常量字段值

ERROR_MESSAGE

public static final int ERROR_MESSAGE
用于错误消息。

另请参见:
常量字段值

INFORMATION_MESSAGE

public static final int INFORMATION_MESSAGE
用于信息消息。

另请参见:
常量字段值

WARNING_MESSAGE

public static final int WARNING_MESSAGE
用于警告消息。

另请参见:
常量字段值

QUESTION_MESSAGE

public static final int QUESTION_MESSAGE
用于问题。

另请参见:
常量字段值

PLAIN_MESSAGE

public static final int PLAIN_MESSAGE
未使用图标。

另请参见:
常量字段值

ICON_PROPERTY

public static final String ICON_PROPERTY
icon 的 bound 属性名。

另请参见:
常量字段值

MESSAGE_PROPERTY

public static final String MESSAGE_PROPERTY
message 的 bound 属性名。

另请参见:
常量字段值

VALUE_PROPERTY

public static final String VALUE_PROPERTY
value 的 bound 属性名。

另请参见:
常量字段值

OPTIONS_PROPERTY

public static final String OPTIONS_PROPERTY
option 的 bound 属性名。

另请参见:
常量字段值

INITIAL_VALUE_PROPERTY

public static final String INITIAL_VALUE_PROPERTY
initialValue 的 bound 属性名。

另请参见:
常量字段值

MESSAGE_TYPE_PROPERTY

public static final String MESSAGE_TYPE_PROPERTY
type 的 bound 属性名。

另请参见:
常量字段值

OPTION_TYPE_PROPERTY

public static final String OPTION_TYPE_PROPERTY
optionType 的 bound 属性名。

另请参见:
常量字段值

SELECTION_VALUES_PROPERTY

public static final String SELECTION_VALUES_PROPERTY
selectionValues 的 bound 属性名。

另请参见:
常量字段值

INITIAL_SELECTION_VALUE_PROPERTY

public static final String INITIAL_SELECTION_VALUE_PROPERTY
initialSelectionValue 的 bound 属性名。

另请参见:
常量字段值

INPUT_VALUE_PROPERTY

public static final String INPUT_VALUE_PROPERTY
inputValue 的 bound 属性名。

另请参见:
常量字段值

WANTS_INPUT_PROPERTY

public static final String WANTS_INPUT_PROPERTY
wantsInput 的 bound 属性名。

另请参见:
常量字段值

icon

protected transient Icon icon
在窗格中使用的图标。


message

protected transient Object message
要显示的消息。


options

protected transient Object[] options
要向用户显示的选项。


initialValue

protected transient Object initialValue
应该在 options 中最初选择的值。


messageType

protected int messageType
消息类型。


optionType

protected int optionType
选项类型,DEFAULT_OPTIONYES_NO_OPTIONYES_NO_CANCEL_OPTIONOK_CANCEL_OPTION 之一。


value

protected transient Object value
当前选择的值,该值将为有效选项或者 UNINITIALIZED_VALUEnull


selectionValues

protected transient Object[] selectionValues
用户可以从中选择值的数组。外观将提供从中做出选择的 UI 组件。


inputValue

protected transient Object inputValue
用户已输入的值。


initialSelectionValue

protected transient Object initialSelectionValue
要在 selectionValues 中选择的初始值。


wantsInput

protected boolean wantsInput
如果为 true,则向用户提供 UI 窗口小部件以获取输入。

构造方法详细信息

JOptionPane

public JOptionPane()
创建一个带有测试消息的 JOptionPane


JOptionPane

public JOptionPane(Object message)
创建一个显示消息的 JOptionPane 的实例,使其使用 UI 提供的普通消息消息类型和默认选项。

参数:
message - 要显示的 Object

JOptionPane

public JOptionPane(Object message,
                   int messageType)
创建一个显示消息的 JOptionPane 的实例,使其具有指定的消息类型和默认选项。

参数:
message - 要显示的 Object
messageType - 要显示的消息类型:ERROR_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGEPLAIN_MESSAGE

JOptionPane

public JOptionPane(Object message,
                   int messageType,
                   int optionType)
创建一个显示消息的 JOptionPane 的实例,使其具有指定的消息类型和选项。

参数:
message - 要显示的 Object
messageType - 要显示的消息类型:ERROR_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGEPLAIN_MESSAGE
optionType - 要在窗格中显示的选项:DEFAULT_OPTIONYES_NO_OPTIONYES_NO_CANCEL_OPTIONOK_CANCEL_OPTION

JOptionPane

public JOptionPane(Object message,
                   int messageType,
                   int optionType,
                   Icon icon)
创建一个显示消息的 JOptionPane 的实例,使其具有指定的消息类型、选项和图标。

参数:
message - 要显示的 Object
messageType - 要显示的消息类型:ERROR_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGEPLAIN_MESSAGE
optionType - 要在窗格中显示的选项:DEFAULT_OPTIONYES_NO_OPTIONYES_NO_CANCEL_OPTIONOK_CANCEL_OPTION
icon - 要显示的 Icon 图像

JOptionPane

public JOptionPane(Object message,
                   int messageType,
                   int optionType,
                   Icon icon,
                   Object[] options)
创建一个显示消息的 JOptionPane 的实例,使其具有指定的消息类型、图标和选项。最初没有选择任何选项。

options 对象应该包含 Component(直接添加的)或 String(包装在 JButton 中)的实例。如果提供 Component,则必须确保单击 Component 时,它向创建的 JOptionPane 传递消息 setValue

参数:
message - 要显示的 Object
messageType - 要显示的消息类型:ERROR_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGEPLAIN_MESSAGE
optionType - 要在窗格中显示的选项:DEFAULT_OPTIONYES_NO_OPTIONYES_NO_CANCEL_OPTIONOK_CANCEL_OPTION
icon - 要显示的 Icon 图像
options - 用户可以选择的选项

JOptionPane

public JOptionPane(Object message,
                   int messageType,
                   int optionType,
                   Icon icon,
                   Object[] options,
                   Object initialValue)
在指定最初选择的选项的前提下,创建一个显示消息的 JOptionPane 的实例,使其具有指定的消息类型、图标和选项。

参数:
message - 要显示的 Object
messageType - 要显示的消息类型:ERROR_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGEPLAIN_MESSAGE
optionType - 要在窗格中显示的选项:DEFAULT_OPTIONYES_NO_OPTIONYES_NO_CANCEL_OPTIONOK_CANCEL_OPTION
icon - 要显示的图标图像
options - 用户可以选择的选项
initialValue - 最初选择的选项;如果为 null,则不做最初选择;只有在使用 options 时才有意义
方法详细信息

showInputDialog

public static String showInputDialog(Object message)
                              throws HeadlessException
显示请求用户输入的问题消息对话框。该对话框使用默认的边框,通常意味着在屏幕上居中显示。

参数:
message - 要显示的 Object
抛出:
HeadlessException - 如果 GraphicsEnvironment.isHeadless 返回 true
另请参见:
GraphicsEnvironment.isHeadless()

showInputDialog

public static String showInputDialog(Object message,
                                     Object initialSelec