JavaFX图形化界面(二) 面板类

HBox面板

  • 水平布局:内容都是自动水平摆放的

  • HBox类常用方法:
    hbox.setPadding(new Insets(10,10,10,10)); //填充
    hbox.setSpacing(10);//间距
    hbox.setAlignment(Pos.CENTER); // 设置HBox中子节点的对齐方式为居中
    hbox.getChildren().addAll(button1, button2); // 向HBox中添加两个按钮

在这里插入图片描述

public void start(Stage stage) throws Exception{
	HBox hbox = new HBox();
	Button b1 = new Button("按钮1");
	Button b2 = new Button("按钮2");
	Button b3 = new Button("按钮3");
	hbox.getChildren().addAll(b1,b2,b3);
	Scene scene = new Scene(hbox,600,400);
	stage.setScene(scene);
	stage.show();
}

如下图所示:
在这里插入图片描述

VBox面板

  • 垂直布局:内容都是自动垂直摆放的

BorderPane面板

  • UI控件只能放在顶部,底部,左侧,右侧,和中心区域

  • **BorderPane类常用方法:
    borderPane.setTop(topLabel); // 设置顶部节点为一个标签
    borderPane.setLeft(leftLabel); // 设置左侧节点为一个标签
    borderPane.setRight(rightLabel); // 设置右侧节点为一个标签
    borderPane.setCenter(centerLabel); // 设置中心节点为一个标签
    borderPane.setBottom(bottomLabel); // 设置底部节点为一个标签

在这里插入图片描述

 public void start(Stage stage){
        BorderPane borderPane = new BorderPane();
        Button mid = new Button("中间");
        Button top = new Button("顶部");
        Button left = new Button("左侧");
        Button right = new Button("右侧");
        Button bottom = new Button("底部");
        borderPane.setCenter(mid);
        borderPane.setTop(top);
        borderPane.setLeft(left);
        borderPane.setRight(right);
        borderPane.setBottom(bottom);

        Scene scene = new Scene(borderPane,400,300);
        stage.setScene(scene);
        stage.show();
    }

在这里插入图片描述

FlowPane面板

  • 流式布局是一种自动调整元素位置的布局方式,当容器的宽度不足以容纳所有元素时,元素会自动换行。
public void start(Stage stage){
        FlowPane root = new FlowPane();

        root.setHgap(10);
        root.setVgap(20);
        root.setPadding(new Insets(15,15,15,15));

        // Button 1
        Button button1= new Button("Button1");
        root.getChildren().add(button1);


        // Button 2
        Button button2 = new Button("Button2");
        button2.setPrefSize(100, 100);
        root.getChildren().add(button2);

        // TextField
        TextField textField = new TextField("Text Field");
        textField.setPrefWidth(110);// 设置按钮的首选宽度为110像素


        root.getChildren().add(textField);

        // CheckBox
        CheckBox checkBox = new CheckBox("Check Box");

        root.getChildren().add(checkBox);

        // RadioButton
        RadioButton radioButton = new RadioButton("Radio Button");
        root.getChildren().add(radioButton);

        Scene scene = new Scene(root, 550, 250);

        stage.setTitle("FlowPane Layout Demo");
        stage.setScene(scene);
        stage.show();
 }

在这里插入图片描述

GridPane面板

  • 网格布局:按行、列进行控件的排列

  • GridPane类常用方法:
    gridPane.setHgap(10); // 设置网格布局容器中列之间的水平间距为10像素
    gridPane.setVgap(10); // 设置网格布局容器中行之间的垂直间距为10像素
    gridPane.add(button, 1, 0, 2, 1); // 向网格布局容器中添加一个按钮,位于第1列第0行,这个按钮占据了2列,1行
 public void start(Stage stage) throws Exception {
        var rootPane = new GridPane();
        rootPane.setPadding(new Insets(10,10,10,10));
        rootPane.setVgap(5);
        rootPane.setHgap(5);
        final var label1 = new Label("用户名");
        final var name = new TextField();
        name.setPromptText("请输入用户名");
        rootPane.add(label1,0,0);
        rootPane.add(name,1,0);
        Label label2 = new Label("口令");
        var password = new PasswordField();
        password.setPromptText("请输入口令");
        rootPane.add(label2,0,1);
        rootPane.add(password,1,1);
        final var submit = new Button("确定");
        final var reset = new Button("重置");

        rootPane.add(submit,0,2);
        rootPane.add(reset,1,2);
        final  var label = new Label();
        rootPane.add(label,0,3,2,1);
        submit.setOnAction((ActionEvent e)->{
            if((name.getText()!=null&&!name.getText().isEmpty())){
                label.setText(name.getText()+" "+password.getText()+","+"欢迎登录!");
            }else{
                label.setText("用户名不能为空!");
            }
        });
        reset.setOnAction((ActionEvent e)->{
            name.clear();
            password.clear();
            label.setText(null);
        });
        password.setOnAction((ActionEvent e)->{
            if(!password.getText().equals("aaa")){
                label.setText("口令不正确!");
                label.setTextFill(Color.rgb(210,39,30));
            }else{
                label.setText("口令通过");
                label.setTextFill(Color.rgb(21,117,84));
            }
            password.clear();
        });
        rootPane.setGridLinesVisible(true);
        var scene = new Scene(rootPane,300,130);
        stage.setScene(scene);
        stage.setTitle("登录窗口");
        stage.show();

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

在这里插入图片描述

  • 11
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值