盒子
在这种布局中,节点排列在单个水平行中。您可以通过实例化javafx.scene.layout.HBox类在应用程序中创建hbox 。您可以使用setPadding()方法在hbox周围设置填充。
要将节点添加到此窗格中,可以将它们作为构造函数的参数传递,也可以将它们添加到窗格的可观察列表中,如下所示:
getChildren().addAll();
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class HBoxExample extends Application {
public void start(Stage stage) {
//创建标签
Label label1 = new Label("用户名: ");
Label label2 = new Label("Password: ");
//创建文本和密码字段
TextField textField = new TextField();
PasswordField pwdField = new PasswordField();
//为节点添加标签
HBox box = new HBox(5);
box.setPadding(new Insets(50, 5 , 5, 50));
box.getChildren().addAll(label1, textField, label2, pwdField);
//设置舞台
Scene scene = new Scene(box, 595, 150, Color.BEIGE);
stage.setTitle("HBox示例");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出结果