ToolBar.java
package util;
import javafx.scene.Parent;
import javafx.scene.layout.BorderPane;
public class ToolBar {
private static BorderPane borderPane;
public void setBorderPane(BorderPane borderPane) {
this.borderPane = borderPane;
}
public static void initialize(BorderPane borderPane) {
ToolBar toolBar = new ToolBar();
toolBar.setBorderPane(borderPane);
}
public static void show(Parent parent) {
borderPane.setLeft(parent);
}
public static void hide() {
borderPane.setLeft(null);
}
}
HomeController.java
package main;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import util.MainContent;
import util.ToolBar;
import java.net.URL;
import java.util.ResourceBundle;
public class HomeController implements Initializable {
@FXML
private Button editorButton;
@FXML
private BorderPane borderPane;
private MainContent mainContent;
@Override
public void initialize(URL location, ResourceBundle resources) {
mainContent = new MainContent(borderPane);
}
@FXML
private void goToEditor() throws Exception {
mainContent.show(getClass().getResource("editor.fxml"));
ToolBar.show(FXMLLoader.load(getClass().getResource("toolBar.fxml")));
}
}
我正在尝试使用工具栏按钮来访问边框。例如将新内容加载到父边框窗格,但将setCenter加载到另一个fxml文件。
我尝试使用带有
工具栏的元素borderpane = (BorderPane) toolBar.getParent();
borderpane.setCenter("someFXML.fxml");
但是出现了NullPointerException
错误,有什么建议吗?感谢。