JavaFX部署 - 图像丢失 [英] JavaFX deployment - images get lost
问题描述
任何想法可能是什么原因?或者甚至有更好的方式发布完成的java项目?
这似乎是一个常见的问题。我自己挣扎着看看我如何引用css和css中的图像。
这是一个在开发环境,Scene Builder和打包的JAR中有效的解决方案。
文件夹结构:
Main.java:
包应用程序;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage){
try {
FXMLLoader loader = new FXMLLoader(Main.class.getResource(view / RootLayout.fxml));
AnchorPane rootLayout =(AnchorPane)loader.load();
场景场景=新场景(rootLayout,400,400);
scene.getStylesheets()。add(getClass()。getResource(css / application.css)toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e){
e.printStackTrace();
}
}
public static void main(String [] args){
launch(args);
}
}
RootLayout.fxml:
<?xml version =1.0encoding =UTF-8?>
<?import java.lang。*?>
<?import javafx.scene.control。*?>
<?import javafx.scene.layout。*?>
<?import javafx.scene.layout.AnchorPane?>
< AnchorPane xmlns =http://javafx.com/javafx/8xmlns:fx =http://javafx.com/fxml/1fx:controller =application view.RootLayoutController>
< children>
< Pane layoutX =0.0layoutY =0.0prefHeight =200.0prefWidth =200.0>
< children>
< Button fx:id =sunButtonlayoutX =74.0layoutY =88.0mnemonicParsing =falseonAction =#handleSunButtonClickstyleClass =sun-buttonstylesheets =@ .. / css /toolbar.csstext =Button/>
< / children>
< / Pane>
< / children>
< / AnchorPane>
RootLayoutController.java:
package application.view;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class RootLayoutController {
@FXML
按钮sunButton;
@FXML
public void handleSunButtonClick(){
System.out.println(Button clicked);
}
}
toolbar.css:
.sun-button {
-fx-graphic:url('./ icons / sun.png');
}
application.css:
.root {
-fx-background-color:lightgray;
}
sun.png:
在开发环境中和打包JAR时(选择在Eclipse中将所需的库解压缩到生成的JAR)。
截图(只是一个加载图标的按钮css)
I finished a small private project in JavaFX with e(fx)clipse. Now I would like to export it as a runnable jar file. Everything works fine, except the fact that the pane and button background images get lost. The paths to these images were defined in a seperate CSS file. Other definitions from this file are implemented well, only the images are missing. Any idea what could be the reason for this? Or is there even a better way to publish a finished java project?
This seems to be a common problem. I struggled with it myself. Take a look at how I reference the css and the image in the css.
Here's a solution that works in the development environment, in Scene Builder and in a packaged JAR.
The folder structure:
Main.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/RootLayout.fxml"));
AnchorPane rootLayout = (AnchorPane) loader.load();
Scene scene = new Scene(rootLayout, 400, 400);
scene.getStylesheets().add(getClass().getResource("css/application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
RootLayout.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.view.RootLayoutController">
<children>
<Pane layoutX="0.0" layoutY="0.0" prefHeight="200.0" prefWidth="200.0">
<children>
<Button fx:id="sunButton" layoutX="74.0" layoutY="88.0" mnemonicParsing="false" onAction="#handleSunButtonClick" styleClass="sun-button" stylesheets="@../css/toolbar.css" text="Button" />
</children>
</Pane>
</children>
</AnchorPane>
RootLayoutController.java:
package application.view;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class RootLayoutController {
@FXML
Button sunButton;
@FXML
public void handleSunButtonClick() {
System.out.println( "Button clicked");
}
}
toolbar.css:
.sun-button {
-fx-graphic: url('./icons/sun.png');
}
application.css:
.root {
-fx-background-color:lightgray;
}
sun.png:
This works in both the development environment and when you package the JAR (choose "Extract required libraries into generated JAR" in Eclipse).
Screenshot (just a button with an icon loaded via css)
这篇关于JavaFX部署 - 图像丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!