是否可以使用JavaFX更改应用程序图标,还是必须使用Swing?


当前回答

您可以在fxml中添加它。阶段水平

<icons>
    <Image url="@../../../my_icon.png"/>
</icons>

其他回答

使用这段代码行,您可以轻松地将图标放到应用程序中

stage.getIcons().add(new Image(“image path”) );

这个程序为StackOverflowIcon设置图标。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class StackoverflowIcon extends Application {

    @Override
    public void start(Stage stage) {
        StackPane root = new StackPane();
        // set icon
        stage.getIcons().add(new Image("/path/to/stackoverflow.jpg"));
        stage.setTitle("Wow!! Stackoverflow Icon");
        stage.setScene(new Scene(root, 300, 250));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

输出Screnshot

为JavaFX 8更新

不需要更改代码。它仍然可以正常工作。在Java 1.8(1.8.0_45)中测试和验证。路径可设置为本地或远程均支持。

stage.getIcons().add(new Image("/path/to/javaicon.png"));

OR

stage.getIcons().add(new Image("https://example.com/javaicon.png"));

希望能有所帮助。谢谢! !

我试过了,效果不错:

stage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));

你认为如何创建新的包,即图像。图标在你的SRC目录和移动到那里你。png图像?那么你只需要写:

Image image = new Image("/image/icons/nameOfImage.png");
primaryStage.getIcons().add(image);

这个解决方案非常适合我,但我仍然不确定它是否正确(这里是初学者)。

如果运行jar文件,Michael Berry指定的代码将改变标题栏和任务栏中的图标。快捷图标不可更改。

如果你运行一个用com编译的本地程序。zenjava,你必须添加一个链接到程序图标:

<plugin>
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>8.8.3</version>
    <configuration>
    ...
        <bundleArguments>
            <icon>${project.basedir}/src/main/resources/images/filename.ico</icon>
        </bundleArguments>
    </configuration>
</plugin>

这将为快捷方式和任务栏添加一个图标。