简体   繁体   中英

Remove icon from JOptionPane

How to remove icon from JOptionPane ?

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

在此输入图像描述

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.

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

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 JDialog instead.

图标免费选项窗格

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM