Java 创建图形用户界面(GUI)组件详解之对话框(弹窗)组件 JoptionPane
文章目录
组件详解
对话框(弹窗)组件
对话框:向用户显示信息或者获取用户提供的信息
分类:
模式对话框:在结束对它的处理之前,不允许用户与应用程序的其余窗口进行交互,主要用于在程序继续运行之前获取用户提供的信息。
例如,当用户想要读取一个文件时,就会弹出一个模式文件对话框;用户必须给定一个文件名,然后程序才能够开始读操作;只有用户关闭这个模式对话框之后,应用才能够继续执行。
无模式对话框:允许用户同时在对话框和应用程序的其他部分输入信息
例如,工具条。只要需要,工具条可以停靠在任何地方,而且用户可以根据需要同时与应用窗口和工具条进行交互。
标准对话框:JoptionPane
Swing 有一组现成的简单对话框,可以让用户提供一些信息。
JOptionPane 有 4 个用于显示对话框的静态方法:
对话框有以下组件:
一个图标
一条消息
一个或多个选项按钮
输入对话框有一个用于接收用户输人的额外组件。
一个文本域,用户可以输入任何的字符串,或是一个组合框,用户可以从中选择一项。
这些对话框的具体布局和为标准消息类型选择的图标都取决于可插接式观感。
左侧的图标取决于下面 5 种消息类型:
每个对话框类型都有一个方法,可以用来提供自己的图标,以替代原来的图标。
可以为每个对话框类型指定一条消息
这里的消息既可以是字符串、图标、用户界面组件,也可以是其他类型的对象。
可以如下显示消息对象:
提供字符串消息是目前为止最常见的情况,而提供一个 Component 会带来更大的灵活性,这是因为可以让 paintComponent()
方法绘制你想要的任何内容。
对话框位于底部的按钮取决于对话框类型和选项类型
showMessageDialog():只能看到一个标准的按钮:确认
showInputDialog():只能看到一组标准按钮:确认/取消
showOptionDialog() :可以指定一组任意的选项
需要提供一个对象数组作为选项,每个数组元素会如下显示:
Component:显示这个组件
其他类型的对象:应用 toString() 方法,然后创建一个按钮,用结果字符串作为标签。
showConfirmDialog() :可以指定 optionType(选项类型)属性为四种选项类型之一:
代码示例:
int selection = JOptionPane.showConfirmDialog(parent,
"Message","Title",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (selection =JOptionPane.OK OPTION){
...
}
JoptionPane 常用方法
javax.swing.JoptionPane:
显示一个消息对话框或者一个内部消息对话框(内部对话框完全显示在其父组件窗体内)
static void showMessageDialog(Component parent, Object message, String title, int messageType,
Icon icon)
static void showMessageDialog(Component parent, Object message, String title, int messageType)
static void showMessageDialog(Component parent, Object message)
static void showInternalMessageDialog(Component parent, Object message, String title, int
messageType,Icon icon)
static void showInternalMessageDialog(Component parent, Object message, String title, int
messageType)
static void showInternalMessageDialog(Component parent, Object message)
显示一个确认对话框或者内部确认对话框(内部对话框完全显示在其父组件窗体内)
static int showConfirmDialog(Component parent, Object message, String title, int optionType,
int messageType, Icon icon)
static int showConfirmDialog(Component parent, Object message, String title, int optionType,
int messageType)
static int showConfirmDialog(Component parent, Object message, String title, int optionType)
static int showConfirmDialog(Component parent, Object message)
static int showInternalConfirmDialog(Component parent, object message, String title, int
optionType,int messageType,Icon icon)
static int showInternalConfirmDialog(Component parent, Object message, String title, int
optionType,int messageType)
static int showInternalConfirmDialog(Component parent, Object message, String title, int
optionType)
static int showInternalConfirmDialog(Component parent, Object message)
显示一个选项对话框或者内部选项对话框(内部对话框完全显示在其父组件窗体内)
static int showoptionDialog(Component parent, Object message, String title, int optionType,
int messageType, Icon icon, Object[] options, Object default)
static int showInternalOptionDialog(Component parent, Object message, String title, int
optionType, int messageType, Icon icon, Object[] options, Object default)
显示一个输入对话框或者内部输入对话框(内部对话框完全显示在其父组件窗体内)
static Object showInputDialog(Component parent, Object message, String title, int messageType,
Icon icon, Object[] values,Object default)
static String showInputDialog(Component parent, Object message, String title, int messageType)
static String showInputDialog(Component parent, Object message)
static String showInputDialog(Object message)
static String showInputDialog(Component parent, Object message, Object default)
static String showInputDialog(Object message, Object default)
static Object showInternalInputDialog(Component parent, Object message, String title, int
messageType, Icon icon, Object[]values, Object default)
static String showInternalInputDialog(Component parent, Object message, String title, int messagelype)
static String showInternalInputDialog(Component parent, Object message)
作者:墨鸦_Cormorant