Java 在窗格中填充宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33414194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill width in a Pane
提问by Vertex
There is a Pane
as root and a GridPane
as its child. The root resizes with the stage. How can I achieve that the GridPane
gets automaticallyresized with the root to fill the width? Any width should possible.Is it possible without property binding?
有一个Pane
作为根和一个GridPane
作为它的孩子。根随着舞台调整大小。我怎样才能做到这一点的GridPane
被自动与根填补宽度调整?任何宽度都应该是可能的。没有属性绑定是否可能?
This is the situation:
情况是这样的:
- A
Pane
root(yellow) is the root of theScene
so it fills the width and height of theStage
. - A
GridPane
(green) with one row and 3 colums as child of root. - The row should have a fixed height.
- The first and third column should have a constant size
- The middle column should grow to maximum
- The
GridPane
should fill the width of its parent
- 甲
Pane
根(黄色)是的根Scene
,使其充满的宽度和高度Stage
。 - A
GridPane
(绿色),一行和 3 列作为root 的孩子。 - 该行应具有固定高度。
- 第一列和第三列应该有一个恒定的大小
- 中间列应该增长到最大
- 本
GridPane
应填写其父的宽度
Unfortunately the GridPane
does not fill the width:
Code:
代码:
@Override
public void start(Stage primaryStage) throws Exception {
// Resizes with the stage
final Pane root = new Pane();
root.setStyle("-fx-background-color: yellow");
// grid: 1 row, 3 colums
// should have a constant height and should grow in width to fill parent pane
final GridPane gridPane = new GridPane();
root.getChildren().add(gridPane);
gridPane.setStyle("-fx-background-color: green");
gridPane.setGridLinesVisible(true);
final RowConstraints row = new RowConstraints(100); // constant height = 100
final ColumnConstraints col1 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
// should grow as much as possible in width
final ColumnConstraints col2 = new ColumnConstraints(200, Control.USE_COMPUTED_SIZE, Double.MAX_VALUE);
final ColumnConstraints col3 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
gridPane.getRowConstraints().add(row);
gridPane.getColumnConstraints().addAll(col1, col2, col3);
final Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setWidth(600);
primaryStage.setHeight(400);
primaryStage.show();
}
采纳答案by James_D
The size of nodes inside panes is controlled by the way the individual pane implements its layout: i.e. the size of a node is effectively determined by its parent. So the overall size of your grid pane is controlled by the layout implementation of Pane
.
窗格内节点的大小由单个窗格实现其布局的方式控制:即节点的大小由其父节点有效确定。因此,您的网格窗格的整体大小由Pane
.
Pane
actually does minimal layout work: it effectively just positions everything at (0,0) and sizes it to its preferred size. So your grid pane gets its preferred width, which is the sum of the preferred widths of the columns.
Pane
实际上做了最少的布局工作:它实际上只是将所有内容定位在 (0,0) 并将其调整为首选大小。因此,您的网格窗格将获得其首选宽度,即各列首选宽度的总和。
So to get your grid pane to grow, you either need to change its preferred width, or use a layout pane that allows you to control how it grows.
因此,要使网格窗格增长,您要么需要更改其首选宽度,要么使用允许您控制其增长方式的布局窗格。
For example:
例如:
gridPane.prefWidthProperty().bind(root.widthProperty());
will make the grid pane grow to the width of root. This will just add extra space to the right of all three columns, so additionally setting
将使网格窗格增长到根的宽度。这只会在所有三列的右侧添加额外的空间,因此另外设置
col2.setHgrow(Priority.ALWAYS);
will let col2
have the extra width.
将让col2
有额外的宽度。
Alternatively, using an AnchorPane
for root
:
或者,使用AnchorPane
for root
:
// Pane root = new Pane();
AnchorPane root = new AnchorPane();
and setting
和设置
AnchorPane.setLeftAnchor(gridPane, 0.0);
AnchorPane.setRightAnchor(gridPane, 0.0);
(again with the col2
hgrow
set) will have the same effect.
(再次与col2
hgrow
设置)将具有相同的效果。
Or you could do
或者你可以做
VBox root = new VBox();
root.setFillWidth(true);
(for example: there are many solutions).
(例如:有很多解决方案)。
The choice of solution depends on what other nodes, if any, you have inside the root pane.
解决方案的选择取决于根窗格中的其他节点(如果有)。
回答by Jordi Castilla
Actually your result is the expected 100 + 200 + 100 = 400px
:
实际上你的结果是预期的100 + 200 + 100 = 400px
:
final ColumnConstraints col1 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
// ↑
final ColumnConstraints col2 = new ColumnConstraints(200, Control.USE_COMPUTED_SIZE, Double.MAX_VALUE);
// ↑
final ColumnConstraints col3 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
// ↑
And you define:
你定义:
primaryStage.setWidth(600);
Just change medium column to fill the remainin 200px
只需更改中列以填充剩余部分 200px
final ColumnConstraints col1 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
final ColumnConstraints col2 = new ColumnConstraints(400, Control.USE_COMPUTED_SIZE, Double.MAX_VALUE);
// ↑ change to 400
final ColumnConstraints col3 = new ColumnConstraints(100, Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
回答by Uluk Biy
Pane
does not automatically layouts its children. Try to use VBox
instead. It will fill the width of available space by (if possible) resizing its childdren. This way the GridPane will resize on stage width changes.
Pane
不会自动布局其子项。尝试使用VBox
。它将通过(如果可能)调整其子项的大小来填充可用空间的宽度。这样 GridPane 将在舞台宽度变化时调整大小。
Now, you can manage the gridpane columns how they share the space. According to your description it is enough to set
现在,您可以管理网格窗格列如何共享空间。根据你的描述设置就够了
col2.setHgrow( Priority.ALWAYS );
回答by cagney
To make gridpane automatically adjust use the fxml. Don't do it with the java.
要使 gridpane 自动调整使用 fxml。不要用java来做。
See below they are set to a percentage width. The very last row in my example below has fixed settings.
见下文,它们被设置为百分比宽度。下面示例中的最后一行具有固定设置。
<GridPane fx:id="gridPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints percentWidth="50" />
<ColumnConstraints percentWidth="50" />
</columnConstraints>
<rowConstraints>
<RowConstraints percentHeight="35" />
<RowConstraints percentHeight="35" />
<RowConstraints maxHeight="106.0" minHeight="10.0" prefHeight="106.0" vgrow="ALWAYS" />
</rowConstraints>
<children>