我是javafx新手,我正在尝试建立一个通过menuitem动作加载新场景的项目。
这是fxml菜单(“已使用场景生成器创建所有fxml”):
<HBox alignment="CENTER_RIGHT" prefHeight="50.0" prefWidth="200.0">
<children>
<Button fx:id="btnLogin" mnemonicParsing="false" onAction="#btnLoginClicked" text="Login" />
<MenuButton fx:id="btnMenu" alignment="CENTER_RIGHT" mnemonicParsing="false" text="MenuButton">
<items>
<MenuItem fx:id="btnLoadArticle" mnemonicParsing="false" onAction="#btnLoadArticleClicked" text="Load article from file" />
<MenuItem fx:id="btnNewArticle" mnemonicParsing="false" onAction="#btnNewArticleClicked" text="New Article" />
<MenuItem fx:id="btnEditArticle" mnemonicParsing="false" onAction="#btnEditArticleClicked" text="EditArticle" />
<MenuItem fx:id="btnDeleteArticle" mnemonicParsing="false" onAction="#btnDeleteArticleClicked" text="Delete article" />
<MenuItem fx:id="btnExit" mnemonicParsing="false" onAction="#btnExitClicked" text="Exit" />
</items>
<HBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</HBox.margin>
</MenuButton>
</children>
<VBox.margin>
<Insets bottom="10.0" left="20.0" right="20.0" />
</VBox.margin>
</HBox>
这是单击“btnediticle”时的控制器代码(其中显示菜单):
@FXML
private void btnEditArticleClicked(ActionEvent event) {
if (selectedArticle != null) {
try {
ArticleEditController editController = new ArticleEditController(this);
editController.setArticle(selectedArticle);
MenuItem eventOrigin = (MenuItem) event.getSource();
ContextMenu contextMenu = eventOrigin.getParentPopup();
contextMenu.getOwnerWindow().getScene().setRoot(editController.getContent());
} catch (Exception e) {
e.printStackTrace();
}
} else {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Want to edit an article?");
alert.setHeaderText(null);
alert.setContentText("An article from the list must be selected in order to edit it.");
alert.showAndWait();
}
}
这是我在场景改变时执行的代码:
private Pane root;
public ArticleEditController(NewsReaderController newsReaderController) {
this.newsReaderController = newsReaderController;
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource(AppScenes.EDITOR.getFxmlFile()));
loader.setController(this);
root = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
public Pane getContent() {
return root;
}
当创建场景时,我得到一个nullpointerexception,但是之后内容会毫无问题地显示出来。这是我得到的唯一信息:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:222)
at javafx.scene.control.skin.MenuButtonSkinBase.lambda$new$7(MenuButtonSkinBase.java:206)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)
希望你们任何人都能帮我。我已经检查过这个问题了,但是给出的答案和我需要的不一样。
提前谢谢!
编辑我使用main.java来显示如下场景:
@Override
public void start(Stage primaryStage) {
newsReaderController = new NewsReaderController();
newsReaderController.setMainController(this);
Pane root = newsReaderController.getContent();
setConnection();
Scene scene = new Scene(root);
primaryStage.setTitle("Newspaper");
primaryStage.initStyle(StageStyle.DECORATED);
primaryStage.setScene(scene);
primaryStage.show();
}
暂无答案!
目前还没有任何答案,快来回答吧!