JOptionPane

方法类型和参数

四种消息提示框方法

  • showConfirmDialog():确认对话框
  • showInputDialog():输入对话框
  • showMessageDialog():消息对话框
  • showOptionDialog():选择对话框

五种消息类型(类型不同,图标不同):

  • ERROR_MESSAGE
  • INFORMATION_MESSAGE
  • WARNING_MESSAGE
  • QUESTION_MESSAGE
  • PLAIN_MESSAGE

参数及其含义

通过调用不同方法,并输入不同参数可以得到不同的对话框

  • parentComponent 对话框所在的容器
  • message 提示消息
  • title 标题
  • optionType 选择按钮类型
  • messageType 消息类型
  • icon 自定义消息图标
  • initialSelectionValue 默认选项或信息
  • selectionValues 选择选项
  • options 操作选项

方法调用形式

JOptionPane.showConfirmDialog

JOptionPane.showConfirmDialog有四种参数设置类型
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)

JoptionPane.showInputDIalog

JOptionPane.showInputDialog有六种参数设置类型
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)

JOptionPane.showMessageDialog

JOptionPane.showMessageDialog有三种参数设置
JOptionPane.showMessageDialog(parentComponent, message);
JOptionPane.showMessageDialog(parentComponent, message, title, messageType);
JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);

JOptionPane.showOptionDialog

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

详细介绍

三种基本模式

  • JOptionPane.showComfirmDialog(null,”我的新世界”);

    在这里插入图片描述

  • JOptionPane.showInputDialog(null,”我的新世界”);

    在这里插入图片描述

  • JOptionPane.showMessage(null,”我的新世界”);

    在这里插入图片描述

五种消息类型

  • 错误:
    JOptionPane.showMessageDialog(null, “错误”,”提示”,JOptionPane.ERROR_MESSAGE);
    在这里插入图片描述
  • 警示:
    JOptionPane.showMessageDialog(null, “警告”,”提示”,JOptionPane.WARNING_MESSAGE);
    在这里插入图片描述
  • 普通信息:
    JOptionPane.showMessageDialog(null, “普通”,”提示”,JOptionPane.INFORMATION_MESSAGE);
    在这里插入图片描述
  • 询问信息:
    JOptionPane.showMessageDialog(null, “提问信息”,”提示”,JOptionPane.QUESTION_MESSAGE);
    在这里插入图片描述
  • 不带图标信息:
    JOptionPane.showMessageDialog(null, “不带图标”,”提示”,JOptionPane.PLAIN_MESSAGE);
    在这里插入图片描述

自定义图标

ImageIcon icon = new ImageIcon(“image/c.jpg”);//图片的大小需要调整到合适程度
JOptionPane.showMessageDialog(null, “自定义图标”,”提示”,JOptionPane.ERROR_MESSAGE,icon);
该消息框的警示信息图标被后面的参数icon所指向的图标覆盖 

在这里插入图片描述

可选按钮optionType(存在效果相同的参数变量)

  • JOptionPane.showConfirmDialog(null, “我的新世界”, “提示”,JOptionPane.OK_OPTION);
    JOptionPane.showConfirmDialog(null, “我的新世界”, “提示”,JOptionPane.YES_OPTION);
    JOptionPane.showConfirmDialog(null, “我的新世界”, “提示”,JOptionPane.YES_NO_OPTION);

    这里写图片描述

  • JOptionPane.showConfirmDialog(null, “我的新世界”, “提示”,JOptionPane.NO_OPTION);
    JOptionPane.showConfirmDialog(null, “我的新世界”, “提示”,JOptionPane.YES_NO_CANCEL_OPTION);
    这里写图片描述

  • JOptionPane.showConfirmDialog(null, “我的新世界”, “提示”,JOptionPane.CANCEL_OPTION);
    JOptionPane.showConfirmDialog(null, “我的新世界”, “提示”,JOptionPane.OK_CANCEL_OPTION);
    这里写图片描述

  • JOptionPane.showConfirmDialog(null, “我的新世界”, “提示”,JOptionPane.CLOSED_OPTION);
    JOptionPane.showConfirmDialog(null, “我的新世界”, “提示”,JOptionPane.DEFAULT_OPTION);
    这里写图片描述

在输入对话框设置下拉菜单选择框

  • Object[] fruits = {“苹果”,”梨子”,”香蕉”,”西瓜”,”荔枝”};
    JOptionPane.showInputDialog(null,”你喜欢什么水果”,”标题”,JOptionPane.QUESTION_MESSAGE,null,fruits,fruits[2]);
    最后一个参数是预选项,你希望显示出来的选项
    在这里插入图片描述

在选择对话框设置选项

  • Object[] fruits = {“苹果”,”梨子”,”香蕉”,”西瓜”,”荔枝”};
    JOptionPane.showOptionDialog(null, “你喜欢什么水果”, “标题”,JOptionPane.YES_NO_CANCEL_OPTION ,JOptionPane.QUESTION_MESSAGE,null, fruits, fruits[0]);
    在这里插入图片描述

对消息框传递的消息进行接收

  • 接收输入框输入的信息
    String str = (String)JOptionPane.showInputDialog(null);

  • 接收并判断点击的按钮是哪个,用int对象op接收对话框返回的值,并用if语句判断
    int op = JOptionPane.showConfirmDialog(null,”新世界”,”提示”,JOptionPane.YES_NO_CANCEL_OPTION);

    if(op==JOptionPane.YES_OPTION){
    
    }else if(op==JOptionPane.NO_OPTION){
    
    }
    
  • 接收选择对话框的消息(必须用数组下标接收)
    Object[] fruits = {“苹果”,”梨子”,”香蕉”,”西瓜”,”荔枝”};
    int op = JOptionPane.showOptionDialog(null, “你喜欢什么水果”, “标题”,JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null, fruits, fruits[0]);
    System.out.print((String)fruits[op]);

  • 接收输入对话框带有下拉列表框的信息(必须用字符串接收)
    Object[] fruits = {“苹果”,”梨子”,”香蕉”,”西瓜”,”荔枝”};
    String op = (String)JOptionPane.showInputDialog(null,”你喜欢什么水果”,”标题”,
    JOptionPane.QUESTION_MESSAGE,null,fruits,fruits[2]);

原文:https://blog.csdn.net/tjk123456/article/details/77868034

  • 29
    点赞
  • 145
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值