[英]JOptionPane : change the Icon
我在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
的显示..,
理想情况下,我不想有一个 Icon (因此JOptionPane.PLAIN_MESSAGE
),但我得到一个default Icon
。
我怎样才能改变这个?
杰森
使用带有图标参数的方法版本,并为图标传入 null。
例如,按我的预期工作
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();
}
}
这是@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);
没有图标,试试这个。
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
。
希望这会奏效:
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);
}
您使用的方法未指定图标。
您应该使用showConfirmDialog(Component, Object, String, int, int, Icon)
,它也可以让您传递一个Icon
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.