我正在玩JavaFX,但有一个小问题,无法解决。
我想向现有的TabPane添加一个新的Tab(从FXML)。
这是我的代码:
try
{
URL location = WPClientController.class.getResource("WPClient.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
Node node = (Node) fxmlLoader.load(location.openStream());
Tab newTab = new Tab("engel", node);
fxTabWP.getTabs().add(newTab);
}
catch (Exception exception)
{
//no real error handling...just print the stacktrace...
exception.printStackTrace();
}
此代码将导致:
该代码有效,但是(如屏幕截图所示)添加的节点(此处为SplitPane)使用其首选大小(在TabPage上,这对我来说是错误的)。
添加的SplitPane的大小应与其父容器的大小相同。(并且当父容器增大或缩小时,它也应该增大和缩小)(例如Swing中的BorderLayout)
编辑(这是TAB FXML的[现在是正确的]内容):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.test.ui.fx.client.ClientController">
<children>
<HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<children>
<SplitPane dividerPositions="0.29797979797979796" prefHeight="160.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<items>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<TreeView fx:id="fxTreeView" prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS" />
</children>
</HBox>
<HBox fx:id="fxTreeNodeContentBox" prefHeight="100.0" prefWidth="200.0" />
</items>
</SplitPane>
</children>
</HBox>
</children>
</VBox>
我的问题:什么是待办事项,所以SplitPane的大小将动态地适应其父容器的大小?
您的FXML文件的根元素带有
<VBox maxHeight="-Infinity" maxWidth="-Infinity" ... >
将maxHeight
和maxWidth
属性阻止其增长超过首选尺寸。(此外,出于某种原因,其子元素的首选大小设置为固定值。)如果删除这些属性,它将允许根元素根据需要增长。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句