Java弹出对话框的实现——JOptionPane的使用方法

在一些Java小程序或游戏中,我们点击某个按钮或者进行某项操作时,经常会弹出一个对话框,来提示我们选择确定、取消等操作。在Java中,可以调用JOptionPane这个类来生成对话框,并根据需求对其进行不同的功能设置。通过调用该类中不同的方法,并输入不同的参数,我们可以得到不同功能的对话框。

创建对话框时我们要导入 javax.swing.JOptionPane,这是JOptionPane的类包。
在Java中,有四种常用的对话框类型,分别是:

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

确认对话框

确认对话框的作用是使用户确认其操作,一般包含操作的介绍信息及“确认”、“取消”等按钮。
确认对话框有四种参数设置类型:
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(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(parentComponent, message);
JOptionPane.showMessageDialog(parentComponent, message, title, messageType);
JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);

选择对话框

选择对话框包含了多个选项操作,因此需要定义很多的参数设置。
选择对话框只有一种参数设置类型:
JOptionPane.showOptionDialog(parentComponent, message, title, optionType, messageType, icon, options, initialValue)

参数

在调用了一个对话框方法后,我们可以填写不同的参数格式,来生成不同格式的对话框。以下是对话框中的参数,以及它们各自的含义:

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

消息类型

编写的对话框共有五种消息类型,分别代表了不同的消息内容,并且会显示不同的图标。
错误❌:ERROR_MESSAGE
信息ℹ️:INFORMATION_MESSAGE
警告⚠️:WARNING_MESSAGE
问题❓:QUESTION_MESSAGE
纯文本:PLAIN_MESSAGE

  • 1
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值