Java中的方法JOptionPane对话框详细解读

JOptionPane 对话框

类 Javax.swing.JOptionPane官方文档

在使用这些方法前需要引入类
import Javax.swing.JOptionPane

目的:用于弹出一个对话框来于用户交互,可以输入输出内容

分别有四个方法

showConfirmDialog(); 用于确认/是或否,返回用户选择的内容
showinputDialog(); 用于输入文本/下拉框型的选择内容
showMessageDialog(); 用于信息提示,无返回值
showOptionDialog(); 用户选择已指定的内容,返回内容在数组当中的下标

分别都是用show + 类型 + Dialog

确认对话框 showConfirmDialog()

确认对话框可以输出一个提示确认信息,可以选择 是/否/取消 返回值是用户选择的内容

showConfirmDialog(Parent Component,
                Object message,
                String Title,
                Option Type,
                messageType,
                icon);

有四种参数设置类型 下列中有可省略部分内容
JOptionPane.showConfirmDialog(parentComponent, message)
JOptionPane.showConfirmDialog(parentComponent, message, title, optionType)
JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType)
JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType, icon)

Parent component(父组件):用于指定确认对话框所在位置,将会在 Parent component(父组件) 的正前方显示,若为null则会在屏幕正中间显示 关于父组件这里不做多解释,自行查阅啦 后续演示使用null
Object massage(对话框内容):对话框上显示的主要信息 字符串
String Title(对话框标题):左上角的标题内容 字符串
Option Type(选项控制):控制选项数量 (有效值是JOptionPane中定义的常量 如下图)
在这里插入图片描述
可以直接使用数字也行 类似于宏定义

DEFAULT_OPTION 确认
YES_NO_OPTION 是 否
YES_NO_CANCEL_OPTION 是 否 取消
OK_CANCEL_OPTION 确认 取消

messageType(消息类型):通过消息类型可以确定不同的提示方式以及默认图标
在这里插入图片描述

icon(图片路径):通过icon接入自定义图片的路径

下面发的使用方法中的new ImageIcon(“images/***.jpg”)引号中的路径的默认为项目文件夹,此处代码只需将images文件夹放入项目文件夹下即可,就是与src,bin文件并列。具体其他关于图片操作不多赘述。

食用方法:
import javax.swing.JOptionPane;
public class Main {
    public static void main(String[] args) {
        ImageIcon icon_wugan = new ImageIcon("image/wuguan.png");
        JOptionPane.showConfirmDialog(null,"我是主要内容\n当前为DEFAULT_OPTION\nshowConfirmDialog","我是标题",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE,icon_wugan);
        JOptionPane.showConfirmDialog(null,"我是主要内容\n当前为YES_NO_OPTION","我是标题",JOptionPane.YES_NO_OPTION);
        JOptionPane.showConfirmDialog(null,"我是主要内容\n当前为YES_NO_CANCEL_OPTION","我是标题",JOptionPane.YES_NO_CANCEL_OPTION);
        JOptionPane.showConfirmDialog(null,"我是主要内容\n当前为OK_CANCEL_OPTION","我是标题",JOptionPane.OK_CANCEL_OPTION);
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

返回值

当然 showConfirmDialog还会有一个int型的返回值用于反馈用户的选择 官方的java文件常量定义如下:
在这里插入图片描述

不明所以的解读1:上面截图中有一段话
Return value from class method if user closes window without selecting anything, more than likely this should be treated as either
a CANCEL_OPTION or NO_OPTION.
意思是若用户关闭页面不做选择则返回值更有可能是CANCEL_OPTION or
NO_OPTION 而不是CLOSED_OPTION
我是否可以解读为返回值-1的情况会比1或2的少呢?所以后续做程序用到这个方法,应该优先判断 YES or OK
的肯定回答?(希望有大佬能回答我)

输入对话框 showInputDialog()

输入对话框可以让用户输入字符串(String)内容 返回值为object型的输入(选择)内容

JOptionPane.showInputDialog(parentComponent,
                            message, 
                            title,
                            messageType, 
                            icon, 
                            selectionValues,
                            initialSelectionValue)

有六种参数设置类型 下列中有可省略部分内容
JOptionPane.showInputDialog(message);
JOptionPane.showInputDialog(parentComponent, message);
JOptionPane.showInputDialog(message, initialSelectionValue);
JOptionPane.showInputDialog(parentComponent, message, initialSelectionValue)
JOptionPane.showInputDialog(parentComponent, message, title, messageType);
JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, initialSelectionValue)

Parent component(父组件):用于指定确认对话框所在位置,将会在 Parent component(父组件) 的正前方显示,若为null则会在屏幕正中间显示
Object massage(对话框内容):对话框上显示的主要信息 字符串
String Title(对话框标题):左上角的标题内容 字符串
messageType(消息类型):通过消息类型可以确定不同的提示方式以及默认图标

icon(图片路径):通过icon接入自定义图片的路径 根目录与src并列
selectionValues选项参数:在此处填入选项的所有参数,举例用的是为object类的数组
initial selection value选项参数默认选中:在此处填入选项参数默认选中的值,在程序加载完成后首先显示这个值

使用方法
import javax.swing.*;


public class Main {
    public static void main(String[] args) {
        ImageIcon icon_wugan = new ImageIcon("image/wuguan.png");
        Object fruit[] = {"apple","banana","orange"};
        String s1 = (String)JOptionPane.showInputDialog(null,
                                    "what?",
                                    "title what?",
                                    JOptionPane.INFORMATION_MESSAGE,
                                    icon_wugan,
                                    fruit,
                                    fruit[0]);
        System.out.print(s1);
    }

在这里插入图片描述

返回值

注意此时的返回值为之前设置的object类型强制转换成string类型,再用于输出
上例对话框给定了类型为object的选项,返回值故为object
若对话框未设定选项,则为一个输入框,返回的数据类型为String型

提示对话框 showMessageDialog()

JOptionPane.showMessageDialog(parentComponent, 
                            message, 
                            title, 
                            messageType, 
                            icon);
                            
                            
有三种参数设置 下列中有可省略部分内容s
JOptionPane.showMessageDialog(parentComponent, message);
JOptionPane.showMessageDialog(parentComponent, message, title, messageType);
JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);

Parent component(父组件):用于指定确认对话框所在位置,将会在 Parent component(父组件) 的正前方显示,若为null则会在屏幕正中间显示
Object massage(对话框内容):对话框上显示的主要信息 字符串
String Title(对话框标题):左上角的标题内容 字符串
messageType(消息类型):通过消息类型可以确定不同的提示方式以及默认图标

icon(图片路径):通过icon接入自定义图片的路径 根目录与src并列

返回值为void型

选项对话框 showOptionDialog

只有一种参数设置
JOptionPane.showOptionDialog(parentComponent,
                            message, 
                            title,
                            optionType, 
                            messageType, 
                            icon, 
                            options, 
                            initialValue);

Parent component(父组件):用于指定确认对话框所在位置,将会在 Parent component(父组件) 的正前方显示,若为null则会在屏幕正中间显示
Object massage(对话框内容):对话框上显示的主要信息 字符串
String Title(对话框标题):左上角的标题内容 字符串
Option Type(选项控制):控制选项内容 (有效值是JOptionPane中定义的常量 如下图)
在这里插入图片描述
messageType(消息类型):通过消息类型可以确定不同的提示方式以及默认图标

icon(图片路径):通过icon接入自定义图片的路径 根目录与src并列
options选项数组:给定的选项的数组
initialvalue默认选中的选项

使用方法
import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        ImageIcon icon_wugan = new ImageIcon("image/wuguan.png");
        Object fruit[] = {"apple","banana","orange"};
        int num1 = JOptionPane.showOptionDialog(null,
                                                "你喜欢哪一种水果?",
                                                "title what?",
                                                JOptionPane.DEFAULT_OPTION,
                                                JOptionPane.INFORMATION_MESSAGE,
                                                null,
                                                fruit,
                                                fruit[2]);
        System.out.print(num1);
    }
}

在这里插入图片描述
上图可以看出,默认选项选中了orange

返回值

此函数返回值为int,为用户所选择的数组的下标

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值