我正在使用場景生成器2.0和月食月神。 在fxml文件中我有我的場景代碼與圖像。 如果我測試這個Eclipse的一切都還好吧,但是如果我出口這可運行罐子,然後運行這個我有窗口而不圖片... 下面是部分代碼:Javafx從罐子的圖像
<ImageView layoutX="63.0" layoutY="14.0">
<image>
<Image url="@../Images/MainPage/O.png" />
</image>
</ImageView>
文件的結構:
Graph:
MainPage:
MainPage.fxml
Images:
MainPage:
O.png
我認爲問題出在路徑上,但我不知道如何製作這條路徑。 代碼裝載機:
public class ScreensController extends StackPane{
private HashMap<String, Node> screens = new HashMap<>();
public ScreensController() {
super();
}
public void addScreen(String name, Node screen){
System.out.println("name: "+name);
System.out.println("screen: "+name);
screens.put(name, screen);
}
public Node getScreen(String name){
return screens.get(name);
}
//name - nazwa Screenu
//rescue - nazwa pliku FXML
public boolean loadScreen(String name, String rescue){
try {
System.out.println("loading Screen "+name+" path: "+rescue);
System.out.println("0001");
FXMLLoader myLoader= new FXMLLoader(getClass().getResource(rescue));
System.out.println("0002");
Parent loadScreeen = (Parent) myLoader.load();
System.out.println("0003");
ScreenControlled myScreenControler = ((ScreenControlled) myLoader.getController());
System.out.println("0004");
myScreenControler.setScreenParent(this);
System.out.println("0005");
addScreen(name, loadScreeen);//Dodanie do HashMap
System.out.println("0006");
return true;
} catch (IOException e) {
System.out.println("IOException Error in ScreenController: "+e.getMessage().substring(1,e.getMessage().length())
+"\n Cause: "+e.getCause()
+"\n Localized Message: "+e.getLocalizedMessage().substring(1,e.getLocalizedMessage().length())
+"\n Class: "+e.getClass()
+"\n ST: ");
e.printStackTrace();
return false;
}
}
public boolean setScreen(final String name){
System.out.println("Setting Screen: "+name);
if(screens.get(name) != null) { //screen loaded
final DoubleProperty opacity = opacityProperty();
if(!getChildren().isEmpty()) { //if there is more than one screen
Timeline fade = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(opacity, 1.0)),
new KeyFrame(new Duration(1000), new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t){
getChildren().remove(0); //remove the displayed screen
getChildren().add(0, screens.get(name)); //add the screen
Timeline fadeIn = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
new KeyFrame(new Duration(800), new KeyValue(opacity, 1.0)));
fadeIn.play();
}
}, new KeyValue(opacity, 0.0)));
fade.play();
}else{
setOpacity(0.0);
getChildren().add(screens.get(name)); //no one else been displayed, then just show
Timeline fadeIn = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
new KeyFrame(new Duration(1000), new KeyValue(opacity, 1.0)));
fadeIn.play();
}
return true;
}else{
System.out.println("screen hasn't been loaded!!!\n");
return false;
}
}
public boolean unloadSCreen(String name) {
if(screens.remove(name)==null) {
System.out.println("Screen " +name+ " didn't exist");
return false;
}else{
return true;
}
}
}
初始化函數:
public void start(Stage stage) throws Exception {
new Fonts().initialize();
this.stage=stage;
ScreensController mainContainer = new ScreensController();
mainContainer.loadScreen(ScreensFramework.screen2ID, ScreensFramework.screen2File);
mainContainer.setScreen(ScreensFramework.screen2ID);
root = new StackPane();//Group root = new Group();
root.getChildren().addAll(mainContainer);
Scene scene = new Scene(root);
this.stage.setScene(scene);
this.stage.show();
}
您可以添加StackTrace嗎? – ItachiUchiha
我沒有StackTrace ...:/ 它只是沒有加載 –
進入jar目錄並輸入'java -jar'並粘貼輸出到這裏。 –
ItachiUchiha