如何使用JavaFX创建HBox?
盒子
在这种布局中,节点排列在单个水平行中。您可以通过实例化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);
}
}输出结果
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短