这有点奇怪,或者我的代码可能有问题.
我正在设置JavaFX应用程序ICON,它在Windows系统上非常清晰可见,但在Ubuntu上却没有.
在Windows上:

在Ubuntu上:

对此背后的原因有任何想法.
代码示例:
@Override
public void start(Stage stage) throws Exception {
try {
setUserAgentStylesheet(STYLESHEET_MODENA);
FXMLLoader loader = new FXMLLoader();
Parent root = (Parent) loader.load(getClass().getResourceAsStream("ui/ParentWindow.fxml"));
final ParentWindowController controller = (ParentWindowController) loader.getController();
stage.addEventHandler(WindowEvent.WINDOW_SHOWN, controller::handleWindowShownEvent);
stage.addEventHandler(WindowEvent.WINDOW_SHOWING, controller::handleWindowShowingEvent);
stage.addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, controller::handleWindowClosingRequestedEvent);
Scene scene = new Scene(root);
scene.getStylesheets().setAll(
getClass().getResource("ui/css/ParentWindow.css").toExternalForm()
);
stage.setScene(scene);
stage.initStyle(StageStyle.UNIFIED);
stage.setResizable(false);
stage.toFront();
stage.setTitle("Simple JavaFX Tool");
stage.getIcons().add(new Image(getClass().getResourceAsStream("resources/images/icon.jpg")));
stage.show();
} catch (IOException iOException) {
iOException.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道如何在javaFX舞台上设置图标.我找到了这种方法,但它没有正常工作.
stage.getIcons().add(new Image(iconImagePath));
Run Code Online (Sandbox Code Playgroud)
stage是javafx.stage.Stage的一个实例,我导入了javafx.scene.image.Image.这是我们收到的例外情况:
网址无效:找不到无效的网址或资源
此外,iconImagePath没有任何问题,它的值是"G:/test.jpg",并且G驱动器中有一个名为test的jpg文件.此外,当我们使用ImageIO读取相同的URL时,我们可以轻松完成.