JavaFX的。为标题栏和操作系统任务栏设置不同的图标 [英] JavaFX. Set different icons for the title bar and the operating system task bar

查看:2483
本文介绍了JavaFX的。为标题栏和操作系统任务栏设置不同的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaFX中是否有办法为标题栏和操作系统任务栏设置不同的应用程序图标?

Is there a way in JavaFX to set different application icons for the title bar and for the operating system task bar?

问题是系统任务栏中显示的图标与标题栏中的图标相比要大得多,系统无法正确调整它们的大小。

The problem is that the icon shown in the system task bar is much bigger compare to the icon in the title bar and they cannot be re-sized properly by the system.

我想为不同的图标大小使用不同的图像。与您在.ico文件中的操作类似。

I would like to use different images for the different icon sizes. Similar to what you do in an .ico file.

如果我调用 stage.getIcons()。add(...)两次,前一个图像将始终用于两个条形。

If I call stage.getIcons().add(...) two times, the former image will be always used for both bars.

我也无法使用已存在的.ico文件(支持不同为此目的。

I was also not able to use an already existing .ico file (that supports different sizes) for this purposes.

推荐答案

有一种方法可以使用两个不同的阶段,但它可能有它的问题(仅在Windows 7的)。以下示例使用Java 8 / JavaFX 8.

There is a way by using two different stages but it may have it's problems (only tested on Windows 7). The following example uses Java 8/JavaFX 8.

此代码设置在JavaFX启动时收到的主阶段任务栏上显示的图标,但使阶段不可见(透明,零尺寸)。然后它会打开一个带有不同图标的新的可见窗口。

This code sets the icon that is shown on the taskbar on the primary stage received on JavaFX startup but makes the stage invisible (transparent, zero size). It then opens a new and visible window with a different icon.

由于这只是一个子窗口而不是真实窗口,因此必须将hide事件委托给隐藏阶段关闭应用程序。可能会有更多事件需要委派,例如最小化窗口。

Since this is only a child window and not the real one, the hide event has to be delegated to the hidden stage to close the application. There may be more events that have to be delegated like minimizing the window.

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.getIcons().add(getImage("taskbar_icon.png"));
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setWidth(0);
        primaryStage.setHeight(0);
        primaryStage.show();

        Stage visibleStage = new Stage();
        visibleStage.initOwner(primaryStage);
        visibleStage.getIcons().add(getImage("window_icon.png"));
        visibleStage.setScene(new Scene(...));
        visibleStage.setOnHidden(e -> Platform.runLater(primaryStage::hide));
        visibleStage.show();
    }
}

这篇关于JavaFX的。为标题栏和操作系统任务栏设置不同的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆