javafx2中控件的自动调整大小

发布于 2024-12-21 16:34:24 字数 486 浏览 1 评论 0原文

我正在尝试在场景图中添加一个 ToolBar 控件,其中包含 2 个按钮和一个 TextField。 我希望 TextField 控件自动调整大小并获取工具栏中的所有可用空间。因此,我使用 HBox 布局控件来添加按钮和文本字段。 我做了如下:

ToolBar tb = new ToolBar();
    HBox hbox = new HBox(8);
    TextField tf = new TextField();
    HBox.setHgrow(tf, Priority.ALWAYS);
    hbox.getChildren().add(new Button("<-"));
    hbox.getChildren().add(new Button("->"));
    hbox.getChildren().add(tf);            
    tb.getItems().add(hbox);

但它不起作用。我哪里错了?请帮忙。

I am trying to add a ToolBar control, containing 2 Buttons and a TextField in my scene graph.
I want that the TextField control gets automatically resized and acquire all the space available into toolbar. So, I used the HBox layout control to add the buttons and textfield.
I did as followed:

ToolBar tb = new ToolBar();
    HBox hbox = new HBox(8);
    TextField tf = new TextField();
    HBox.setHgrow(tf, Priority.ALWAYS);
    hbox.getChildren().add(new Button("<-"));
    hbox.getChildren().add(new Button("->"));
    hbox.getChildren().add(tf);            
    tb.getItems().add(hbox);

But its not working. Where am I going wrong? Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不吻晚风 2024-12-28 16:34:24

您可以尝试下一步:

    ToolBar tb = new ToolBar();
    TextField tf = new TextField();
    HBox hbox = new HBox(8);
    hbox.prefWidthProperty().bind(tb.widthProperty().subtract(20));
    HBox.setHgrow(tf, Priority.ALWAYS);
    hbox.getChildren().add(new Button("<-"));
    hbox.getChildren().add(new Button("->"));
    hbox.getChildren().add(tf);
    tb.getItems().add(hbox);

You can try next:

    ToolBar tb = new ToolBar();
    TextField tf = new TextField();
    HBox hbox = new HBox(8);
    hbox.prefWidthProperty().bind(tb.widthProperty().subtract(20));
    HBox.setHgrow(tf, Priority.ALWAYS);
    hbox.getChildren().add(new Button("<-"));
    hbox.getChildren().add(new Button("->"));
    hbox.getChildren().add(tf);
    tb.getItems().add(hbox);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文