JavaFX - 选项卡中的 FXML

标签 java tabs javafx

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

<小时/>

它是tabs.fxml的 Controller :

选项卡类

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>

它是ma​​in.fxml。它包含一个表单和一个按钮。我尝试通过 Tab 类在我的“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>

您可以在 Controller 中用 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();
    }
}

关于JavaFX - 选项卡中的 FXML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27910838/

相关文章:

德尔福:毛玻璃片

多种对象类型的 JavaFX TreeView? (和更多)

java - 为什么有些节点有 x 和 y 位置而其他节点没有

java - 我正在尝试使用单击的按钮来计算数组中一列数字的平均值

java - 如何在 servlet 操作中同步请求

java - 如何通过输入将尽可能多的元素添加到数组列表中?

java - Glassfish 服务器日志泛滥并中断空闲线程

c++ - 在C++中将制表符分隔的文件读取到数组中

Python:在写入 csv 文件时避免列的覆盖/不可读

css - 垂直对齐 CheckboxTableCell 的复选框