Poo*_*dra 9 java javafx vertical-alignment vbox
我使用 aVBox并为其添加了三个标签。标签之间的垂直间距是用setSpacing()方法设置的。但它是一个固定值,比如20。如果我将值更改为50,空间将增加。但它是硬编码。
我想知道是否有任何可以使用的预定义方法,以便标签均匀分布(基于VBox高度)。如果我调整窗口大小,标签(我的意思是标签之间的空间)应该调整,以便它们仍然均匀分布(任何两个标签之间的空间相同)。
作为VBox被设定在的左侧BorderPane,我想“ lab0 ”在“显示行1 ”,“ lab1中的在”“显示第5行”和“实验2 ”为“显示第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.length;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.length;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.length;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.length;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.length;i++)
{
tf[i] = new TextField("Display Line "+(i+1));
pane.getChildren().add(tf[i]);
}
pane.setAlignment(Pos.CENTER_RIGHT);
return pane;
}
}
Run Code Online (Sandbox Code Playgroud)
Nic*_*tto 10
您可以做的是在每个标签之前和之后添加“间隔符”,由于VBox.setVgrow(Node child, Priority value)具有Priority.ALWAYS优先级值,这些标签将始终根据可用空间增长或缩小。
这是创建“间隔器”的方法
private Node createSpacer() {
final Region spacer = new Region();
// Make it always grow or shrink according to the available space
VBox.setVgrow(spacer, Priority.ALWAYS);
return spacer;
}
Run Code Online (Sandbox Code Playgroud)
那么你的代码将是:
VBox getTextFields()
{
VBox pane = new VBox();
pane.setPadding(new Insets(10,10,10,10));
TextField tf[]=new TextField[9];
// Add first spacer
pane.getChildren().add(createSpacer());
for(int i=0;i<tf.length;i++)
{
tf[i] = new TextField("Display Line "+(i+1));
pane.getChildren().add(tf[i]);
// Add a spacer after the label
pane.getChildren().add(createSpacer());
}
pane.setAlignment(Pos.CENTER_RIGHT);
return pane;
}
Run Code Online (Sandbox Code Playgroud)
关于您的问题的第二部分与您想要将标签labX与Display Line X对齐的事实有关,最简单的方法是将所有应该在同一行中的标签放在一个公共的HBox“间隔符”中,作为如上所述
| 归档时间: |
|
| 查看次数: |
4013 次 |
| 最近记录: |