从JOptionPane中移除图标

12

如何从 JOptionPane 中移除图标?

ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
int result = JOptionPane.showConfirmDialog((Component) null, label, "ScreenPreview", JOptionPane.OK_CANCEL_OPTION);

在此输入图像描述


2
JOptionPane.PLAIN_MESSAGE ? - mishik
https://dev59.com/TGPVa4cB1Zd3GeqP65F-#10489515 - dev2d
1
@mishik:JOptionPane.PLAIN_MESSAGE 不会显示“确认”和“取消”按钮。 - Code Hungry
3个回答

25

您可以通过直接指定消息的外观来完成此操作。

您的代码将采用默认样式,而此代码将使用“PLAIN_MESSAGE”样式,该样式缺少图标。组件的行为保持不变。

JOptionPane.showConfirmDialog(null, label, "ScreenPreview", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

更多信息:http://docs.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html


2

通过使用下面的透明图标(而不是黑色的“闪屏图像”),这相当容易。尽管请注意,虽然选项窗格在显示方式上提供了一些“摆动空间”,但更改几个内容后,使用 JDialog 更容易。

免费选项窗格图标

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

class IconFree {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // A transparent image is invisible by default.
                Image image = new BufferedImage(
                        1, 1, BufferedImage.TYPE_INT_ARGB);
                JPanel gui = new JPanel(new BorderLayout());
                // ..while an RGB image is black by default.
                JLabel clouds = new JLabel(new ImageIcon(new BufferedImage(
                        250, 100, BufferedImage.TYPE_INT_RGB)));
                gui.add(clouds);

                JOptionPane.showConfirmDialog(null, gui, "Title",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        new ImageIcon(image));
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

1

JOptionPane.QUESTION_MESSAGE的位置上写入-1。


为什么是“-1”?这是什么意思?为什么不使用“JOptionPane”常量之一? - Robert
它的写法应该是这样的:JOptionPane.showMessageDialog(parent, message, status, -1);因此,-1将隐藏showMessageDialog中使用的图标。 - tonimaroni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接