javax.swing.JOptionPane.showMessageDialog() 方法

来源:互联网 发布:php 大端 编辑:程序博客网 时间:2024/06/06 14:17

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 0
原创粉丝点击