Java FX-FXML:将 fxml 宽度绑定到不同的 fxml 宽度
Java FX-FXML: Binding an fxml width to a different fxml width
我有一个 TabPane FXML 文件,当您按下一个按钮时,它会添加一个新选项卡,该选项卡从不同的 fxml 文件中获取内容。如何将选项卡的宽度绑定到选项卡窗格的宽度?
@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:
这是一个从头开始的例子,可能是因为我正在使用场景构建,但我不知道。将所有内容的大小设置为计算大小并不是我所需要的。我需要的是找出如何将子 fxml 文件的节点绑定到它的父 fxml 文件的节点。因此,当我调整屏幕大小时,所有内容都会调整大小,但在 Scene Builder 中似乎不可能。
Does not bind, the stage is too small
When I expand the screen, more of the subtab is revealed
主要 FXML:
<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:
<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>
问题是辅助 fxml:
如果不指定子项的锚点,AnchorPane
的行为就像 Pane
,即子项的大小会调整到他们喜欢的大小并保持这个大小,而不管 AnchorPane
大小。
要更改此行为,请在辅助 fxml 中对 TabPane
指定这些约束:
<TabPane prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.leftAnchor="0" AnchorPane.rightAnchor="0" AnchorPane.topAnchor="0" AnchorPane.bottomAnchor="0">
...
</TabPane>
或者更简单地使用 <TabPane>
作为 fxml 的根元素:
<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
:
<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>
我有一个 TabPane FXML 文件,当您按下一个按钮时,它会添加一个新选项卡,该选项卡从不同的 fxml 文件中获取内容。如何将选项卡的宽度绑定到选项卡窗格的宽度?
@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:
这是一个从头开始的例子,可能是因为我正在使用场景构建,但我不知道。将所有内容的大小设置为计算大小并不是我所需要的。我需要的是找出如何将子 fxml 文件的节点绑定到它的父 fxml 文件的节点。因此,当我调整屏幕大小时,所有内容都会调整大小,但在 Scene Builder 中似乎不可能。
Does not bind, the stage is too small
When I expand the screen, more of the subtab is revealed
主要 FXML:
<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:
<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>
问题是辅助 fxml:
如果不指定子项的锚点,AnchorPane
的行为就像 Pane
,即子项的大小会调整到他们喜欢的大小并保持这个大小,而不管 AnchorPane
大小。
要更改此行为,请在辅助 fxml 中对 TabPane
指定这些约束:
<TabPane prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.leftAnchor="0" AnchorPane.rightAnchor="0" AnchorPane.topAnchor="0" AnchorPane.bottomAnchor="0">
...
</TabPane>
或者更简单地使用 <TabPane>
作为 fxml 的根元素:
<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
:
<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>