P11 JDialog 窗口
- 1.概述
- 2.JOptionPane 代码实例
- 3.效果演示
- 4.JDialog 代码实例
- 5.效果演示
系统:Win10
Java:1.8.0_333
IDEA:2020.3.4
Gitee:https://gitee.com/lijinjiang01/JavaSwing
1.概述
JDialog:一个对话框,可以使用 JDialog 类可以创建自定义对话框,或者调用JOptionPane 中的多个静态方法快速创建各种标准的对话框
JOptionPane:是 JavaSwing 内部已实现好的,以静态方法的形式提供调用,能够快速方便的弹出要求用户提供值或向其发出通知的标准对话框
JOptionPane 提供的标准对话框类型分为以下几种:
方法名 | 描述 |
---|---|
showConfirmDialog | 确认对话框,询问一个问题是否执行 |
showInputDialog | 输入对话框,要求用户提供某些输入 |
showMessageDialog | 消息对话框,向用户展示一个消息,没有返回值 |
showOptionDialog | 选项对话框,上述三项的大统一,自定义按钮文本,询问用户需要点击哪个按钮 |
这些标准对话框的基本外形布局通常如下图所示:
上述四个类型的方法(包括其若干重载)的参数遵循一致的模式,下面介绍各参数的含义:
(1) parentComponent: 对话框的父级组件,决定对话框显示的位置,对话框的显示会尽量紧靠组件的中心,如果传 null,则显示在屏幕的中心。
(2) title: 对话框标题。
(3) message: 消息内容。
(4) messageType: 消息类型,主要是提供默认的对话框图标。可能的值为:
- JOptionPane.PLAIN_MESSAGE 简单消息(不使用图标)
- JOptionPane.INFORMATION_MESSAGE 信息消息(默认)
- JOptionPane.QUESTION_MESSAGE 问题消息
- JOptionPane.WARNING_MESSAGE 警告消息
- JOptionPane.ERROR_MESSAGE 错误消息
(5) icon: 自定义的对话框图标,如果传 null,则图标类型由 messageType 决定。
(6) optionType: 选项按钮的类型。
(7) options、initialValue: 自定义的选项按钮(如果传 null,则选项按钮由 optionType 决定),以及默认选中的选项按钮。
(8) selectionValues、initialSelectionValue: 提供的输入选项,以及默认选中的选项。
下面是 JOptionPane 类中各标准对话框的静态方法重载:
消息对话框:
static void showMessageDialog(Component parentComponent, Object message)static void showMessageDialog(Component parentComponent, Object message, String title, int messageType)static void showMessageDialog(Component parentComponent,Object message, String title,int messageType,Icon icon)
确认对话框:
static int showConfirmDialog(Component parentComponent,Object message)static int showConfirmDialog(Component parentComponent, Object message, String title,int optionType)static int showConfirmDialog(Component parentComponent, Object message, String title,int optionType,int messageType)static int showConfirmDialog(Component parentComponent,Object message, String title,int optionType,int messageType, Icon icon)
输入对话框:
static String showInputDialog(Component parentComponent,Object message)static String showInputDialog(Component parentComponent,Object message, Object initialSelectionValue)static String showInputDialog(Component parentComponent,Object message, String title,int messageType)static Object showInputDialog(Component parentComponent,Object message, String title,int messageType,Icon icon, Object[] selectionValues,Object initialSelectionValue)
选项对话框:
static int showOptionDialog(Component parentComponent,Object message, String title,int optionType, int messageType, Icon icon,Object[] options,Object initialValue)
2.JOptionPane 代码实例
示例代码
import com.lijinjiang.beautyeye.BeautyEyeLNFHelper;
import com.lijinjiang.beautyeye.ch3_button.BEButtonUI;
import javax.swing.*;
import java.awt.event.ActionEvent;public class Demo01 {public static void main(String[] args) {try {BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;BeautyEyeLNFHelper.launchBeautyEyeLNF();} catch (Exception e) {e.printStackTrace();}JFrame frame = new JFrame();frame.setTitle("Demo01"); // 设置窗口标题frame.setSize(400, 360); // 设置窗口显示大小-JPanel panel = new JPanel();panel.setLayout(null);/* 1.消息对话框(提示消息) */JButton button1 = new JButton("showMessageDialog(提示)");button1.setBounds(100, 20, 180, 30);button1.addActionListener(e -> JOptionPane.showMessageDialog(frame, "这是一条提示信息","标题1", JOptionPane.INFORMATION_MESSAGE));/* 2.消息对话框(警告消息) */JButton button2 = new JButton("showMessageDialog(警告)");button2.setBounds(100, 70, 180, 30);button2.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));button2.addActionListener(e -> JOptionPane.showMessageDialog(frame, "警告!警告!","标题2", JOptionPane.WARNING_MESSAGE));/* 3.确认对话框 */JButton button3 = new JButton("showConfirmDialog");button3.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));button3.setBounds(100, 120, 180, 30);button3.addActionListener(e -> {int res = JOptionPane.showConfirmDialog(frame, "确认要删除?","标题3", JOptionPane.YES_NO_CANCEL_OPTION);System.out.println("选择的结果:" + res);});/* 4.输入对话框(文本输入) */JButton button4 = new JButton("showInputDialog(输入)");button4.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red));button4.setBounds(100, 170, 180, 30);button4.addActionListener(e -> {String input = JOptionPane.showInputDialog(frame, "请输入访问密码","标题4", JOptionPane.WARNING_MESSAGE);System.out.println("输入的内容:" + input);});/* 5.输入对话框(下拉框选择) */JButton button5 = new JButton("showInputDialog(下拉)");button5.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue));button5.setBounds(100, 220, 180, 30);button5.addActionListener(e -> {Object[] selectValues = new Object[]{"春季", "夏季", "秋季", "冬季"};Object value = JOptionPane.showInputDialog(frame, "请选择最喜欢的季节:","标题5", JOptionPane.PLAIN_MESSAGE, null, selectValues, selectValues[0]);System.out.println("选择的内容:" + value);});/* 6.选项对话框 */JButton button6 = new JButton("showOptionDialog");button6.setBounds(100, 270, 180, 30);button6.addActionListener((ActionEvent e) -> {Object[] options = new Object[]{"上午", "下午"};int optionSelected = JOptionPane.showOptionDialog(frame, "请选择合适的时间","标题6", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE,null, options, options[0]);if (optionSelected >= 0) {System.out.println("选择的按钮:" + options[optionSelected]);}});panel.add(button1);panel.add(button2);panel.add(button3);panel.add(button4);panel.add(button5);panel.add(button6);frame.add(panel);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口默认关闭操作frame.setLocationRelativeTo(null); // 相对屏幕居中frame.setVisible(true); // 设置窗口可见}
}
3.效果演示
4.JDialog 代码实例
对话框 JDialog 和 JFrame 都是继承自 java.awt.Window,用法与 JFrame 类似
对话框分为 模态 和 非模态:
- 模态: 弹出对话框后,对话框的父级窗口不可操作
- 非模态: 弹出对话框后,对话框的父级窗口可以正常操作
对话框不能最小化
代码如下
import com.lijinjiang.beautyeye.BeautyEyeLNFHelper;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class Demo02 {// 显示自定义对话框private static void showCustomDialog(JFrame frame) {// 定义个模态框final JDialog dialog = new JDialog(frame, "提示", true);// 设置大小dialog.setSize(250, 150);// 设置大小不可变dialog.setResizable(false);// 设置对话框的相对显示位置dialog.setLocationRelativeTo(frame);// 创建一个标签显示消息信息JLabel messageLabel = new JLabel("对话框消息内容");// 创建一个按钮用于关闭对话框JButton closeBtn = new JButton("关闭");closeBtn.addActionListener(e -> dialog.dispose());// 创建内容面板,在面板内可以根据自己的需求添加任何组件并作任意布局JPanel panel = new JPanel();// 添加组件到面板panel.add(messageLabel);panel.add(closeBtn);// 设置对话框的内容面板dialog.setContentPane(panel);// 显示对话框dialog.setVisible(true);}public static void main(String[] args) {try {BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;BeautyEyeLNFHelper.launchBeautyEyeLNF();} catch (Exception e) {e.printStackTrace();}JFrame frame = new JFrame();frame.setTitle("Demo02"); // 设置窗口标题frame.setSize(400, 300); // 设置窗口显示大小-JPanel panel = new JPanel();panel.setLayout(null);/* 显示自定义对话框 */JButton button1 = new JButton("显示自定义对话框");button1.setBounds(100, 100, 180, 30);button1.addActionListener(e -> showCustomDialog(frame));panel.add(button1);frame.add(panel);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口默认关闭操作frame.setLocationRelativeTo(null); // 相对屏幕居中frame.setVisible(true); // 设置窗口可见}
}