如何使用FXML在JavaFX 2中指定宽度百分比? [英] How do specify a width percentage in JavaFX 2 using FXML?
问题描述
我正在寻找一种方法来将FXML中的maxWidth大小设置为80%.
I am looking for a way to say set a maxWidth size to 80% in FXML.
很像Web开发.
<VBox fx:id="testVB" prefWidth="600">
但这不是:
<VBox fx:id="testVB" prefWidth="80%">
But this does not:
<VBox fx:id="testVB" prefWidth="80%">
我知道在Straight JavaFX2 non-fxml中可以创建inset吗?在FMXL中的代码之外执行此操作的最佳方法是什么?
I know that in Straight JavaFX2 non-fxml you can create insets? What is the best way to do this outside of code in FMXL?
谢谢!
Riley
推荐答案
我不确定可以.您需要使用GridPane
布局组件.在此组件中,您可以指定行和列约束,在这些约束中,可以将宽度指定为百分比.例如:
I'm not sure you can. You need to use the GridPane
layout component. In this component, you can specify rows and columns constraints, and in these constraints you can specify a width as a percentage. For example:
<GridPane>
<children>
<TitledPane text="testGridPane" GridPane.columnIndex="0" GridPane.rowIndex="0" />
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="80.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="20.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
此代码定义GridPane
,其第一列的宽度为80%. TitledPane
设置在该GridPane
第一列的第一单元格中,并且可以(因为您需要确保TitledPane
的宽度约束与您的需求相匹配)占据该宽度的80% GridPane
.
This code defines a GridPane
with a first column with a width of 80%. The TitledPane
is set in the first cell of the first column of this GridPane
, and can (because you need to be sure that the width constraints of the TitledPane
match your needs) occupy 80% of the width of the GridPane
.
请注意,我删除了所有与您的问题无关的信息.顺便说一句,Oracle的Scene Builder工具对于定义复杂的FXML布局非常有用.
Please note that I removed all information not relevant to your question. By the way, Oracle's Scene Builder tool is very useful to define complex FXML layout.
这篇关于如何使用FXML在JavaFX 2中指定宽度百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!