Java 几种showMessageDialog的表示

最近在做swing程序中遇到使用消息提示框的,JOptionPane类其中封装了很多的方法。

很方便的,于是就简单的整理了一下。

1.1 showMessageDialog

显示一个带有OK 按钮的模态对话框。

下面是几个使用showMessageDialog 的例子:

import javax.swing.JOptionPane; //导包

public class showMessageDialogDemo {
	public static void main(String[] args) {
		JOptionPane.showMessageDialog(null, "友情提示");  
	}

}

  

效果如下:


 

import java.awt.Component;

import javax.swing.JOptionPane;

public class showMessageDialogDemo {
private static Component jPanel;

public static void main(String[] args) {
JOptionPane.showMessageDialog(jPanel, "提示消息", "标题",JOptionPane.WARNING_MESSAGE);
}

}

 效果如下:

Java代码  
JOptionPane.showMessageDialog(null, "提示消息.", "标题",JOptionPane.ERROR_MESSAGE);  

  效果如下:

 

 

Java代码  
JOptionPane.showMessageDialog(null, "提示消息.", "标题",JOptionPane.PLAIN_MESSAGE);  

  效果如下:

 

1.2 showOptionDialog

这个函数可以改变显示在按钮上的文字。你还可以执行更多的个性化操作。

常规的消息框:

int n = JOptionPane.showConfirmDialog(null, "你高兴吗?", "标题",JOptionPane.YES_NO_OPTION);//i=0/1  
		System.out.println("n:" + n); //高兴 - 0 , 不高兴 - 1 

 效果如下:

个性话消息框:

Object[] options ={ "好啊!", "去一边!" }; 
int m = JOptionPane.showOptionDialog(null, "我可以约你吗?", "标题",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); 
System.out.println("m:" + m); //好啊! -- 0 , 去一边! -- 1

  效果如下:

 

1.3 showInoutDialog

该方法返回一个Object 类型。这个Object 类型一般是一个String 类型,反应了用户的输入。

下拉列表形式的例子:

Object[] obj2 ={ "足球", "篮球", "乒乓球","网球" };  
		String s = (String) JOptionPane.showInputDialog(null,"请选择你的爱好:\n", "爱好", JOptionPane.PLAIN_MESSAGE, new ImageIcon("icon.png"), obj2, "网球");  
		System.out.println("s:"+s);

文本框形式的例子:

private static Icon icon;

String s = (String) JOptionPane.showInputDialog(null,"请输入你的爱好:\n","title",JOptionPane.PLAIN_MESSAGE,icon,null,"在这输入"); System.out.println("s:"+s);

 

   对应的小图标可参照下图:

 

 

 下面是本文档所有的参考代码:

package will01;

import java.awt.Component;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class showMessageDialogDemo {
	private static Component jPanel;
	private static Icon icon;

	public static void main(String[] args) {
		//JOptionPane.showMessageDialog(null, "友情提示");  
		//JOptionPane.showMessageDialog(jPanel, "提示消息", "标题",JOptionPane.WARNING_MESSAGE);  
		//JOptionPane.showMessageDialog(null, "提示消息.", "标题",JOptionPane.ERROR_MESSAGE);  
		//JOptionPane.showMessageDialog(null, "提示消息.", "标题",JOptionPane.PLAIN_MESSAGE);  
	
		
		//int n = JOptionPane.showConfirmDialog(null, "你高兴吗?", "标题",JOptionPane.YES_NO_OPTION);//i=0/1  
		//System.out.println("n:" + n); //高兴 - 0 , 不高兴 - 1 
	
		//Object[] options ={ "好啊!", "去一边!" };  
		//int m = JOptionPane.showOptionDialog(null, "我可以约你吗?", "标题",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);  
		//System.out.println("m:" + m); //好啊! -- 0 , 去一边! -- 1 
	
		//Object[] obj2 ={ "足球", "篮球", "乒乓球","网球" };  
		//String s = (String) JOptionPane.showInputDialog(null,"请选择你的爱好:\n", "爱好", JOptionPane.PLAIN_MESSAGE, new ImageIcon("icon.png"), obj2, "网球");  
		//System.out.println("s:"+s);
		
		
		String s = (String) JOptionPane.showInputDialog(null,"请输入你的爱好:\n","title",JOptionPane.PLAIN_MESSAGE,icon,null,"在这输入");  
		System.out.println("s:"+s);
	}

}

 参考文档: https://www.cnblogs.com/guohaoyu110/p/6440333.html 

 

posted on 2019-07-08 16:08  William_Dai  阅读(3257)  评论(0编辑  收藏  举报

导航