javafx vbox 居中_如何平均分配JavaFX VBox的元素

Im使用VBox並向其添加了三個標籤。標籤之間的垂直空間使用setSpacing()方法進行設置。但它是一個固定值,說20。如果我將該值更改爲50,則空間將會增加。但它是硬編碼。如何平均分配JavaFX VBox的元素

我想知道是否有任何預定義的方法可以使用,以便標籤均勻分佈(基於VBox高度)。如果我調整窗口的大小,應該調整標籤(我的意思是標籤之間的空間),使它們仍然均勻分佈(在任何兩個標籤之間具有相同的空間)。

作爲VBox被設定在BorderPane的左側,我想 「lab0」 在 「顯示行1」, 「lab1中的」 在 「顯示行5」 和「實驗2 「at」Display Line 9「(根據對齊方式)。在更多TextFields被添加的情況下,「lab2」最後應該是「Display Line X」。

我的代碼在這裏...請標記。

import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.RadioButton;

import javafx.scene.control.TextField;

import javafx.scene.control.ToggleGroup;

import javafx.scene.paint.Color;

import javafx.scene.shape.Circle;

import javafx.scene.text.Font;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.FlowPane;

import javafx.scene.layout.GridPane;

import javafx.scene.layout.StackPane;

import javafx.scene.layout.VBox;

import javafx.scene.layout.HBox;

import javafx.stage.Stage;

public class MyJavaFx extends Application

{

public MyJavaFx()

{

}

public void start(Stage primaryStage)

{

BorderPane pane = new BorderPane();

pane.setTop(getRadioButtons());

pane.setBottom(getPushButtons());

pane.setLeft(getLeftLabels());

pane.setRight(getRightLabels());

pane.setCenter(getTextFields());

Scene scene=new Scene(pane,1000,800);

primaryStage.setTitle("Even");

primaryStage.setScene(scene);

primaryStage.show();

}

public static void main(String arg[])

{

Application.launch(arg);

}

FlowPane getRadioButtons()

{

FlowPane pane = new FlowPane();

pane.setPadding(new Insets(10,10,10,10));

pane.setHgap(10);

RadioButton rb[]=new RadioButton[3];

ToggleGroup radioGroup = new ToggleGroup();

for(int i=0;i

{

rb[i] = new RadioButton("Flag "+(i+1));

rb[i].setToggleGroup(radioGroup);

pane.getChildren().add(rb[i]);

}

rb[0].setSelected(true);

pane.setAlignment(Pos.CENTER);

return pane;

}

FlowPane getPushButtons()

{

FlowPane pane = new FlowPane();

pane.setPadding(new Insets(10,30,30,10));

pane.setHgap(10);

Button rb[]=new Button[3];

for(int i=0;i

{

rb[i] = new Button("but"+i);

pane.getChildren().add(rb[i]);

}

pane.setAlignment(Pos.BOTTOM_RIGHT);

return pane;

}

VBox getLeftLabels()

{

VBox pane = new VBox();

pane.setPadding(new Insets(10,10,10,10));

pane.setSpacing(20);

Label ll[]=new Label[3];

for(int i=0;i

{

ll[i] = new Label("lab"+i);

pane.getChildren().add(ll[i]);

}

ll[0].setAlignment(Pos.TOP_LEFT);

ll[1].setAlignment(Pos.CENTER_LEFT);

ll[2].setAlignment(Pos.BOTTOM_LEFT);

pane.setAlignment(Pos.CENTER_LEFT);

return pane;

}

VBox getRightLabels()

{

VBox pane = new VBox();

pane.setPadding(new Insets(10,10,10,10));

pane.setSpacing(20);

Label rb[]=new Label[3];

for(int i=0;i

{

rb[i] = new Label("lab"+i);

pane.getChildren().add(rb[i]);

}

pane.setAlignment(Pos.CENTER_RIGHT);

return pane;

}

VBox getTextFields()

{

VBox pane = new VBox();

pane.setPadding(new Insets(10,10,10,10));

pane.setSpacing(2);

TextField tf[]=new TextField[9];

for(int i=0;i

{

tf[i] = new TextField("Display Line "+(i+1));

pane.getChildren().add(tf[i]);

}

pane.setAlignment(Pos.CENTER_RIGHT);

return pane;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值