JavaFX - TabPane/Tab 如何添加填充选项卡大小的内容?

2024-02-22


我正在使用 JavaFX,遇到一个小问题,但无法解决。
我想向现有 TabPane 添加一个新选项卡(来自 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" ... >

The maxHeight and maxWidth属性阻止它增长超出其首选大小。 (此外,由于某种原因,其子元素的首选大小被设置为固定值。)如果删除这些属性,它将允许根元素根据需要增长。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JavaFX - TabPane/Tab 如何添加填充选项卡大小的内容? 的相关文章

随机推荐