简体   繁体   English

JavaFX,如何调整复选框的大小?

[英]JavaFX, How to Resize a Checkbox?

I can't seem to resize a check box, regardless of using width/height, minWidth/minHeight or maxWidth/maxHeight. 无论使用宽度/高度,minWidth / minHeight还是maxWidth / maxHeight,我似乎都无法调整复选框的大小。 Changing the min width only seems to effect the nodes padding 仅更改最小宽度似乎只会影响节点填充

package checkboxtest;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class CheckBoxTest extends Application {

    @Override
    public void start(Stage primaryStage) {

        CheckBox checkBoxResize = new CheckBox("Resize");
        checkBoxResize.setMinWidth(300);
        checkBoxResize.setMinHeight(300);

        CheckBox checkBoxNoResize = new CheckBox("No Resize");

        StackPane root = new StackPane();
        root.getChildren().add(checkBoxResize);
        root.getChildren().add(checkBoxNoResize);

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

        primaryStage.setTitle("Hello Checkbox!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

The "box" and "mark" (ie check mark or tick) in a check box are determined by CSS. 复选框中的“框”和“标记”(即复选标记或勾号)由CSS确定。 Both are rendered as regions with padding to determine their sizes; 两者都被渲染为带有填充的区域以确定它们的大小; the mark uses a SVG shape to render the check mark. 标记使用SVG形状来渲染复选标记。 So to change the size, you need to change the padding applied to these. 因此,要更改大小,您需要更改应用于这些的填充。

The defaults, from modena.css , are 来自modena.css的默认值是

.check-box > .box {
    /* ... */
    -fx-padding: 0.166667em 0.166667em 0.25em 0.25em; /* 2 2 3 3 */
}
.check-box > .box > .mark {
    /* ... */
    -fx-padding: 0.416667em 0.416667em 0.5em 0.5em; /* 5 5 6 6 */
    -fx-shape: "M-0.25,6.083c0.843-0.758,4.583,4.833,5.75,4.833S14.5-1.5,15.917-0.917c1.292,0.532-8.75,17.083-10.5,17.083C3,16.167-1.083,6.833-0.25,6.083z";
}

So to increase the size by a factor of 12, you can use: 因此,要将大小增加12倍,您可以使用:

.big-check-box > .box {
    -fx-padding: 2em 2em 3em 3em ;
}
.big-check-box > .box > .mark {
    -fx-padding: 5em 5em 6em 6em; 
}

The following SSCCE, with the above CSS (second code block) in a file "big-check-box.css": 以下SSCCE,上面的CSS(第二个代码块)在文件“big-check-box.css”中:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class BigCheckBox extends Application {

    @Override
    public void start(Stage primaryStage) {
        CheckBox checkBox = new CheckBox("Regular");
        CheckBox bigCheckBox = new CheckBox("Big");
        bigCheckBox.getStyleClass().add("big-check-box");
        VBox root = new VBox(5, checkBox, bigCheckBox);

        root.setPadding(new Insets(20));
        Scene scene = new Scene(root);
        scene.getStylesheets().add("big-check-box.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

gives

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM