如何使用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 婆婆高寿祝福语大全简短