11-2:使用JOptionPane类的静态方法:
JOptionPane的类层次结构图:
java.lang.Object
--java.awt.Component
--java.awt.Container
--javax.swing.JComponent
--javax.swing.JOptionPane
以JDialog来制作对话框,你必须实作对话框中的每一个组件,但有时候我们的对话框只是要显示一段文字,或是一些简单的
选择(是或否),这时候可以利用JOptionPane类,它可以让你很简单的做出这样的效果,不仅大大的减少了程序代码的编写,也
让整个程序看起来清爽许多!
JOptionPane的构造函数:
- JOptionPane():建立一个显示测试信息的JOptionPane组件。
- JOptionPane(Object message):建立一个显示特定信息的JOptionPane组件。
- 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组件,并设置信息与选项,且可显示出图案.选项值是一个Object Array,可
用作更改按钮上的文字.
- JOptionPane(Object message,int messageType,int optionType,Icon icon,Object[] options,Object initialValue):建立一个
显示特定信息的JOptionPane组件,并设置信息与选项类型,且可以显示出图案.选项值是
一个Object Array,可用作更改按钮上的文字,并设置默认按钮.
使用JOptionPane对象所得到的对话框是modal为true形式,也就是说我们必须先关闭对话框窗口才能回到产生对话框的母窗口上
.
要利用JOptionPane类来输出对话框,通常我们不会new一个JOptionPane对象出来,而是使用JOptionPane所提供的一些静态方法,
不用产生JOptionPane对象就可以直接使用,这些方法方法都是以showXxxxxDialog的形式出现,若你的对话框是出现在
InternaleFrame上,你可以用showInternalXxxxxDialog的各种方法产生对话框.以下我们整理出JOptionPane提供输出对话框的所有
静态方法:
Message Dialog
- void showMessageDialog(Component parentComponent,Object message)
- void showMessageDialog(Component parentComponent,Object message,String title,int messageType)
- void showMessageDialog(Component parentComponent,Object message,String title,int messageType,Icon
icon)
- void showInternalMessageDialog(Component parentComponent,Object message)
- void showInternalMessageDialog(Component parentComponent,Object message,String title,int messageType)
- void showInternalMessageDialog(Component parentComponent,Object message,String title,int messageType
,Icon icon)
说明: 显示信息对话框,对话框中只含有一个按钮,通常是"确定"按钮,例如安装完某个软件时通常会跳出一个对话框告知你
安装已经成功.这类的方法有5种参数:
- parentComponent:是指产生对话框的组件为何,通常是指Frame或Dialog组件.
- message:是指要显示的组件,通常是String或Label类型
- title:对话框标题列上显示的文字.
- messageType:指定信息类型,共有5种类型,分别是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
QUESTION_MESSAGE,PLAIN_MESSAGE(不显示图标).指定类型后对话框就会出现相对应的图标.
- icon:若你不喜欢java给的图标,你可以自己自定图标.
Confirm Dialog
- int showConfirmDialog(Component parentComponent,Object message)
- int showConfirmDialog(Component parentComponent,Object message,String title,int optionType)
- int showConfirmDialog(Component parentComponent,Object message,String title,int optionType,
int messageType)
- int showConfirmDialog(Component parentComponent,Object message,String title,int optionType,
int messageType,Icon icon)
- int showInternalConfirmDialog(Component parentComponent,Object message)
- int showInternalConfirmDialog(Component parentComponent,Object message,String title,int optionType)
- int showInternalConfirmDialog(Component parentComponent,Object message,String title,int optionType
,int messageType)
- int showInternalConfirmDialog(Component parentComponent,Object message,String title,int optionType
,int messageType,Icon icon)
说明: 显示确认对话框,这类的对话框通常会问用户一个问题,然后用户回答是或不是,例如当我们修改了某个文件的内容
却没存盘就要离开时,系统大部份都会跳出确认对话框.询问我们是否要存储修改过的内容.确认对话框方法有6种
参数:
- parentComponent:是指产生对话框的组件为何,通常是指Frame或Dialog组件.
- message:是指要显示的组件,通常是String或Label类型
- title:对话框标题列上显示的文字.
- optionType:确定按钮的类型,有5种类型,分别是DEFAULT_OPTION,YES_NO_OPTION,YES_NO_CANCEL,与
OK_CANCEL_OPTION.
messageType:指定信息类型,共有5种类型,分别是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
QUESTION_MESSAGE,PLAIN_MESSAGE(不显示图标).指定类型后对话框就会出现相对应的图标.
- icon:若你不喜欢java给的图标,你可以自己自定图标.
返回值为一整数值,依用户按下什么钮而定,YES_OPTION=0,NO_OPTION=1,CANCEL_OPTION=2,OK_OPTION=0
,CLOSED_OPTION=-1(当用户都不选直接关掉对话框时)
Input Dialog
- String showInputDialog(Object message)
String showInputDialog(Component parentComponent,Object message)
- String showInputDialog(Component parentComponent,Object message,String title,int messageType)
- Object showInputDailog(Component parentComponent,Object message,String title,int messageType,Icon icon
,Object[] selectionValues,Object initialSelectionValue)
- String showInternalInputDialog(Object message)
- String showInternalInputDialog(Component parentComponent,Object message)
- String showInternalInputDialog(Component parentComponent,Object message,String title,int messageType)
- Object showInternalInputDialog(Component parentComponent,Object message,String title,int messageType,
Icon icon,Object[] selectionValues,Object initialSelectionValue)
说明: 显示输入对话框,这类的对话框可以让用户输入相关的信息,当用户按下确定钮后,系统会得到用户所输入的信息.输入
对话框不仅可以让用户输入文字,也可以提供Combo Box组件让用户选择相关信息,避免用户输入错误.输入对话框方法
有8种参数:
- parentComponent:是指产生对话框的组件为何,通常是指Frame或Dialog组件.
- message:是指要显示的组件,通常是String或Label类型
- title:对话框标题列上显示的文字.
- messageType:指定信息类型,共有5种类型,分别是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE, QUESTION_MESSAGE,PLAIN_MESSAGE(不显示图标).指定类型后对话框就会出现相对应的图标.
- icon:若你不喜欢java给的图标,你可以自己自定图标.
- selectionValues:给用户选择的可能值.
- Object Array中的数据会以ComboBox方式显示出来.
- initialSelectionValue:对话框初始化时所显示的值.
当用户按下确定按钮时会返回用户输入的信息,若按下取消按钮则返回null.
Option Dialog
- int showOptionDialog(Component parentComponent,Object message,String title,int optionType,int
messageType,Icon icon,Object[] options,Object initalValue)
- int showInternalOptionDialog(Component parentComponent,Object message,String title,int optionType,
int messageType,Icon icon,Object[] options,Object initialValue)
说明: 显示选择对话框,这类的对话框可以让用户自定义对话类型,最大的好处是可以改变按钮上的文字.选择对话框方法有8种参数:
- parentComponent:是指产生对话框的组件为何,通常是指Frame或Dialog组件.
- message:是指要显示的组件,通常是String或Label类型
- title:对话框标题列上显示的文字.
- optionType:确定按钮的类型,有5种类型,分别是DEFAULT_OPTION,YES_NO_OPTION,YES_NO_CANCEL,与
OK_CANCEL_OPTION.
- messageType:指定信息类型,共有5种类型,分别是ERROR_MESSAGE,INFORMATION_MESSAGE,WARING_MESSAGE,
QUESTION_MESSAGE,PLAIN_MESSAGE(不显示图标).指定类型后对话框就会出现相对应的图标.
- icon:若你不喜欢java给的图标,你可以自己自定图标.
- options:给用户选择的按钮显示文字.
- initalValue:对话框初始化时按钮默认值.
返回值为一整数值,依用户按下什么钮而定,YES_OPTION=0,NO_OPTION=1,CANCEL_OPTION=2,OK_OPTION=0
,CLOSED_OPTION=-1(当用户都不选直接关掉对话框时)
上面表格看起来好像很多方法似的,但实际上只区分成四大类,你只需要选择要用哪类的对话框,再决定使用那类中的哪个方法即
可.我们慢慢来为你介绍这四个种类的对话框.