首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java fxml :将fxml宽度绑定到不同的fxml宽度

Java fxml :将fxml宽度绑定到不同的fxml宽度
EN

Stack Overflow用户
提问于 2018-07-12 03:34:48
回答 1查看 392关注 0票数 0

我有一个TabPane FXML文件,当您按下一个按钮时,它会添加一个新的选项卡,从另一个fxml文件中获取它的内容。如何将选项卡的宽度绑定到选项卡窗格的宽度?

代码语言:javascript
复制
@FXML
private void makeNewTab(ActionEvent event) {

    int totalTabs = selectTab.getTabs().size() - 1; // this is the TabPane

    Tab newTab = new Tab();
    newTab.setText("New tab" + totalTabs);
    newTab.setClosable(false);

    try{
    newTab.setContent(FXMLLoader.load(getClass().getResource("CoreScenes/NewTabSceneFXML.fxml")));
    }
    catch(IOException e){
    System.out.println("Failed to Load 'NewTabSceneFXML.fxml'. Unknown Error.");
    }


    selectTab.getTabs().add(newTab);
}

这是因为它是添加选项卡罚款,但它不适合它的宽度,这是我需要的。

编辑1:

这是一个从头开始的例子,可能是因为我使用的是场景构建,但我不知道。将所有东西的大小设置为计算大小不是我所需要的。我需要的是了解如何将sub文件的节点绑定到它的父fxml文件的节点。因此,当我调整屏幕大小时,所有的东西都会调整大小,但在场景生成器中似乎不可能。

不拘束,舞台太小

当我展开屏幕时,会显示更多的子选项卡。

主FXML:

代码语言:javascript
复制
<TabPane fx:id="mainTab" prefHeight="453.0" prefWidth="499.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1"  fx:controller="testtabs.FXMLDocumentController">
   <tabs>
      <Tab fx:id="ranTab" text="Untitled Tab 1">
         <content>
            <Button fx:id="button" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="100.0" prefWidth="115.0" text="Button" />
         </content>
      </Tab>
   </tabs>
</TabPane>

二级FXML:

代码语言:javascript
复制
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testtabs.FXMLTab2Controller">
   <children>
      <TabPane prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="Untitled Tab 1">
               <content>
                  <HBox prefHeight="100.0" prefWidth="200.0">
                     <children>
                        <SplitPane prefHeight="200.0" prefWidth="200.0" />
                        <ScrollPane prefHeight="200.0" prefWidth="200.0">
                          <content>
                            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" />
                          </content>
                        </ScrollPane>
                        <ScrollPane prefHeight="200.0" prefWidth="200.0" />
                     </children>
                  </HBox>
               </content></Tab>
          <Tab text="Untitled Tab 2" />
        </tabs>
      </TabPane>
   </children>
</AnchorPane>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-13 07:41:42

问题是次要的fxml:

在没有为子节点指定锚点的情况下,AnchorPane的行为就像一个Pane,即将子节点的大小调整到它们喜欢的大小,并且不管AnchorPane大小如何,保持这个大小。

若要更改此行为,请在辅助fxml中指定TabPane上的这些约束:

代码语言:javascript
复制
<TabPane prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.leftAnchor="0" AnchorPane.rightAnchor="0" AnchorPane.topAnchor="0" AnchorPane.bottomAnchor="0">
    ...
</TabPane>

或者更简单地使用<TabPane>作为fxml的根元素:

代码语言:javascript
复制
<TabPane prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testtabs.FXMLTab2Controller">
    ...
</TabPane>

注意:--用作该TabPane内容的HBox --不会将子级的宽度调整为比首选宽度更大的宽度,因为您没有使用默认值(NEVER)以外的hgrow属性。您也可以将其修复为例如,始终像这样生长ScrollPane

代码语言:javascript
复制
<HBox prefHeight="100.0" prefWidth="200.0">
   <children>
      <SplitPane prefHeight="200.0" prefWidth="200.0" />
      <ScrollPane prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
        <content>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" />
        </content>
      </ScrollPane>
      <ScrollPane prefHeight="200.0" prefWidth="200.0" />
   </children>
</HBox>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51296937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档