JavaFX VBox和HBox布局 [英] JavaFX VBox and HBox layouts

查看:498
本文介绍了JavaFX VBox和HBox布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究JavaFX应用程序,该应用程序具有由外部数据结构生成的布局,该结构由以下内容组成:

I'm working on a JavaFX application which has a layout generated from an external data structure, composed of

  • 显示知道其自身纵横比(高度取决于宽度)的组件
  • 2种类型的结构组件
    • 在所有页面上以相等的宽度显示所有子项,每个子项都根据需要留出尽可能多的垂直空间
    • 使用全宽显示页面上的所有子项,并根据需要占用尽可能多的垂直空间
    • displaying components that know their own aspect ratios (height as a dependent of width)
    • 2 types of structural components that
      • display all children with an equal width across their page, each child up as much vertical space as needed
      • display all children down the page using the full width and taking as much vertical space as needed

      但是我发现事情并没有像我期望的那样显示.我做了一个简化的案例来说明问题.

      But I'm finding things aren't displaying as I expect. I've made a simplified case that demonstrates the problem.

      下面是代码,问题是v3没有显示,我无法一生找出原因.我想我不了解VBox es和HBox es的某些方面.

      The code is below, and the problem is that v3 doesn't get displayed, and I can't for the life of me work out why. I guess there's some facet of VBoxes and HBoxes that I haven't understood.

      我非常感谢您的任何帮助或想法.预先感谢!

      I'd really appreciate any help or ideas. Thanks in advance!

      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.Priority;
      import javafx.scene.layout.Region;
      import javafx.scene.layout.VBox;
      import javafx.scene.paint.Color;
      import javafx.scene.shape.Rectangle;
      import javafx.stage.Stage;
      
      import java.util.Random;
      
      public class Test extends Application {
      
          static Random rand = new Random();
      
          public static void main(String args[]) {
              Application.launch("something");
          }
      
          @Override
          public void start(Stage mainStage) throws Exception {
              testVBoxes(mainStage);
          }
      
          private void testVBoxes(Stage mainStage) {
              VBox root = new VBox();
              Scene one = new Scene(root, 800, 600, Color.WHITE);
      
              FixedAspectRatioH h1 = new FixedAspectRatioH();
              FixedAspectRatioH h2 = new FixedAspectRatioH();
              FixedAspectRatioH h3 = new FixedAspectRatioH();
              FixedAspectRatioV v1 = new FixedAspectRatioV();
              FixedAspectRatioV v2 = new FixedAspectRatioV();
              FixedAspectRatioV v3 = new FixedAspectRatioV();
      
              h1.prefWidthProperty().bind(root.widthProperty());
      
              h2.add(v2);
      
              v1.add(h3);
              v1.add(h2);
      
              h1.add(v1);
              h1.add(v3);
      
              root.getChildren().add(h1);
      
              mainStage.setScene(one);
              mainStage.show();
          }
      
          private class FixedAspectRatioV extends VBox {
              public FixedAspectRatioV() {
                  Rectangle r = new Rectangle();
                  r.setFill(Color.rgb(rand.nextInt(256), rand.nextInt(256),     rand.nextInt(256)));
                  r.widthProperty().bind(widthProperty());
                  r.heightProperty().bind(r.widthProperty().divide(3));
                  getChildren().add(r);
              }
      
              public void add(Region n) {
                  n.prefWidthProperty().bind(widthProperty());
                  getChildren().add(n);
              }
          }
      
          private class FixedAspectRatioH extends HBox {
              public FixedAspectRatioH() {
                  Rectangle r = new Rectangle();
                  r.setFill(Color.rgb(rand.nextInt(256), rand.nextInt(256),     rand.nextInt(256)));
                  r.widthProperty().bind(widthProperty().divide(4));
                  r.heightProperty().bind(r.widthProperty());
                  getChildren().add(r);
              }
      
              public void add(Region n) {
                  HBox.setHgrow(n, Priority.ALWAYS);
                  getChildren().add(n);
              }
          }
      }
      

      推荐答案

      已经2年了,但是解决方案是您忘记了

      its 2 years but the solution is you forgot

      Node.setPrefSize(width,height);
      

      并将其添加到您的构造函数Hbox.setFillHeight(true);

      and also add this to your constructor Hbox.setFillHeight(true);

      这篇关于JavaFX VBox和HBox布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆