0


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

标签: java

本文转载自: https://blog.csdn.net/qq_42592994/article/details/120082313
版权归原作者 曼城铁闸 所有, 如有侵权,请联系我们删除。

“Java弹出对话框的实现——JOptionPane的使用方法”的评论:

还没有评论