我試圖在主fxml中加載一個fxml文件(內容區域)。JAVAFX - 在main fxml中加載使用anchorpane的fxml
由於代碼被幾個文件分裂,我不能在這裏放置所有的代碼,但如果需要,我會用這個問題構建一個小版本。我只放置代碼的相關部分。
主要FXML文件
...
</MenuBar>
<AnchorPane fx:id="mainContent" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS" />
<ToolBar prefHeight="25.0" VBox.vgrow="NEVER" />
</children>
</VBox>
內容FXML(參加重視anchorpane ID)
...
<AnchorPane id="background" prefHeight="600.0" prefWidth="800.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<Label minWidth="400.0" prefWidth="423.0" text="Tools" textAlignment="CENTER" textFill="WHITE" textOverrun="CLIP" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
<effect>
<Glow level="1.0" />
</effect>
<font>
<Font name="Verdana Bold" size="45.0" />
</font>
</Label>
<Label text="Version 0.01 alfa" textFill="WHITE" AnchorPane.rightAnchor="140.0" AnchorPane.topAnchor="69.0">
<font>
<Font name="Verdana" size="20.0" />
</font>
</Label>
<Label text="2014 - José Longo" textFill="WHITE" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" />
</children>
<effect>
<InnerShadow />
</effect>
</AnchorPane>
MainApp.java
...
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
AnchorPane mainScreen = (AnchorPane) loader.load(MainApp.class.getResource("/net/somewhere/fxml/Scene.fxml"));
Scene scene = new Scene(mainScreen);
scene.getStylesheets().add("/net/somewhere/styles/Styles.css");
FXMLController mainController = loader.getController();
ScreenNavigator.setMainControler(mainController);
ScreenNavigator.setContent(ScreenNavigator.BACKGROUND); // if i remove/comment this the stage apears ok
stage.setTitle("Tools");
stage.setScene(scene);
stage.show();
}
控制器類
public void setScreen(AnchorPane anchorPane){
mainContent.getChildren().clear();
mainContent.getChildren().addAll(anchorPane);
}
內容控制器
public class ScreenNavigator {
//fxml files
public static final String BACKGROUND = "/net/somewhere/fxml/Background.fxml";
private static FXMLController mainController;
public static void setMainControler(FXMLController mainController){
ScreenNavigator.mainController = mainController;
}
public static void setContent(String fxmlFile){
try {
URL url = ScreenNavigator.class.getResource(fxmlFile);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(url);
loader.setBuilderFactory(new JavaFXBuilderFactory());
AnchorPane content = (AnchorPane) loader.load(url.openStream());
System.out.println(content.getId()); //not null the object i can get the id
mainController.setScreen(content); // i get java.lang.NullPointerException at net.somewhere.controllers.ScreenNavigator.setContent(ScreenNavigator.java:40)
} catch (IOException ex) {
Logger.getLogger(ScreenNavigator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
當嘗試運行我得到這個錯誤:
--- exec-maven-plugin:1.2.1:exec (default-cli) @ Tools ---
background <---- i can get the id of anchorpane
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at net.somewhere.controllers.ScreenNavigator.setContent(ScreenNavigator.java:40) <----
at net.somewhere.MainApp.start(MainApp.java:30)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
... 1 more
我試圖用的getResource()的getResourceAsStream()與節點,面板等...最後以在這裏的一個例子在這個網站結束,但沒有成功...總是得到這種類型的錯誤...
有人能指點我解決這個問題的方向嗎?
TIA
JL
什麼是 「Scene.fxml」 在MainApp?這個異常說明ScreenNavigator中的「mainController」爲null,這意味着FXMLController mainController = loader.getController();一片空白。檢查爲什麼loader.getController()返回null?加載錯誤的fxml文件? FXML中沒有指定fx:控制器? –
謝謝Uluk ...我會努力... – JLongo