Mic*_*rry 219
假设您的舞台是"舞台"并且文件位于文件系统上:
stage.getIcons().add(new Image("file:icon.png"));
Run Code Online (Sandbox Code Playgroud)
根据下面的评论,如果它包含在一个包含的jar中,你需要使用以下方法:
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("icon.png")));
Run Code Online (Sandbox Code Playgroud)
小智 72
我试过这个,它完全有效.代码是:
stage.getIcons().add(
new Image(
<yourclassname>.class.getResourceAsStream( "icon.png" )));
Run Code Online (Sandbox Code Playgroud)
icon.png与源文件位于同一文件夹下.
Mad*_*ota 67
启动器的完整程序:)此程序设置堆栈溢出图标.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
输出Screnshot
针对JavaFX 8进行了更新
无需更改代码.它仍然可以正常工作.在Java 1.8(1.8.0_45)中经过测试和验证.路径可以设置为本地或远程都支持.
stage.getIcons().add(new Image("/path/to/javaicon.png"));
Run Code Online (Sandbox Code Playgroud)
要么
stage.getIcons().add(new Image("https://example.com/javaicon.png"));
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.谢谢!!
小智 8
你可以在fxml中添加它.阶段水平
<icons>
<Image url="@../../../my_icon.png"/>
</icons>
Run Code Online (Sandbox Code Playgroud)
如果您有一个图像文件夹并保存图标,请使用此文件夹
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/images/comparison.png")));
Run Code Online (Sandbox Code Playgroud)
如果你直接在你的包装中使用它,这不是一个好习惯,请使用它
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("comparison.png")));
Run Code Online (Sandbox Code Playgroud)
如果你有一个文件夹结构,你的图标在里面使用
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("../images/comparison.png")));
Run Code Online (Sandbox Code Playgroud)