如何更改 JFileChooser 中的默认 Java 图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6994772/
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-10-30 18:11:14  来源:igfitidea点击:

How to change default java icon in JFileChooser

javaswingiconsjfilechooser

提问by Thusitha

I want to change the inbuilt java icon from JFileChooser. JFrameclass has a setIconImage()method for set icon.But I couldn't find anything like that for JFileChooser. Without changing that coffee cup anyone can easily recognize that my software is made with java. Can anyone can help me to avoid this?

我想将内置的 java 图标从JFileChooser. JFrame类有一个setIconImage()设置图标的方法。但我找不到类似的东西JFileChooser。无需更改那个咖啡杯,任何人都可以轻松识别出我的软件是用 java 制作的。任何人都可以帮助我避免这种情况吗?

回答by Petesh

IIRC the icon for the JFileChooser is taken from the jFrame that is passed in. By changing the icon for the JFrame, you should also get the reflected icon change in the JFileChooser.

IIRC JFileChooser 的图标取自传入的 jFrame。通过更改 JFrame 的图标,您还应该获得 JFileChooser 中反映的图标更改。

the code:

代码:

JFileChooser choice = new JFileChooser()
choice.showOpenDialog(parent);

The icon that is used is the icon from the parent.

使用的图标是来自父级的图标。

回答by oliholz

This could help:

这可以帮助:

JFileChooser fc = new JFileChooser(new File("C:/")){
    @Override
    protected JDialog createDialog( Component parent ) throws HeadlessException {
        JDialog dialog = super.createDialog( parent );
        BufferedImage image = new BufferedImage( 16, 16, BufferedImage.TYPE_3BYTE_BGR );
        dialog.setIconImage( image );
        return dialog;
    }
};
fc.showOpenDialog(frame);

回答by Luis

javax.swing.JFileChooser jfc = new javax.swing.JFileChooser(new java.io.File("C:/Users/Documents")) {
            @Override
            protected javax.swing.JDialog createDialog(java.awt.Component parent) throws java.awt.HeadlessException {
                javax.swing.JDialog dialog = super.createDialog(parent);

                dialog.setIconImage(new
                        javax.swing.ImageIcon("C:/Img.png").getImage());

                return dialog;

            }
        };