showmessagedialog警告和信息消息类型在一个框架中

时间:2017-10-17 08:14:28

标签: java swing joptionpane

JOptionPane.showMessageDialog只能获得一种消息类型。如何通过JOptionPane实现与 WARNING_MESSAGE INFORMATION_MESSAGE 相同的对话?

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class OptionPaneTest implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new OptionPaneTest());
    }

    @Override
    public void run() {
        JPanel p = new JPanel(new GridLayout(2, 1, 0, 10));
        JLabel infoLabel = new JLabel("Here is my info message");
        infoLabel.setIconTextGap(10);
        infoLabel.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
        p.add(infoLabel);
        JLabel warnLabel = new JLabel("Here is my warning message");
        warnLabel.setIconTextGap(10);
        warnLabel.setIcon(UIManager.getIcon("OptionPane.warningIcon"));
        p.add(warnLabel);
        JOptionPane.showMessageDialog(null, p, "Here is my info and warn message", JOptionPane.PLAIN_MESSAGE);
        System.exit(0);
    }
}