0
点赞
收藏
分享

微信扫一扫

JavaFX(四、输入框,标签,AnchorPane布局类,HBox和VBox布局类)

书呆鱼 2021-09-27 阅读 71

输入框,标签

public class 输入框标签 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
//        Button b1=new Button("按钮B1");
//        b1.setLayoutX(100);
//        b1.setLayoutY(100);
//        b1.setPrefWidth(100);
//        b1.setPrefHeight(50);
        Group group=new Group();

        Scene scene=new Scene(group);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX");
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);


        TextField textField=new TextField();
//        textField.setText("这是文本");
        textField.setLayoutX(100);
        textField.setLayoutY(100);
        //设置提示
        textField.setTooltip(new Tooltip("提示信息"));
        //设置未输入时候的显示文字,必须没有焦点才显示出来(和html不同)
        textField.setPromptText("请输入姓名");
        //去除输入框默认焦点
        textField.setFocusTraversable(false);
        //限制输入长度
        textField.textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.length() > 7) {
                textField.setText(oldValue);
            }
        });
        //选中文字的监听
        textField.selectedTextProperty().addListener((observable, oldValue, newValue) -> {

        });
        //密码框
        PasswordField passwordField=new PasswordField();
        passwordField.setLayoutX(200);
        passwordField.setLayoutY(200);

        //标签控件
        Label label=new Label("我是标签");
        //单击
        label.setOnMouseClicked(event -> System.out.println("标签被点击"));
        //而且即使没有setOnMouseClicked这种事件,也可以通过addEventHandler/filter实现
        group.getChildren().addAll(textField,passwordField,label);

        primaryStage.show();
    }

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

AnchorPane布局类

案例一

public class AnchorPane布局类  extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        Button btn1=new Button("btn1");
        Button btn2=new Button("btn2");


        //一旦使用AnchorPane接管布局,则此时内部使用按钮本身设置位置就无效了
        AnchorPane anchorPane=new AnchorPane();
//        anchorPane.setLayoutX(10 );

        //设置内边距
        anchorPane.setPadding(new Insets(10));


        //同理:可以button放在group中,然后AnchorPane管理group
        AnchorPane.setTopAnchor(btn1,0.0);
        AnchorPane.setLeftAnchor(btn1,10.0);

        AnchorPane.setTopAnchor(btn2,100.0);
        AnchorPane.setLeftAnchor(btn2,100.0);
        anchorPane.getChildren().addAll(btn1,btn2);
        anchorPane.setStyle(" -fx-background-color: aqua;");
        anchorPane.setOnMouseClicked(event -> {

        });

//        Group group=new Group();
        Scene scene=new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX");
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();
    }

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

案例二

public class AnchorPane布局类二 extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        Button b1=new Button("按钮");

        //告知:b1脱离父容器管理并且在这个位置上消失,这样在anchorPane1右下角就没有b1按钮了
//        b1.setManaged(false);但是打印父级的子元素个数,仍然包含这个脱离管理的元素
//        b1.setVisible(true);代表看不见,但是没脱离管理,且位置还有
//        b1.setOpacity(1);设置透明度
        AnchorPane anchorPane=new AnchorPane();
        AnchorPane anchorPane1=new AnchorPane();
        anchorPane1.setStyle("-fx-background-color: red;");
        anchorPane1.getChildren().add(b1);
        AnchorPane.setBottomAnchor(b1,0.0);
        AnchorPane.setRightAnchor(b1,0.0);
        //设置内边距
//        anchorPane.setPadding(new Insets(10));

        anchorPane.getChildren().addAll(anchorPane1);
        anchorPane.setStyle(" -fx-background-color: aqua;");



        Scene scene=new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX");
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();


        //此种方式,此时必须放在show后面,因为默认值的宽高无法获取,必须显示之后才行
        AnchorPane.setTopAnchor(anchorPane1,0.0);
        AnchorPane.setLeftAnchor(anchorPane1,0.0);
        AnchorPane.setBottomAnchor(anchorPane1,anchorPane.getHeight()/2);
        AnchorPane.setRightAnchor(anchorPane1,anchorPane.getWidth()/2);

        primaryStage.heightProperty().addListener((observable, oldValue, newValue) -> {
            //此时不能使用newvalue的值,因为包含边框和顶部栏等
            AnchorPane.setBottomAnchor(anchorPane1,anchorPane.getHeight()/2);
        });
        primaryStage.widthProperty().addListener((observable, oldValue, newValue) -> {
            AnchorPane.setRightAnchor(anchorPane1,anchorPane.getWidth()/2);
        });
    }

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

水平和垂直布局

public class HBox和VBox extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Button b1=new Button("b1");
        Button b2=new Button("b2");

        Button b3=new Button("b3");
        Button b4=new Button("b4");

        HBox hBox=new HBox();//水平布局
        hBox.setStyle("-fx-background-color: aliceblue;");
        hBox.setPrefHeight(300);
        hBox.setPrefWidth(300);
        //内边距
        hBox.setPadding(new Insets(10));
        //内部元素的间距
        hBox.setSpacing(10.0);
        //设置外边距,针对内部元素,挨个处理
        HBox.setMargin(b1,new Insets(10));
        //对齐方式,如果设置了,则以这个为准,而之前设置的其他属性可能无效
        hBox.setAlignment(Pos.BOTTOM_CENTER);
        hBox.getChildren().addAll(b1,b2);


        VBox vBox=new VBox();
        vBox.setStyle("-fx-background-color: antiquewhite;");
        vBox.setPrefHeight(300);
        vBox.setPrefWidth(300);
        vBox.getChildren().addAll(b3,b4);

        AnchorPane anchorPane=new AnchorPane();
        anchorPane.setStyle("-fx-background-color: aqua;");
        anchorPane.getChildren().addAll(hBox,vBox);
        AnchorPane.setLeftAnchor(vBox,305.0);

        Scene scene=new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX");
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.show();
    }

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

相关推荐

0 条评论