Java中,类似Windows中MessageBox的使用,用到了JOptionPane类。
利用JOptionPane类中的各个static方法生成了各种类型的对话框。这些对话框都是模式对话框。具体的方法有:
ConfirmDialog ---确认对话框,提出问题,由用户来选择回答,对话框有Yes/No按钮;
InputDialog ---提示输入文本的对话框;
MessageDialog -- 仅显示一些信息;
OptionDialog ---组合了以上三个对话框。
函数原型:
int javax.swing.JOptionPane.showConfirmDialog(Component parentComponent, Object message, String title, int optionType) throws HeadlessException
参数:
parentComponent
- determines the
Frame
in which the dialog is displayed; if
null
, or if the
parentComponent
has no
Frame
, a default
Frame
is used
message
- the
Object
to display
title
- the title string for the dialog
optionType
- an int designating the options available on the dialog:
YES_NO_OPTION
,
YES_NO_CANCEL_OPTION
, or
OK_CANCEL_OPTION
返回值:用户选择的按钮(Yes, No, Cancel, OK)
应用实例:
int rc = JOptionPane.showConfirmDialog(null,
"File already exist. Do you want to overwrite?",
"Overwrite file",
JOptionPane.YES_NO_OPTION);
函数原型:
String javax.swing.JOptionPane.showInputDialog(Object message) throws HeadlessException
参数:message
- the Object
to display
返回值:用户输入的信息
应用实例:
JOptionDialog.showInputDialog("Please input a value");
函数原型:
void javax.swing.JOptionPane.showMessageDialog(Component parentComponent, Object message) throws HeadlessException
参数:
parentComponent
- determines the
Frame
in which the dialog is displayed; if
null
, or if the
parentComponent
has no
Frame
, a default
Frame
is used
message
- the
Object
to display
应用实例:
JOptionPane.showMessageDialog(null, "Getting device list failed. Please check the device.");
弹出一个MessageBox,标题为"消息",内容为"Getting device list failed. Please check the device.",并有一个"确定"按钮。
函数原型:
int javax.swing.JOptionPane.showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) throws HeadlessException
参数:
parentComponent
- determines the
Frame
in which the dialog is displayed; if
null
, or if the
parentComponent
has no
Frame
, a default
Frame
is used
message
- the
Object
to display
title
- the title string for the dialog
optionType
- an integer designating the options available on the dialog:
DEFAULT_OPTION
,
YES_NO_OPTION
,
YES_NO_CANCEL_OPTION
, or
OK_CANCEL_OPTION
messageType
- an integer designating the kind of message this is, primarily used to determine the icon from the pluggable Look and Feel:
ERROR_MESSAGE
,
INFORMATION_MESSAGE
,
WARNING_MESSAGE
,
QUESTION_MESSAGE
, or
PLAIN_MESSAGE
icon
- the icon to display in the dialog
options
- an array of objects indicating the possible choices the user can make; if the objects are components, they are rendered properly; non-
String
objects are rendered using their
toString
methods; if this parameter is
null
, the options are determined by the Look and Feel
initialValue
- the object that represents the default selection for the dialog; only meaningful if
options
is used; can be
null
返回值:an integer indicating the option chosen by the user, or CLOSED_OPTION
if the user closed the dialog
应用实例:
Object[] options = {"确定", "取消", "帮助"};
int response = JOptionPane.showOptionDialog(null, "这是一个选择对话框", "选项对话框标题", JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(response == 0) ... Yes
else if(response == 1) ... Cancel
else if(response == 2) ... Help