javafx vbox 居中_JavaFX VBox和HBox布局

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

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.

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);

}

}

}

解决方案

its 2 years but the solution is you forgot

Node.setPrefSize(width,height);

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值