JOptionPane:更改图标

时间:2011-07-03 12:25:07

标签: java swing joptionpane

我在JOptionPane

中有这种布局
JCheckBox checkbox = new JCheckBox("Do not show this message again.");
String message = "Level loaded successfully";  
Object[] params = {message, checkbox};  
int n = JOptionPane.showConfirmDialog(p, params, "Draft saving",
     JOptionPane.PLAIN_MESSAGE);  

除了Icon ..,

的显示外,一切都很好

理想情况下,我不希望有图标(因此JOptionPane.PLAIN_MESSAGE),但我得到default Icon

我该如何更改?

杰森

6 个答案:

答案 0 :(得分:6)

例如,按照我的预期工作

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class OptionPaneOptions {

    private JPanel options;
    private Object[] items;

    public OptionPaneOptions() {
        options = new JPanel(new GridLayout(0, 3));
        options.add(createOptionPane("Plain Message", JOptionPane.PLAIN_MESSAGE));
        options.add(createOptionPane("Error Message", JOptionPane.ERROR_MESSAGE));
        options.add(createOptionPane("Information Message", JOptionPane.INFORMATION_MESSAGE));
        options.add(createOptionPane("Warning Message", JOptionPane.WARNING_MESSAGE));
        options.add(createOptionPane("Want to do something?", JOptionPane.QUESTION_MESSAGE));
        items = new Object[]{"First", "Second", "Third"};
        JComboBox choiceCombo = new JComboBox(items);
        options.add(titled(new JOptionPane(choiceCombo,
                JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION), "Question Message"));
        JFrame frame = new JFrame("JOptionPane'Options");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(options, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    static JOptionPane createOptionPane(String message, int type) {
        JOptionPane pane = new JOptionPane(message, type);
        String title = message;
        if (type == JOptionPane.QUESTION_MESSAGE) {
            title = "Question Message";
            pane.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
        }
        return titled(pane, title);
    }

     static <T extends JComponent> T titled(T c, String title) {
        c.setBorder(BorderFactory.createTitledBorder(title));
        return c;
    }

    public static void main(String[] args) {
        OptionPaneOptions test = new OptionPaneOptions();
    }
}

答案 1 :(得分:5)

使用带有图标参数的version of the method,并为图标传入null。

答案 2 :(得分:3)

这是@Jesse Barnum谈论的格式

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

您向int messageType提供了也确定图标

的messageType常量

示例:此图标使用JOptionPane.QUESTION_MESSAGE

输出问题图标
JOptionPane.showMessageDialog(null,"Are you OK?","HI",JOptionPane.QUESTION_MESSAGE,null);

这个输出信息图标

JOptionPane.showMessageDialog(null,"You are reading something","FYI",JOptionPane.INFORMATION_MESSAGE,null);

答案 3 :(得分:2)

您使用的方法未指定图标。

您应该使用showConfirmDialog(Component, Object, String, int , int , Icon),这样您也可以传递Icon

答案 4 :(得分:1)

To Have no Icon Try this. String[] options = {"Ok", "Cancel"};

    int choices = JOptionPane.showOptionDialog(Ann.this, "Do you Want To Exit Calculator ", "Exit Calculator", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,null,options, options[0]);

Though am using OptionDialog, You Can Change it to Confirm Dialog

答案 5 :(得分:1)

希望这会奏效:

    Image icon = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    int option=JOptionPane.showConfirmDialog(null, "Do you really want to Exit Alarm Clock - By jLab", "Alarm Clock - By jLab", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,icon);
        if(option==JOptionPane.YES_OPTION){
            System.exit(0);
        }else{

        }
相关问题