我正在研究JavaFX应用程序。我想写一个记事本。现在我正在我的菜单栏上工作。这是我的Menu.fxml文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<MenuBar id="menuBar" fx:id="menuBar" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.menu.MenuController">
<menus>
<Menu id="fileMenu" fx:id="fileMenu" mnemonicParsing="false" text="File">
<items>
<MenuItem id="newFileItem" fx:id="newFileItem" text="New Ctrl+N" />
<MenuItem id="openFileItem" fx:id="openFileItem" text="Open Ctrl+O" onAction="#openFile" />
<MenuItem id="saveFileItem" fx:id="saveFileItem" text="Save Ctrl+S" />
<MenuItem id="saveFileAsItem" fx:id="saveFileAsItem" text="Save file as... Ctrl+S"/>
</items>
</Menu>
<Menu id="editionMenu" fx:id="editionMenu" mnemonicParsing="false" text="Edit">
</Menu>
<Menu id="settingsMenu" fx:id="settingsMenu" mnemonicParsing="false" text="Settings">
</Menu>
<Menu id="helpMenu" fx:id="helpMenu" mnemonicParsing="false" text="Help">
</Menu>
</menus>
</MenuBar>
我想为每个菜单编写一个通用控制器和几个专用控制器,这样我就没有一个负责文件管理,设置,编辑等的大控制器。 这是MenuController:
package controllers.menu;
import javafx.fxml.FXML;
import javafx.scene.control.MenuBar;
public class MenuController {
@FXML
private MenuBar menuBar;
private FileMenuController fileMenuController;
private EditMenuController editMenuController;
private SettingsMenuController settingsMenuController;
private HelpMenuController helpMenuController;
public MenuController() {
fileMenuController = new FileMenuController();
fileMenuController.setMenuBar(menuBar);
}
public void openFile(){
fileMenuController.openFile();
}
}
这是FileMenuController:
package controllers.menu;
import javafx.scene.control.MenuBar;
import javafx.stage.FileChooser;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
class FileMenuController {
private MenuBar menuBar;
public void openFile() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("*.txt", "*.txt"),
new FileChooser.ExtensionFilter("All types", "*.*")
);
File file = fileChooser.showOpenDialog(menuBar.getScene().getWindow());
}
public void setMenuBar(MenuBar menuBar) {
this.menuBar = menuBar;
}
}
当我在文件菜单中选择打开选项时,我遇到了以下例外情况:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
at controllers.menu.FileMenuController.openFile(FileMenuController.java:23)
at controllers.menu.MenuController.openFile(MenuController.java:19)
任何人都可以给我一些建议如何解决这个问题?是否有更好的方法来分割菜单控制器,以便每个控制器只负责一件事?
答案 0 :(得分:1)
构造函数在menuBar设置之前被调用,因此MenuController应该使用Initalizable接口中的initalize方法。 修复了MenuController:
package controllers.menu;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuBar;
import java.net.URL;
import java.util.ResourceBundle;
public class MenuController implements Initializable{
@FXML
private MenuBar menuBar;
private FileMenuController fileMenuController;
private EditMenuController editMenuController;
private SettingsMenuController settingsMenuController;
private HelpMenuController helpMenuController;
public void openFile(){
fileMenuController.openFile();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
fileMenuController = new FileMenuController();
fileMenuController.setMenuBar(menuBar);
}
}