目录

JOptionPane

介绍 (Introduction)

JOptionPane类是一个组件,它提供标准方法来弹出值的标准对话框或通知用户某些内容。

Class 声明 (Class Declaration)

以下是javax.swing.JOptionPane类的声明 -

public class JOptionPane
   extends JComponent
      implements Accessible

字段 (Field)

以下是javax.swing.JOptionPane类的字段 -

  • static int CANCEL_OPTION - 如果选择CANCEL,则返回类方法的返回值。

  • static int CLOSED_OPTION - 如果用户在没有选择任何内容的情况下关闭窗口,则返回类方法的值,这很可能会被视为CANCEL_OPTION或NO_OPTION。

  • static int DEFAULT_OPTION - 类型含义Look and Feel不应提供任何选项仅使用JOptionPane中的选项。

  • static int ERROR_MESSAGE - 用于错误消息。

  • protected Icon icon - 窗格中使用的protected Icon icon

  • static string ICON_PROPERTY - 图标的绑定属性名称。

  • static int INFORMATION_MESSAGE - 用于信息消息。

  • static string INITIAL_SELECTION_VALUE_PROPERTY - initialSelectionValue的绑定属性名称。

  • static string INITIAL_VALUE_PROPERTY - initialValue的绑定属性名称。

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

  • protected Object initialValue - 应在选项中初始选择的值。

  • static string INPUT_VALUE_PROPERTY - static string INPUT_VALUE_PROPERTY绑定属性名称。

  • protected Object inputValue - 用户输入的值。

  • protected Object message - 要显示的消息。

  • static string MESSAGE_PROPERTY - 消息的绑定属性名称。

  • static string MESSAGE_TYPE_PROPERTY - 类型的绑定属性名称。

  • static int OK_CANCEL_OPTION - 用于showConfirmDialog的类型。

  • protected int messageType - 消息类型。

  • static int NO_OPTION - 如果选择NO,则从类方法返回值。

  • static int OK_OPTION - 如果选择OK,则从类方法返回值。

  • static string OPTION_TYPE_PROPERTY - optionType的绑定属性名称。

  • protected Object[] options - 显示给用户的选项。

  • static string OPTIONS_PROPERTY - 选项的绑定属性名称。

  • 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 - 值的绑定属性名称。

  • 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,则从类方法返回值。

类构造函数 (Class Constructors)

Sr.No. 构造函数和描述
1

JOptionPane()

使用测试消息创建JOptionPane。

2

JOptionPane(Object message)

创建JOptionPane实例以使用纯文本消息类型和UI提供的默认选项显示消息。

3

JOptionPane(Object message, int messageType)

创建JOptionPane的实例以显示具有指定消息类型和默认选项的消息

4

JOptionPane(Object message, int messageType, int optionType)

创建JOptionPane的实例以显示具有指定消息类型和选项的消息。

5

JOptionPane(Object message, int messageType, int optionType, Icon icon)

创建JOptionPane的实例以显示具有指定消息类型,选项和图标的消息。

6

JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options)

创建JOptionPane的实例以显示具有指定消息类型,图标和选项的消息。

7

JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options, Object initialValue)

创建JOptionPane实例以显示具有指定消息类型,图标和选项的消息,并指定最初选择的选项。

Class Methods

Sr.No. 方法和描述
1

JDialog createDialog(Component parentComponent, String title)

创建并返回一个新的JDialog,它以parentComponent框架中的parentComponent为中心。

2

JDialog createDialog(String title)

创建并返回具有指定标题的新的无父JDialog。

3

JInternalFrame createInternalFrame(Component parentComponent, String title)

创建并返回JInternalFrame的实例。

4

AccessibleContext getAccessibleContext()

返回与此JOptionPane关联的AccessibleContext。

5

static JDesktopPane getDesktopPaneForComponent(Component parentComponent)

返回指定组件的桌面窗格。

6

static Frame getFrameForComponent(Component parentComponent)

返回指定组件的Frame。

7

Icon getIcon()

返回此窗格显示的图标。

8

Object getInitialSelectionValue()

返回最初选择给用户显示的输入值。

9

Object getInitialValue()

返回初始值。

10

Object getInputValue()

如果wantsInput为true,则返回用户输入的值。

11

int getMaxCharactersPerLineCount()

返回消息中一行上的最大字符数。

12

Object getMessage()

返回此窗格显示的消息对象。

13

int getMessageType()

返回消息类型。

14

Object[] getOptions()

返回用户可以做出的选择。

15

int getOptionType()

返回显示的选项类型。

16

static Frame getRootFrame()

返回用于未提供框架的类方法的Frame。

17

Object[] getSelectionValues()

返回输入选择值。

18

OptionPaneUI getUI()

返回实现此组件的L&F的UI对象。

19

String getUIClassID()

返回实现此组件的L&F的UI类的名称。

20

Object getValue()

返回用户选择的值。

21

boolean getWantsInput()

返回wantsInput属性的值。

22

protected String paramString()

返回此JOptionPane的字符串表示形式。

23

void selectInitialValue()

请求选择初始值,将焦点设置为初始值。

24

void setIcon(Icon newIcon)

设置要显示的图标。

25

void setInitialSelectionValue(Object newValue)

设置最初显示为用户选择的输入值。

26

void setInitialValue(Object newInitialValue)

设置要启用的初始值 - 最初显示窗格时具有焦点的Component。

27

void setInputValue(Object newValue)

设置用户选择或输入的输入值。

28

void setMessage(Object newMessage)

设置选项窗格的message-object。

29

void setMessageType(int newType)

设置选项窗格的消息类型。

30

void setOptions(Object[] newOptions)

设置此窗格显示的选项。

31

void setOptionType(int newType)

设置要显示的选项。

32

static voidsetRootFrame(Frame newRootFrame)

设置要用于未提供框架的类方法的框架。

33

void setSelectionValues(Object[] newValues)

设置窗格的输入选择值,该窗格为用户提供可供选择的项列表。

34

void setUI(OptionPaneUI ui)

设置实现此组件的L&F的UI对象。

35

void setValue(Object newValue)

设置用户选择的值。

36

void setWantsInput(boolean newValue)

设置wantsInput属性。

37

static int showConfirmDialog(Component parentComponent, Object message)

使用选项Yes,No和Cancel打开一个对话框; 使用标题,选择一个选项。

38

static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType)

打开一个对话框,其中选项数由optionType参数确定。

39

static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType)

打开一个对话框,其中选项数由optionType参数确定,其中messageType参数确定要显示的图标。

40

static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon)

打开带有指定图标的对话框,其中选项数由optionType参数确定。

41

static String showInputDialog(Component parentComponent, Object message)

显示一个问题消息对话框,请求来自父组件父级的用户输入。

42

static String showInputDialog(Component parentComponent, Object message, Object initialSelectionValue)

显示一个问题消息对话框,请求用户输入并作为parentComponent的父级。

43

static String showInputDialog(Component parentComponent, Object message, String title, int essageType)

显示一个对话框,请求来自父组件父级的用户输入,对话框具有标题标题和消息类型messageType。

44

static Object showInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)

在阻止对话框中提示用户输入,其中可以指定初始选择,可能的选择和所有其他选项。

45

static String showInputDialog(Object message)

显示请求用户输入的问题消息对话框。

46

static String showInputDialog(Object message, Object initialSelectionValue)

显示请求用户输入的问题消息对话框,输入值初始化为initialSelectionValue。

47

static int showInternalConfirmDialog(Component parentComponent, Object message)

打开一个内部对话框面板,其中包含选项Yes,No和Cancel; 使用标题,选择一个选项。

48

static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType)

打开一个内部对话框面板,其中选项的数量由optionType参数确定。

49

static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType)

打开一个内部对话框面板,其中选项数由optionType参数确定,其中messageType参数确定要显示的图标。

50

static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon)

打开带有指定图标的内部对话框面板,其中选项数由optionType参数确定。

51

static String showInternalInputDialog(Component parentComponent, Object message)

显示内部问题消息对话框,请求来自父组件父级的用户输入。

52

static String showInternalInputDialog(Component parentComponent, Object message, String title, int messageType)

显示一个内部对话框,请求来自父组件父级的用户输入,对话框具有标题标题和消息类型messageType。

53

static Object showInternalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)

在阻塞内部对话框中提示用户输入,其中可以指定初始选择,可能的选择和所有其他选项。

54

static voidshowInternalMessageDialog(Component parentComponent, Object message)

打开内部确认对话框面板。

55

static voidshowInternalMessageDialog(Component parentComponent, Object message, String title, int messageType)

打开一个内部对话框面板,使用messageType参数确定的默认图标显示消息。

56

static voidshowInternalMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)

打开一个显示消息的内部对话框面板,指定所有参数。

57

static voidshowMessageDialog(Component parentComponent, Object message)

打开一个标题为“消息”的信息消息对话框。

58

static voidshowMessageDialog(Component parentComponent, Object message, String title, int messageType)

打开一个对话框,使用messageType参数确定的默认图标显示消息。

59

static voidshowMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)

打开一个显示消息的对话框,指定所有参数。

60

static int showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue)

打开带有指定图标的对话框,其中初始选择由initialValue参数确定,选项数由optionType参数确定。

61

void updateUI()

来自UIManager的通知L&F已经改变。

62

static int showInternalOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue)

打开具有指定图标的内部对话框面板,其中初始选择由initialValue参数确定,选项数由optionType参数确定。

方法继承 (Methods Inherited)

该类继承以下类中的方法 -

  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JOptionPane示例

使用您选择的任何编辑器创建以下Java程序,例如D:/ 》 SWING 》 com 》 iowiki 》 gui 》

SwingControlDemo.java

package com.iowiki.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showDialogDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());
      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showDialogDemo(){                                       
      headerLabel.setText("Control in action: JOptionPane"); 
      JButton okButton = new JButton("OK");        
      JButton javaButton = new JButton("Yes/No");
      JButton cancelButton = new JButton("Yes/No/Cancel");
      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(mainFrame, "Welcome to iowiki.com");
         }          
      });
      javaButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            int output = JOptionPane.showConfirmDialog(mainFrame
               , "Click any button"
               ,"iowiki.com"
               ,JOptionPane.YES_NO_OPTION);
            if(output == JOptionPane.YES_OPTION){
               statusLabel.setText("Yes selected.");
            } else if(output == JOptionPane.NO_OPTION){
               statusLabel.setText("No selected.");
            }
         }
      });
      cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {                
            int output = JOptionPane.showConfirmDialog(mainFrame
               , "Click any button"
               ,"iowiki.com"
               ,JOptionPane.YES_NO_CANCEL_OPTION,
               JOptionPane.INFORMATION_MESSAGE);
            if(output == JOptionPane.YES_OPTION){
               statusLabel.setText("Yes selected.");
            } else if(output == JOptionPane.NO_OPTION){
               statusLabel.setText("No selected.");
            } else if(output == JOptionPane.CANCEL_OPTION){
               statusLabel.setText("Cancel selected.");
            }
         }
      });
      controlPanel.add(okButton);
      controlPanel.add(javaButton);
      controlPanel.add(cancelButton);       
      mainFrame.setVisible(true);  
   }
}

使用命令提示符编译程序。 转到D:/ 》 SWING并键入以下命令。

D:\SWING>javac com\iowiki\gui\SwingControlDemo.java

如果没有错误发生,则表示编译成功。 使用以下命令运行该程序。

D:\SWING>java com.iowiki.gui.SwingControlDemo

验证以下输出。

Swing JOptionPane
↑回到顶部↑
WIKI教程 @2018