6

我有这个布局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.

我怎样才能改变这个?

杰森

4

6 回答 6

8

使用带有图标参数的方法版本,并为图标传入 null。

于 2011-07-03T12:30:06.597 回答
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();
    }
}
于 2011-07-03T13:02:21.647 回答
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);
于 2013-04-05T13:46:51.940 回答
2

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

您应该使用showConfirmDialog(Component, Object, String, int , int , Icon),这使您也可以通过 an Icon

于 2011-07-03T12:32:41.137 回答
1

没有图标,试试这个。

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]
);

虽然我使用的是OptionDialog,但您可以将其更改为ConfirmDialog

于 2017-04-24T17:33:11.667 回答
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);
}
于 2017-05-13T18:15:18.953 回答