Swing
Swing教學首頁
Swing介紹
Swing開發環境安裝
Swing控件
Swing Component類
Swing Container類
Swing JComponent類
Swing JLabel
Swing JButton
Swing JColorChooser
Swing JCheckBox
Swing JRadioButton
Swing JList
Swing JComboBox
Swing JTextField
Swing JTextArea
Swing ImageIcon
Swing JScrollBar
Swing JOptionPane
Swing JFileChooser
Swing JProgressBar
Swing JSlider
Swing JSpinner
Swing事件處理
SWING Event事件類
SWING AWTEvent事件類
SWING ActionEvent事件類
SWING InputEvent事件類
SWING KeyEvent事件類
SWING MouseEvent事件類
SWING WindowEvent事件類
SWING AdjustmentEvent事件處理
SWING ComponentEvent處理類
SWING ContainerEvent事件處理類
SWING MouseMotionEvent事件處理
SWING PaintEvent事件類
Swing事件監聽器
SWING ActionListener接口
SWING ComponentListener接口
Swing ItemListener接口
Swing KeyListener接口
Swing MouseListener Interface
Swing WindowListener接口
Swing AdjustmentListener接口
Swing ContainerListener接口
Swing MouseMotionListener接口
Swing FocusListener接口
Swing事件適配器
Swing FocusAdapter類
Swing KeyAdapter類
Swing MouseAdapter類
Swing WindowAdapter類
Swing MouseMotionAdapter類
Swing Layout佈局
Swing LayoutManager接口
Swing LayoutManager2接口
Swing BorderLayout佈局
Swing CardLayout佈局
Swing FlowLayout佈局類
Swing GridLayout佈局類
Swing GridBagLayout佈局類
Swing GroupLayout佈局類
Swing SpringLayout佈局類
Swing Menu菜單類
Swing JMenuBar類
Swing JMenuItem類
Swing JMenu類
Swing JCheckboxMenuItem類及例子
Swing JRadioButtonMenuItem類及例子
Swing JPopupMenu類及實例
Swing容器
Swing JPanel類及實例
Swing JFrame類和實例
Swing JWindow類及實例

Swing JOptionPane

JOptionPane 類一個組件,它提供了標準的方法,彈出一個標準的對話框,或者通知用戶的東西。

類聲明

以下是聲明 javax.swing.JOptionPane類:

public class JOptionPane extends JComponent implements Accessible

字段域

以下是javax.swing.JOptionPane類的字段:

  • static int CANCEL_OPTION --Return value from class method if CANCEL is chosen.

  • static int CLOSED_OPTION --Return value from class method if user closes window without selecting anything, more than likely this should be treated as either a CANCEL_OPTION or NO_OPTION.

  • static int DEFAULT_OPTION --Type meaning Look and Feel should not supply any options -- only use the options from the JOptionPane.

  • static int ERROR_MESSAGE --Used for error messages.

  • protected Icon icon --Icon used in pane.

  • static string ICON_PROPERTY --Bound property name for icon.

  • static int INFORMATION_MESSAGE --Used for information messages.

  • static string INITIAL_SELECTION_VALUE_PROPERTY --Bound property name for initialSelectionValue.

  • static string INITIAL_VALUE_PROPERTY --Bound property name for initialValue.

  • protected Object initialSelectionValue --Initial value to select in selectionValues.

  • protected Object initialValue --Value that should be initially selected in options.

  • static string INPUT_VALUE_PROPERTY --Bound property name for inputValue.

  • protected Object inputValue --Value the user has input.

  • protected Object message --Message to display.

  • static string MESSAGE_PROPERTY --Bound property name for message.

  • static string MESSAGE_TYPE_PROPERTY --Bound property name for type.

  • protected int messageType --Message type.

  • static int NO_OPTION --Return value from class method if NO is chosen.

  • static int OK_CANCEL_OPTION --Type used for showConfirmDialog.

  • static int OK_OPTION --Return value form class method if OK is chosen.

  • static string OPTION_TYPE_PROPERTY --Bound property name for optionType.

  • protected Object[] options --Options to display to the user.

  • static string OPTIONS_PROPERTY --Bound property name for option.

  • protected int optionType --Option type, one of DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION or OK_CANCEL_OPTION.

  • static int PLAIN_MESSAGE --No icon is used.

  • static int QUESTION_MESSAGE --Used for questions.

  • static string SELECTION_VALUES_PROPERTY --Bound property name for selectionValues.

  • protected Object[] selectionValues --Array of values the user can choose from.

  • static Object UNINITIALIZED_VALUE --Indicates that the user has not yet selected a value.

  • protected Object value --Currently selected value, will be a valid option, or UNINITIALIZED_VALUE or null.

  • static string VALUE_PROPERTY --Bound property name for value.

  • static string WANTS_INPUT_PROPERTY --Bound property name for wantsInput.

  • protected boolean wantsInput --If true, a UI widget will be provided to the user to get input.

  • static int WARNING_MESSAGE --Used for warning messages.

  • static int YES_NO_CANCEL_OPTION --Type used for showConfirmDialog.

  • static int YES_NO_OPTION --Type used for showConfirmDialog.

  • static int YES_OPTION --Return value from class method if YES is chosen.

類構造函數

S.N.

構造函數 & 描述

1

JOptionPane()
Creates a JOptionPane with a test message.

2

JOptionPane(Object message)
Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI.

3

JOptionPane(Object message, int messageType)
Creates an instance of JOptionPane to display a message with the specified message type and the default options

4

JOptionPane(Object message, int messageType, int optionType)
Creates an instance of JOptionPane to display a message with the specified message type and options.

5

JOptionPane(Object message, int messageType, int optionType, Icon icon)
Creates an instance of JOptionPane to display a message with the specified message type, options, and icon.

6

JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options)
Creates an instance of JOptionPane to display a message with the specified message type, icon, and options.

7

JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options, Object initialValue)
Creates an instance of JOptionPane to display a message with the specified message type, icon, and options, with the initially-selected option specified.

類方法

S.N.

方法 & 描述

1

JDialog createDialog(Component parentComponent, String title)
Creates and returns a new JDialog wrapping this centered on the parentComponent in the parentComponent's frame.

2

JDialog createDialog(String title)
Creates and returns a new parentless JDialog with the specified title.

3

JInternalFrame createInternalFrame(Component parentComponent, String title)
Creates and returns an instance of JInternalFrame.

4

AccessibleContext getAccessibleContext()
Returns the AccessibleContext associated with this JOptionPane.

5

static JDesktopPane getDesktopPaneForComponent(Component parentComponent)
Returns the specified component's desktop pane.

6

static Frame getFrameForComponent(Component parentComponent)
Returns the specified component's Frame.

7

Icon getIcon()
Returns the icon this pane displays.

8

Object getInitialSelectionValue()
Returns the input value that is displayed as initially selected to the user.

9

Object getInitialValue()
Returns the initial value.

10

Object getInputValue()
Returns the value the user has input, if wantsInput is true.

11

int getMaxCharactersPerLineCount()
Returns the maximum number of characters to place on a line in a message.

12

Object getMessage()
Returns the message-object this pane displays.

13

int getMessageType()
Returns the message type.

14

Object[] getOptions()
Returns the choices the user can make.

15

int getOptionType()
Returns the type of options that are displayed.

16

static Frame getRootFrame()
Returns the Frame to use for the class methods in which a frame is not provided.

17

Object[] getSelectionValues()
Returns the input selection values.

18

OptionPaneUI getUI()
Returns the UI object which implements the L&F for this component.

19

String getUIClassID()
Returns the name of the UI class that implements the L&F for this component.

20

Object getValue()
Returns the value the user has selected.

21

boolean getWantsInput()
Returns the value of the wantsInput property.

22

protected String paramString()
Returns a string representation of this JOptionPane.

23

void selectInitialValue()
Requests that the initial value be selected, which will set focus to the initial value.

24

void setIcon(Icon newIcon)
Sets the icon to display.

25

void setInitialSelectionValue(Object newValue)
Sets the input value that is initially displayed as selected to the user.

26

void setInitialValue(Object newInitialValue)
Sets the initial value that is to be enabled -- the Component that has the focus when the pane is initially displayed.

27

void setInputValue(Object newValue)
Sets the input value that was selected or input by the user.

28

void setMessage(Object newMessage)
Sets the option pane's message-object.

29

void setMessageType(int newType)
Sets the option pane's message type.

30

void setOptions(Object[] newOptions)
Sets the options this pane displays.

31

void setOptionType(int newType)
Sets the options to display.

32

static voidsetRootFrame(Frame newRootFrame)
Sets the frame to use for class methods in which a frame is not provided.

33

void setSelectionValues(Object[] newValues)
Sets the input selection values for a pane that provides the user with a list of items to choose from.

34

void setUI(OptionPaneUI ui)
Sets the UI object which implements the L&F for this component.

35

void setValue(Object newValue)
Sets the value the user has chosen.

36

void setWantsInput(boolean newValue)
Sets the wantsInput property.

37

static int showConfirmDialog(Component parentComponent, Object message)
Brings up a dialog with the options Yes, No and Cancel; with the title, Select an Option.

38

static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType)
Brings up a dialog where the number of choices is determined by the optionType parameter.

39

static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType)
Brings up a dialog where the number of choices is determined by the optionType parameter, where the messageType parameter determines the icon to display.

40

static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon)
Brings up a dialog with a specified icon, where the number of choices is determined by the optionType parameter.

41

static String showInputDialog(Component parentComponent, Object message)
Shows a question-message dialog requesting input from the user parented to parentComponent.

42

static String showInputDialog(Component parentComponent, Object message, Object initialSelectionValue)
Shows a question-message dialog requesting input from the user and parented to parentComponent.

43

static String showInputDialog(Component parentComponent, Object message, String title, int messageType)
Shows a dialog requesting input from the user parented to parentComponent with the dialog having the title title and message type messageType.

44

static Object showInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)
Prompts the user for input in a blocking dialog where the initial selection, possible selections, and all other options can be specified.

45

static String showInputDialog(Object message)
Shows a question-message dialog requesting input from the user.

46

static String showInputDialog(Object message, Object initialSelectionValue)
Shows a question-message dialog requesting input from the user, with the input value initialized to initialSelectionValue.

47

static int showInternalConfirmDialog(Component parentComponent, Object message)
Brings up an internal dialog panel with the options Yes, No and Cancel; with the title, Select an Option.

48

static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType)
Brings up a internal dialog panel where the number of choices is determined by the optionType parameter.

49

static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType)
Brings up an internal dialog panel where the number of choices is determined by the optionType parameter, where the messageType parameter determines the icon to display.

50

static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon)
Brings up an internal dialog panel with a specified icon, where the number of choices is determined by the optionType parameter.

51

static String showInternalInputDialog(Component parentComponent, Object message)
Shows an internal question-message dialog requesting input from the user parented to parentComponent.

52

static String showInternalInputDialog(Component parentComponent, Object message, String title, int messageType)
Shows an internal dialog requesting input from the user parented to parentComponent with the dialog having the title title and message type messageType.

53

static Object showInternalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)
Prompts the user for input in a blocking internal dialog where the initial selection, possible selections, and all other options can be specified.

54

static voidshowInternalMessageDialog(Component parentComponent, Object message)
Brings up an internal confirmation dialog panel.

55

static voidshowInternalMessageDialog(Component parentComponent, Object message, String title, int messageType)
Brings up an internal dialog panel that displays a message using a default icon determined by the messageType parameter.

56

static voidshowInternalMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)
Brings up an internal dialog panel displaying a message, specifying all parameters.

57

static voidshowMessageDialog(Component parentComponent, Object message)
Brings up an information-message dialog titled "Message".

58

static voidshowMessageDialog(Component parentComponent, Object message, String title, int messageType)
Brings up a dialog that displays a message using a default icon determined by the messageType parameter.

59

static voidshowMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)
Brings up a dialog displaying a message, specifying all parameters.

60

static int showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue)
Brings up a dialog with a specified icon, where the initial choice is determined by the initialValue parameter and the number of choices is determined by the optionType parameter.

61

void updateUI()
Notification from the UIManager that the L&F has changed.

62

static int showInternalOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue)
Brings up an internal dialog panel with a specified icon, where the initial choice is determined by the initialValue parameter and the number of choices is determined by the optionType parameter.

方法繼承

這個類從以下類繼承的方法:

  • javax.swing.JComponent

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

JOptionPane 例子

選擇使用任何編輯器創建以下java程序在 D:/ > SWING > com > yiibai > gui >

SwingControlDemo.java

package com.yiibai.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 TutorialsPoint.com"); } }); javaButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int output = JOptionPane.showConfirmDialog(mainFrame , "Click any button" ,"TutorialsPoint.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" ,"TutorialsPoint.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 comyiibaiguiSwingControlDemo.java

如果沒有錯誤出現,這意味着編譯成功。使用下面的命令來運行程序。

D:SWING>java com.yiibai.gui.SwingControlDemo

驗證下面的輸出

Swing