JOptionPane:showMessageDialog

本教程介绍如何使用 JOptionPane.showMessageDialog() 方法创建和显示显示消息的简单对话框。可以使用类 javax.swing.JOptionPane.

showMessageDialog 显示一个带有单个确定”按钮的窗口,您可以使用静态方法指定消息、标题和图标:

showMessageDialog(组件父级、对象消息、字符串标题、int typeMessage、图标图标)或:
  • parent:是父组件(窗口或面板);
  • message:是将在 JOptionPane 中显示的字符串消息;
  • title:是 string.
  • typeMessage 类型的标题:这是 entire 类型的消息类型: INFORMATION_MESSAGE、WARNING_MESSAGE、ERROR_MESSAGE和PLAIN_MESSAGE;
  • icon:替换默认图标的图标;
下面是一个测试 showMessageDialog:

import javax.swing.JOptionPane;

public class MessageDialog {

public static void main(String[] args) {
JOptionPane.showMessageDialog(null,
数据库已更新”,
更新完成”,
JOptionPane.INFORMATION_MESSAGE
null);
}

}
执行后的结果:

JOptionPane

与其他作为函数并返回 String 或 int 类型的 JOptionPane 方法不同,showMessageDialog 是一个不返回任何内容的过程。

我们将 null 放在parent”参数中,因为对话框没有父级。如果不想插入图标,请在 icon 参数框中输入 null。编译器根据消息类型显示默认图标。

References
Java Doc: showMessageDialog method
java2s: 使用 JOptionPane 显示消息:JOptionPane 对话框