如何为所有 Java Swing 窗口设置默认图标?

发布于 2024-09-08 03:50:39 字数 162 浏览 1 评论 0原文

如何为所有 Java Swing 窗口设置默认图标?

否则我必须为我创建的每个框架设置图标。

你有什么建议? 简单的黑客攻击也被接受。

多谢

更新:如果您建议的方法可以保持现有的框架创建代码不变,那就最好了。谢谢

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

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

What's your suggestions?
Simple hackings are also accepted.

many thx

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不回头走下去 2024-09-15 03:50:39

创建一个扩展 JFrame 的抽象类

在构造函数中设置您的图标。

创建扩展新的Abstract Class的子类并在构造函数中调用super

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

您也可以从新类创建对象

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

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();
    }
}
我不咬妳我踢妳 2024-09-15 03:50:39

为了使 Windows 图标全局更改而不更改旧代码,我使用此代码片段

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$
}

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$
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文