我尝试将“ main.fxml”文件显示到与其他.fxml文件连接的TapPane中。但不幸的是抛出异常。怎么了?
它是tabs.fxml的控制器:
标签类
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类在我的“ 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强制转换为javafx的View.Tabs.initialize(Tabs.java:21)上的javafx.scene.control.Tab /E:/JavaFXTest/out/production/JavaFXTest/View/tabs.fxml .fxml.FXMLLoader.load(FXMLLoader.java:2193)位于javafx.fxml.FXMLLoader.load(FXMLLoader.java:2069)位于javafx.fxml.FXMLLoader.load(FXMLLoader.java:2830)位于javafx.fxml.FXMLLoader在javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)处加载(FXMLLoader.java:2809)
错误消息告诉您问题所在:
无法将javafx.scene.layout.AnchorPane强制转换为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>
或者
您可以Tab
在控制器中创建Java代码:
@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();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句