JavaFX:setHgrow(...) 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21865044/
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
JavaFX: setHgrow(...) doesn't work
提问by Tonkel
I couldn't get the Elements in my HBox to grow, so I downloaded the following example code from java2s.com. It serves as a minimal not working example:
我无法让 HBox 中的元素增长,因此我从java2s.com下载了以下示例代码。它作为一个最小的不工作的例子:
package fxtest;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Fxtest extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("");
Group root = new Group();
Scene scene = new Scene(root, 600, 250, Color.WHITE);
HBox hbox = new HBox();
TextField field = new TextField();
HBox.setHgrow(field, Priority.ALWAYS);
hbox.getChildren().addAll(new Label("Search:"), field, new Button("Go"));
root.getChildren().add(hbox);
primaryStage.setScene(scene);
primaryStage.show();
}
}
The result looks like this.
结果看起来像这样。
If I understand it correctly, the TextField should grow, shouldn't it?
如果我理解正确,TextField 应该会增长,不是吗?
My Java Version is 1.7.0_51
我的 Java 版本是 1.7.0_51
My JavaFX Version is 2.2.51-b13
我的 JavaFX 版本是 2.2.51-b13
Do you know why it isn't working/what I have to do? Thanks!
你知道为什么它不起作用/我必须做什么吗?谢谢!
采纳答案by jewelsea
Why HBox.setHgrowdoesn't work for you
为什么HBox.setHgrow对你不起作用
You use a Groupas the root node of your scene and groups are not resizable, so the group size does not adjust to the scene's size.
您使用组作为场景的根节点,并且组的大小不可调整,因此组大小不会根据场景的大小进行调整。
A Group will take on the collective bounds of its children and is not directly resizable.
By default, a Group will "auto-size" its managed resizable children to their preferred sizes during the layout pass
Group 将承担其子项的集体边界,并且不能直接调整大小。
默认情况下,组将在布局过程中将其托管的可调整大小的子项“自动调整大小”为他们的首选大小
In general, you very rarely want to use a Group as the root node for a scene.
通常,您很少希望使用组作为场景的根节点。
How to Fix it
如何修复
Use only resizable nodes in your scene (Panesubclasses or Controlsubclasses).
Sample
样本
Substitute your start method with the method below which removes the Group root node and instead makes the HBox the root node for the scene:
用下面的方法替换你的 start 方法,该方法删除 Group 根节点,而是使 HBox 成为场景的根节点:
public void start(Stage primaryStage) {
HBox hbox = new HBox(10);
TextField field = new TextField();
HBox.setHgrow(field, Priority.ALWAYS);
hbox.setAlignment(Pos.BASELINE_LEFT);
hbox.getChildren().addAll(
new Label("Search:"), field, new Button("Go")
);
hbox.setPadding(new Insets(10));
Scene scene = new Scene(hbox, 600, 250, Color.WHITE);
primaryStage.setScene(scene);
primaryStage.show();
}
Output of the sample after resizing the window (you can see that the search text field grows and shrinks to fit available space as requested):
调整窗口大小后示例的输出(您可以看到搜索文本字段会根据要求增长和缩小以适应可用空间):
回答by Tonkel
jewelsea provided the right answer for the given example; thanks for that!
Jewelsea 为给定的示例提供了正确的答案;感谢那!
It helped me figuring out a problem with similar symptoms but another root. If you use ScrollPanes, you need to set
它帮助我找出了一个具有类似症状但另一个根源的问题。如果使用 ScrollPanes,则需要设置
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
in order to get the desired growing effect.
以获得理想的生长效果。