简体   繁体   English

JavaFX:setHgrow(...)不起作用

[英]JavaFX: setHgrow(…) doesn't work

I couldn't get the Elements in my HBox to grow, so I downloaded the following example code from java2s.com . 我无法让我的HBox中的Elements增长,所以我从java2s.com下载了以下示例代码。 It serves as a minimal not working example: 它是一个最小的不工作的例子:

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! 谢谢!

Why HBox.setHgrow doesn't work for you 为什么HBox.setHgrow不适合你

You use a Group as 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 默认情况下,组将在布局过程中将其可管理的可调整大小的子项“自动调整”为其首选大小

In general, you very rarely want to use a Group as the root node for a scene. 通常,您很少希望使用Group作为场景的根节点。

How to Fix it 如何解决它

Use only resizable nodes in your scene ( Pane subclasses or Control subclasses). 仅在场景中使用可调整大小的节点( 窗格子类或控制子类)。

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方法,删除组根节点,而不是让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): 调整窗口大小后输出样本(您可以看到搜索文本字段增长和缩小以适应请求的可用空间):

增长

收缩

jewelsea provided the right answer for the given example; jewelsea为这个例子提供了正确的答案; thanks for that! 感谢那!

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. 为了获得理想的生长效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM