开发者

How to set the default icon for all Java Swing windows?

开发者 https://www.devze.com 2023-01-06 00:20 出处:网络
How to set the default icon for all Java Swing windows? Otherwise i have to set icons for every frame i created.

How to set the default icon for all Java Swing windows?

Otherwise i have to set icons for every frame i created.

What's 开发者_C百科your suggestions? Simple hackings are also accepted.

many thx

Update: Best if the method you suggested can leave existing frame-creation codes untouched. thx


Create an Abstact class that extends JFrame

In the constructor set your icon.

create child class that extends your new Abstract Class and call super in your constructor

public abstract class MainFrame extends JFrame {
    protected MainFrame() {
        this.setIconImage(null); // Put your own image instead of null
    }
}

public class ChildFrame extends MainFrame {
    public ChildFrame() {
        super();
    }
}

You can also just create object from your new class

public class MainFrame extends JFrame {
    public MainFrame() {
        this.setIconImage(null); // Put your own image instead of null
    }
}

public class Frame {

    private MainFrame mainframe = new MainFrame();

    public Frame() {
        super();
    }
}


To make windows icons changes globally without changing old code I am using this code snippet

public static void fixWindowsIcons(final List<Image> iconImages) {
    PropertyChangeListener l = new PropertyChangeListener() {

        private Window prevActiveWindow;

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            final Window o = KeyboardFocusManager.getCurrentKeyboardFocusManager()
                    .getActiveWindow();
            if (o != null && prevActiveWindow != o) {
                prevActiveWindow = o;
                List<Image> windowIcons = o.getIconImages();
                if (windowIcons == null || windowIcons.size() == 0) {
                    o.setIconImages(iconImages);
                }
            }
        }
    };
    KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .addPropertyChangeListener("activeWindow", l); //$NON-NLS-1$
}
0

精彩评论

暂无评论...
验证码 换一张
取 消