---
title: "消息对话框MessageDialog综合应用：九九表"
author: "lbsnm"
date: "2012-04-01T23:39:09Z"
tags: []
categories: ["编程语言"]
slug: "messagedialog"
draft: false
lastmod: "2012-04-02T00:03:34Z"
id: 1472914
url: "https://lbsnm.iteye.com/blog/1472914"
---

java信息对话框：MessageDialog是一个非常有用的组件，在java交互编程中，需要给出如出错、警告、操作、结果等等提示时，MessageDialog就会起到事半功倍的效果。

MessageDialog的编程需要引用showMessageDialog方法，该方法是javax.swing包中的JOptionPane类的成员，因此引用时需要导入JOptionPane类，该类共有三个showMessageDialog重载方法。

1.showMessageDialog(Component parentComponent, Object message) 

该方法是默认的信息对话框。Object message通常是字符串对象。如下例：

 

例：JOptionPane.showMessageDialog(null,"提示信息");

![](http://dl.iteye.com/upload/attachment/0065/9124/5679ed6d-32aa-39fa-b3f9-e809743a4bf7.png)  
 

其中对话框的标题和图标完全是默认的。

2.showMessageDialog(Component parentComponent, Object message, String title, int messageType)

该方法是MessageDialog中最为丰富多彩的对话框，编程人员可以通过参数String title设定对话框的标题，通过参数int messageType设定对话框的图标，以确定对话框的信息类型。

常用的类型有：

错误信息对话框：ERROR\_MESSAGE

例：JOptionPane.showMessageDialog(null,"提示信息","标题",JOptionPane.ERROR\_MESSAGE);

![](http://dl.iteye.com/upload/attachment/0065/9143/e1827cc1-37bd-38c4-99f4-a2a82a7fc681.png)

 

JOptionPane.ERROR\_MESSAGE为JOptionPane中定义的符号常量。

信息对话框：INFORMATION\_MESSAGE

例：JOptionPane.showMessageDialog(null,"提示信息","标题",JOptionPane. INFORMATION\_MESSAGE);

该设置与默认对话框的图标一样，只不过可以由用户设定标题。

 

![](http://dl.iteye.com/upload/attachment/0065/9145/069cf1f4-b6d3-35f8-bcd4-041a8aef62a9.png)

 

警告信息对话框：WARNING\_MESSAGE 

例：JOptionPane.showMessageDialog(null,"提示信息","标题",JOptionPane. WARNING\_MESSAGE);

![](http://dl.iteye.com/upload/attachment/0065/9147/c257d2d7-e51d-3b1f-93ae-8cc8b3361ef4.png)

 

询问信息对话框：QUESTION\_MESSAGE

例：JOptionPane.showMessageDialog(null,"提示信息","标题",JOptionPane. QUESTION\_MESSAGE);

![](http://dl.iteye.com/upload/attachment/0065/9149/452bdcad-71ae-3b5b-bf79-52f9b5061114.png)

 

简单信息对话框（无图标）：PLAIN\_MESSAGE

例：JOptionPane.showMessageDialog(null,"提示信息","标题",JOptionPane. PLAIN\_MESSAGE);

![](http://dl.iteye.com/upload/attachment/0065/9151/6b39f774-3284-394d-971d-2fb38029612f.png)

 

3\. showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)

该方法是功能最完整的方法，用户可以通过参数Icon icon添加自己的图标。

例：

import javax.swing.JOptionPane;

import javax.swing.Icon;

import javax.swing.ImageIcon;

class TestMessageDialog{

public static void main(String\[] args){

Icon icon=new ImageIcon(“grapes.gif”);

JOptionPane.showMessageDialog(null,"提示信息","标题",JOptionPane. PLAIN\_MESSAGE,icon);

System.exit(0);

}

}

![](http://dl.iteye.com/upload/attachment/0065/9153/6f4c53ca-6337-313f-bdf4-5fcb622ba713.png)

 

  本例中图形文件grapes.gif需要放在与类文件 TestMessageDialog相同的包里。

  如果需要在对话框中显示多行信息，可以在提示信息中加入换行符号：“\\n”。

例：输出九九表

class TestMessageDialog{

public static void main(String args\[]){

String s="";

for(int i=1;i&lt;=9;i++){

for(int j=1;j&lt;=i;j++){

s=s+j+"\*"+i+"="+i\*j+"  ";

}

s=s+"\\n";

}

JOptionPane.showMessageDialog(null,s,"九九表",JOptionPane.PLAIN\_MESSAGE);

System.exit(0);

}

}

 

![](http://dl.iteye.com/upload/attachment/0065/9155/ce4f8674-dd38-3367-92e8-123883026897.png)