P11 JDialog 窗口

news/2024/10/22 18:33:57/

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); // 设置窗口可见}
}

5.效果演示

在这里插入图片描述


http://www.ppmy.cn/news/707514.html

相关文章

C语言学习笔记—P11(数组<2>+图解+题例+三子棋游戏<初级>)

目录 前言 ●由于作者水平有限,文章难免存在谬误之处,敬请读者斧正,俚语成篇,恳望指教! ——By 作者:新晓故知 数组讲解: 二维数组创建必须有列!行可有可无! 数组越界&#xff1…

[南京大学2022操作系统-P11] 操作系统上的进程 (最小 Linux; fork, execve 和 exit)

最小Linux:一个程序创造全世界 操作系统刚开始,只会加载第一个init程序,然后就会等待中断,随后变为"异常处理程序"。 什么叫操作系统? 操作系统就是一个状态机的管理者。 什么叫虚拟化? 虚拟…

重定向转发,接收请求参数及数据回显-P11,12

重定向和转发: 我们的实现本身就是转发 。 想删掉视图解析器的话,就需要在return把路径写全 重定向就改为redirect:而且重定向不走视图解析器,因为是新的请求,新的URL。 接收请求参数: 第一种是默认的方式…

DI依赖注入-P8,P9,P10,P11

1.构造器注入 之前写过了~~~~ 2.Set方式注入【重点】 3.拓展方式注入 2.Set方式注入【重点】 【环境搭建】 1.复杂类型 2.真实测试对象 四个文件 Student实体类的创建: 主要是依据官方文档来建立。那个Address也是为了测试不同的类型,而创建的引…

p11 p12 p13 p14 p15

p11: 字符型: 作用(字符型变量用于显示单个字符) 语法:char ch a; 注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号 注意2;单引号内只能有一个字符&…

【机器学习】P11 神经网络

神经网络 生物神经元人工神经网络Reference 生物神经元 人类如何思考? 神经科学 [ 1 ] ^{[1]} [1] 研究表明,神经元 是人类思考的基本单元。神经元是大脑中最基本的信息处理单元,它们通过复杂的电信号和化学信号传递来进行信息处理和传递。…

P11机器学习--李宏毅笔记(Bactch normolization 让error surface 变好)

目录 一、怎么变成好的error face 二、Feature Normalization 三、在深度学习中再使用Feature Normalization 四、batch normolization 一、怎么变成好的error face 从左上图来看,在w1方向上loss斜率很小但是在w2方向上loss斜率很大,当你在这个实验里…

深入浅出scala之变量定义(P11-20)

文章目录 1. 变量定义2.数据类型3.数值类型4.浮点类型5.字符类型6.字符串类型7.其他数据类型8.类型转换 1. 变量定义 基本语法: 变量定义一定要初始化 var|val变量名[: 变量类型] .变量值使用var或者val定义一个变量。 使用var(variable)声明变量,可以被重新赋值…