JavaFX - 选项卡中的 FXML

JavaFX - FXML in Tab

我尝试将我的 "main.fxml" 文件显示到与我的其他 .fxml 文件连接的 TapPane 中。但不幸的是抛出异常。怎么了?


tabs.fxml的控制器:

制表符 class

package View;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class Tabs implements Initializable {

    @FXML
    TabPane tabPane = null;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        try {
            tabPane.getTabs().addAll((Tab) FXMLLoader.load(this.getClass().getResource("main.fxml")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

tabs.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane
    maxHeight="-Infinity"
    maxWidth="-Infinity"
    minHeight="-Infinity"
    minWidth="-Infinity"
    prefHeight="400.0"
    prefWidth="600.0"
    xmlns="http://javafx.com/javafx/8"
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="View.Tabs">
   <children>
      <TabPane
        fx:id="tabPane"
        prefHeight="400.0"
        prefWidth="600.0"
        tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="Untitled Tab 1" >
            <content>

            </content>
          </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

main.fxml。它包含一个表单和一个按钮。我尝试通过 Tab class

在我的“tab.fxml”中显示此表单
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane
    id="main"
    maxHeight="-Infinity"
    maxWidth="-Infinity"
    minHeight="-Infinity"
    minWidth="-Infinity"
    prefHeight="400.0"
    prefWidth="600.0"
    xmlns="http://javafx.com/javafx/8"
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="View.Chat">
   <children>
  <AnchorPane
        minHeight="0.0"
        minWidth="0.0"
        prefHeight="180.0"
        prefWidth="200.0" />
      <TextArea
        fx:id="text"
        layoutX="14.0"
        layoutY="317.0"
        prefHeight="69.0"
        prefWidth="435.0" />
      <TextArea
        fx:id="chatField"
        editable="false"
        layoutX="14.0"
        layoutY="14.0"
        prefHeight="289.0"
        prefWidth="435.0" />
      <Button
        fx:id="send"
        layoutX="486.0"
        layoutY="334.0"
        mnemonicParsing="false"
        onAction="#sendMessage"
        prefHeight="30.0
        prefWidth="66.0"
        text="Send" />
   </children>
</AnchorPane>

但是抛出异常:

javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.control.Tab /E:/JavaFXTest/out/production/JavaFXTest/View/tabs.fxml at View.Tabs.initialize(Tabs.java:21) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2193) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2069) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2830) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)

报错信息告诉你问题所在:

javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.control.Tab

您的 tab.fxml 文件的根元素是 AnchorPane,但您试图将其视为 Tab:

(Tab) FXMLLoader.load(this.getClass().getResource("main.fxml"))

您可以更改 FXML 文件,使根元素成为制表符:

<Tab xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="View.Chat">

<AnchorPane> id="main" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" >
   <children>
  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
      <TextArea fx:id="text" layoutX="14.0" layoutY="317.0" prefHeight="69.0" prefWidth="435.0" />
      <TextArea fx:id="chatField" editable="false" layoutX="14.0" layoutY="14.0" prefHeight="289.0" prefWidth="435.0" />
      <Button fx:id="send" layoutX="486.0" layoutY="334.0" mnemonicParsing="false" onAction="#sendMessage" prefHeight="30.0" prefWidth="66.0" text="Send" />
   </children>
</AnchorPane>
</Tab>

您可以在控制器的 Java 代码中创建 Tab

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    try {
        Tab tab = new Tab();
        tabPane.getTabs().add(tab);
        tab.setContent((Node) FXMLLoader.load(this.getClass().getResource("main.fxml")));
    } catch (IOException e) {
        e.printStackTrace();
    }
}