javax.swing.JOptionPane.showMessageDialog() 方法

import javax.swing.JOptionPane;


Java 8 api:

1.

public static void showMessageDialog(Component parentComponent,
                                     Object message)
                              throws HeadlessException

Brings up an information-message dialog titled "Message".

Parameters:

parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used

message - the Object to display

Throws:

HeadlessException - if GraphicsEnvironment.isHeadless returns true


2.

public static void showMessageDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType)
                              throws HeadlessException

Brings up a dialog that displays a message using a default icon determined by the messageType parameter.

Parameters:

parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used

message - the Object to display

title - the title string for the dialog

messageType - the type of message to be displayed: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE

Throws:

HeadlessException - if GraphicsEnvironment.isHeadless returns true


3.

public static void showMessageDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType,
                                     Icon icon)
                              throws HeadlessException

Brings up a dialog displaying a message, specifying all parameters.
Parameters:
parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
message - the Object to display
title - the title string for the dialog
messageType - the type of message to be displayed: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
icon - an icon to display in the dialog that helps the user identify the kind of message that is being displayed
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless returns true


source code:

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class JOptionPaneDemo extends JFrame implements ActionListener
{
	private JButton buttons[];
	private String names[] = {"show the first function",
			"show the error message dialog",
			"show the information message dialog",
			"show the warning message dialog",
			"show the question message dialog",
			"show the plain message dialog",
			"show the message dialog with icon"};
	private Container container;
	Font font = new Font("Times New Roman", Font.PLAIN, 20);
	ImageIcon bug = new ImageIcon("C:/Users/10653/Desktop/java.png");
	public JOptionPaneDemo()
	{
		super("JOptionPaneDemo");
		container = getContentPane();
		container.setLayout(new FlowLayout());
		buttons = new JButton[names.length];
		for(int count = 0; count < names.length; count++)
		{
			buttons[count] = new JButton(names[count]);
			buttons[count].setFont(font);
			buttons[count].addActionListener(this);
			container.add(buttons[count]);
		}
		setSize(800, 600);
		setVisible(true);
	}
	public void actionPerformed(ActionEvent event)
	{
		String string = event.getActionCommand();
		if(string == "show the first function")
			JOptionPane.showMessageDialog(new JFrame(), "message");
		if(string == "show the error message dialog")
			JOptionPane.showMessageDialog(new JFrame(), "error message", "error", JOptionPane.ERROR_MESSAGE);
		if(string == "show the information message dialog")
			JOptionPane.showMessageDialog(new JFrame(), "information message", "information", JOptionPane.INFORMATION_MESSAGE);
		if(string == "show the warning message dialog")
			JOptionPane.showMessageDialog(new JFrame(), "warning message", "warning", JOptionPane.WARNING_MESSAGE);
		if(string == "show the question message dialog")
			JOptionPane.showMessageDialog(new JFrame(), "question message", "question", JOptionPane.QUESTION_MESSAGE);
		if(string == "show the plain message dialog")
			JOptionPane.showMessageDialog(new JFrame(), "plain message", "plain", JOptionPane.PLAIN_MESSAGE);
		if(string == "show the message dialog with icon")
			JOptionPane.showMessageDialog(new JFrame(), "message with icon", "message", JOptionPane.INFORMATION_MESSAGE, bug);
	}
	public static void main(String srgs[])
	{
		JOptionPaneDemo application = new JOptionPaneDemo();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}


pictures of the results:


JOptionPane.showMessageDialog(new JFrame(), "message")


JOptionPane.showMessageDialog(new JFrame(), "error message", "error", JOptionPane.ERROR_MESSAGE);



JOptionPane.showMessageDialog(new JFrame(), "information message", "information", JOptionPane.INFORMATION_MESSAGE);



JOptionPane.showMessageDialog(new JFrame(), "warning message", "warning", JOptionPane.WARNING_MESSAGE);



JOptionPane.showMessageDialog(new JFrame(), "question message", "question", JOptionPane.QUESTION_MESSAGE);



JOptionPane.showMessageDialog(new JFrame(), "plain message", "plain", JOptionPane.PLAIN_MESSAGE);



JOptionPane.showMessageDialog(new JFrame(), "message with icon", "message", JOptionPane.INFORMATION_MESSAGE, bug);


tips:when brings up the message dialog by custom icon, parameter "messageType" doesn't influence the display effect. That is, whether you use "PLAIN_MESSAGE" or "ERROR_MESSAGE" or other parameters for “messageType” when you use the third override function, the display effects are same.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值