【发布时间】:2022-11-23 23:44:13
【问题描述】:
我有一个 TabPane 声明如下:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TabPane?>
<TabPane fx:id="rootNode" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@dark_theme.css" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.AppController" />
我想从我的控制器添加标签。所以我这样做:
jsonConfig.getAvailableChannelIds().forEach( chId -> {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("tab_item.fxml"));
Tab item = fxmlLoader.load();
item.setText(String.format("%d", chId));
rootNode.getTabs().add(item);
}catch (Exception e) {
e.printStackTrace();
}
});
“tab_item.fxml”看起来如下:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.layout.VBox?>
<Tab xmlns:fx="http://www.w3.org/1999/XSL/Transform">
<VBox>
<fx:include source="test.fxml"/>
</VBox>
</Tab>
最后是“test.fxml”:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: red;" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" />
这就是我所拥有的:
我缺少什么来用红色方块填充 Tab 内容?
【问题讨论】:
-
我的猜测是
test.fxml中的AnchorPane需要maxHeight和maxWidth设置为Infinity。 -
您将首选尺寸明确设置为 600x400。你希望发生什么/
-
不相关:此
xmlns:fx="http://www.w3.org/1999/XSL/Transform"不是 FXML 的名称空间。您提供的 XML 命名空间用于 XSL transforms,这根本不是您的文档所使用的。而是使用xmlns:fx="http://javafx.com/fxml"。在这种情况下,命名空间只对 XML 验证(您没有使用)和帮助工具(如 IDE)提供更好的编辑帮助很重要。 -
如果你也摆脱了最小和最大尺寸,那么允许锚窗格增长,然后你可以告诉
VBox使test.fxml中的锚窗格增长到它喜欢的大小,使用通常的@ 987654340@设置。不过,我真的不明白为什么你在那里有VBox。