JavaFX HBox

JavaFX教程 - JavaFX HBox


JavaFX API具有将UI控件显示到场景图上的布局类。

HBox布局类将JavaFX子节点放在水平行中。新的子节点附加到右侧的末尾。

默认情况下,HBox布局尊重孩子的首选宽度和高度。

当父节点不可调整大小时,例如Group节点,HBox的行高度被设置为子节点的最大优选高度。

默认情况下,每个子节点与左上(Pos.TOP_LEFT)位置对齐。

我们可以编程改变HBox的布局约束,例如边框,填充,边距,间距和对齐。

当处理不可缩放的子节点(例如Shape节点)时,父节点考虑了Shape的矩形边界(ParentInBounds),它的宽度和高度。

当处理可调整大小的节点(如TextField控件)时,父节点计算TextField水平增长的可用空间。

要在HBox中水平生长UI控件,请使用静态HBox.setHgrow()方法。



例子

以下代码将TextField控件设置为在调整父HBox的宽度时水平增长:

TextField  myTextField = new TextField();
HBox.setHgrow(myTextField,  Priority.ALWAYS);

完整的源代码。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/*from   w w w. j a  v  a  2s  . c o m*/
public class Main extends Application {
  @Override
  public void start(Stage primaryStage) {
    TextField myTextField = new TextField();
    HBox hbox = new HBox();
    hbox.getChildren().add(myTextField);
    HBox.setHgrow(myTextField, Priority.ALWAYS);
    
    Scene scene = new Scene(hbox, 320, 112, Color.rgb(0, 0, 0, 0));
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

上面的代码生成以下结果。

null


实施例2

下面的代码为HBox添加了四个矩形,设置了HBox约束并展示了HBox布局控件的许多间距属性。

矩形节点不可调整大小。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
//  ww  w.  ja  v  a 2s  .c  om
public class Main extends Application {
  @Override
  public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250);
    // 5 pixels space between child nodes
    HBox hbox = new HBox(5);
    // 1 pixel padding between child nodes only
    hbox.setPadding(new Insets(1));
    Rectangle r1 = new Rectangle(10, 10);
    Rectangle r2 = new Rectangle(20, 100);
    Rectangle r3 = new Rectangle(50, 20);
    Rectangle r4 = new Rectangle(20, 50);

    HBox.setMargin(r1, new Insets(2, 2, 2, 2));

    hbox.getChildren().addAll(r1, r2, r3, r4);
    root.getChildren().add(hbox);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
  public static void main(String[] args) {
    launch(args);
  }
}

上面的代码生成以下结果。

null

在HBox中成长

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
//from  w w w . ja v a2 s  .com
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    HBox hbox = new HBox();
    Button button1 = new Button("Add               ");
    Button button2 = new Button("Remove   ");
    HBox.setHgrow(button1, Priority.ALWAYS);
    HBox.setHgrow(button2, Priority.ALWAYS);
    button1.setMaxWidth(Double.MAX_VALUE);
    button2.setMaxWidth(Double.MAX_VALUE);
    hbox.getChildren().addAll(button1, button2);
    
    root.getChildren().add(hbox);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

上面的代码生成以下结果。

null

为HBox设置首选宽度

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/*from w w w .  j a v  a 2  s  .c  om*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    HBox hbox = new HBox();
    Button button1 = new Button("Add               ");
    Button button2 = new Button("Remove   ");
    HBox.setHgrow(button1, Priority.ALWAYS);
    HBox.setHgrow(button2, Priority.ALWAYS);
    button1.setMaxWidth(Double.MAX_VALUE);
    button2.setMaxWidth(Double.MAX_VALUE);
    hbox.getChildren().addAll(button1, button2);
    
    hbox.setPrefWidth(400);
    
    root.getChildren().add(hbox);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

上面的代码生成以下结果。

null

在HBox的控件之间设置空格

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/*  ww w  .j a va2  s  . c om*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    HBox hbox = new HBox(8);//space
    Button button1 = new Button("Add               ");
    Button button2 = new Button("Remove   ");
    HBox.setHgrow(button1, Priority.ALWAYS);
    HBox.setHgrow(button2, Priority.ALWAYS);
    button1.setMaxWidth(Double.MAX_VALUE);
    button2.setMaxWidth(Double.MAX_VALUE);
    hbox.getChildren().addAll(button1, button2);
    
    hbox.setPrefWidth(400);
    
    root.getChildren().add(hbox);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

上面的代码生成以下结果。

null

为HBox设置填充和间距

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/*w  ww.ja v  a  2s.  c  o  m*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("HBox Test");

    // HBox
    HBox hb = new HBox();
    hb.setPadding(new Insets(15, 12, 15, 12));
    hb.setSpacing(10);

    // Buttons
    Button btn1 = new Button();
    btn1.setText("Button1");
    hb.getChildren().add(btn1);

    Button btn2 = new Button();
    btn2.setText("Button2");
    hb.getChildren().add(btn2);

    Button btn3 = new Button();
    btn3.setText("Button3");
    hb.getChildren().add(btn3);

    Button btn4 = new Button();
    btn4.setText("Button4");
    hb.getChildren().add(btn4);

    // Adding HBox to the scene
    Scene scene = new Scene(hb);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

上面的代码生成以下结果。

null
0.0432s