我对JavaFX教程中的项目有一点问题,我有三个FXML文件,其中一个“ Main”包含边框窗格,第二个包含Toggle Buttons和Button,我想使用Button在Main Central中打开第三个FXML文件边框窗格,但是当我尝试单击按钮时发生错误。
这是我的代码
@FXML
private BorderPane mainBorderPane;
@FXML
private ToggleButtonLibriaryController toggleButtonLibriaryController;
@FXML
public void initialize() {
toggleButtonLibriaryController.setMainController(this);
}
public void setCenter() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("addBookMenu.fxml"));
Pane pane = loader.load();
mainBorderPane.setCenter(pane);
}
还有按钮控制器
private MainController mainController;
@FXML
private ToggleGroup ToggleLibriary;
@FXML
private HBox ToggleButtonLibriary = new HBox();
@FXML
public void OpenLibriary() {
System.out.println("biblioteka");
@FXML
private Button addBookButton = new Button();
@FXML
private ToggleButton LibriaryToggleButton = new ToggleButton();
@FXML
private ToggleButton BookListToogleButton = new ToggleButton();
@FXML
private ToggleButton StatisticToogleButton = new ToggleButton();
@FXML
public void OpenBookList() {
System.out.println("ksiazki");
}
@FXML
public void OpenStatics() {
System.out.println("Statystyka");
}
public void setMainController(MainController mainController) {
this.mainController = mainController;
}
@FXML
public void addBook() throws IOException{
if (ToggleLibriary.getSelectedToggle() != null) {
ToggleLibriary.getSelectedToggle().setSelected(false);
}
mainController.setCenter();
}
我将非常感谢您的帮助
我的FXML文件
<BorderPane fx:id="mainBorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" pickOnBounds="false" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.MainController."> <top>
<VBox BorderPane.alignment="CENTER">
<fx:include fx:id="toggleButtonLibriary" source="ToggleButtonLibriary.fxml" />
</VBox>
还有我的第二名
<HBox fx:id="ToggleButtonLibriary" spacing="10.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.ToggleButtonLibriaryController">
<children>
<ToggleButton fx:id="LibriaryToggleButton" mnemonicParsing="false" onAction="#OpenLibriary" prefHeight="60.0" prefWidth="130.0" text="%library">
<toggleGroup>
<ToggleGroup fx:id="ToggleLibriary" />
</toggleGroup>
</ToggleButton>
<ToggleButton fx:id="BookListToogleButton" mnemonicParsing="false" onAction="#OpenBookList" prefHeight="60.0" prefWidth="130.0" text="%list.books" toggleGroup="$ToggleLibriary" />
<ToggleButton fx:id="StatisticToogleButton" mnemonicParsing="false" onAction="#OpenStatics" prefHeight="60.0" prefWidth="130.0" text="%stats" toggleGroup="$ToggleLibriary" />
<Button fx:id="addBookButton" mnemonicParsing="false" onAction="#addBook" prefHeight="60.0" prefWidth="130.0" text="%add_book">
<graphic>
<ImageView fitHeight="34.0" fitWidth="41.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/Icons/indeks.png" />
</image>
</ImageView>
</graphic>
</Button>
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>