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:

javax.swing.JOptionPane.showMessageDialog() 方法_第1张图片


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

javax.swing.JOptionPane.showMessageDialog() 方法_第2张图片


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

javax.swing.JOptionPane.showMessageDialog() 方法_第3张图片


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

javax.swing.JOptionPane.showMessageDialog() 方法_第4张图片


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

javax.swing.JOptionPane.showMessageDialog() 方法_第5张图片


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

javax.swing.JOptionPane.showMessageDialog() 方法_第6张图片


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

javax.swing.JOptionPane.showMessageDialog() 方法_第7张图片


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

javax.swing.JOptionPane.showMessageDialog() 方法_第8张图片

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.

你可能感兴趣的:(Java,方法)