java 从 JOptionPane 中删除图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16981821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:36:47  来源:igfitidea点击:

Remove icon from JOptionPane

javaimageswingjoptionpaneimageicon

提问by Code Hungry

How to remove icon from JOptionPane?

如何从中删除图标JOptionPane

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

enter image description here

在此处输入图片说明

回答by Dth

You can do it by directly specifying the Look and Feel of your message.

您可以通过直接指定消息的外观来实现。

Your code will take the default one, while this one will use the "PLAIN_MESSAGE" style, which lacks the icon. The behaviour of the component remains unchanged.

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

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

More info: http://docs.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html

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

回答by Andrew Thompson

This is fairly easy by using a transparent icon as below (as opposed to the black 'splash image'). Though note that while option pane offers some 'wiggle space' in terms of how it is displayed, go to change a couple of things and it quickly becomes easier to use a JDialoginstead.

通过使用如下的透明图标(而不是黑色的“初始图像”),这相当容易。尽管请注意,虽然选项窗格在显示方式方面提供了一些“摆动空间”,但要更改一些内容,使用 a 很快就会变得更容易JDialog

Icon Free Option Pane

无图标选项窗格

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

回答by Aldrik

Write -1 in place of JOptionPane.QUESTION_MESSAGE.

用 -1 代替JOptionPane.QUESTION_MESSAGE.