JavaFX窗体、控件常用属性设置

01 设置鼠标在控件上显示图标:

Button控件调用Cursor的枚举类型创建鼠标图标:

Button showAlertButton = new Button("Show Alert");
showAlertButton.setCursor(Cursor.WAIT);

对Scene实例调用Cursor的静态方法cursor创建鼠标图标实例:

Cursor waitCur = Cursor.cursor("WAIT")
scene.setCursor(waitCur);

02 获取显示屏幕尺寸、设置主窗体位置、尺寸:

private Stage setScreenDetails(Stage stage) {
        // 获取显示屏尺寸。
        Rectangle2D rectangle2D = Screen.getPrimary().getBounds();
        double width = rectangle2D.getWidth();
        double height = rectangle2D.getHeight();

        // 设置主窗体坐标
        stage.setX(width / 5);
        stage.setY(height / 5);
        
        // 设置主窗体尺寸。
        stage.setWidth(width * 3 / 5);
        stage.setHeight(height * 3 / 5);
        stage.setResizable(true);
        stage.setMinWidth(300);
        stage.setMinHeight(400);
//        primaryStage.setMaxWidth(width / 2);
//        primaryStage.setMaxHeight(height / 2);

        return stage;
    }

03 设置控件的属性Effect,包括阴影,旋转,平移。

private void start02(Stage primaryStage) throws Exception {
        // 设置阴影。
        Button button = new Button("Close");
        button.setEffect(new DropShadow());
        
        // 无设置。
        Button button1 = new Button("Start");
        
        // 设置平移、旋转。
        CheckBox checkBox = new CheckBox("CheckBox Test");
        checkBox.setEffect(new DropShadow());
        checkBox.getTransforms().addAll(new Translate(150, 150), new Rotate(30));
        
        VBox root = new VBox(3);
        root.getChildren().addAll(button, button1, checkBox);
        
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Testing LayoutBounds");
        primaryStage.show();
        
        // 打印控件属性。
        System.out.println("button(layoutBounds)  = " + button.getLayoutBounds());
        System.out.println("button(boundsInLocal) = " + button.getBoundsInLocal());

        System.out.println("button1(layoutBounds)  = " + button1.getLayoutBounds());
        System.out.println("button1(boundsInLocal) = " + button1.getBoundsInLocal());
        
        System.out.println("checkBox(layoutBounds)  = " + checkBox.getLayoutBounds());
        System.out.println("checkBox(boundsInLocal) = " + checkBox.getBoundsInLocal());
    }

运行结果:
JavaFX窗体、控件常用属性设置_第1张图片​​​​​​​

你可能感兴趣的:(Java,JavaFX,Java,JavaFX,开发语言)