【问题标题】:JavaFx : Is it possible to align the Box above the Text content in a checkbox?JavaFx:是否可以在复选框中对齐文本内容上方的框?
【发布时间】:2020-08-13 10:20:25
【问题描述】:

我想实现这样的目标:

【问题讨论】:

  • 不支持(afaics,虽然不完全确定) - 如果真的不支持:应该可以使用自定义皮肤:实现它的 layoutChildren 来做任何你需要的事情

标签: java css javafx fxml


【解决方案1】:

以下是实现您想要的一个简单示例。更专业的方法是实现自定义皮肤。也许其他人会分享它。

应用:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;


public class App extends Application {

    @Override
    public void start(Stage stage) {

        // Create a separated label at first:
        Label label = new Label("Am efectuat toate");

        // Create a check box without a text
        CheckBox checkBox = new CheckBox();

        // Put the check box as the graphic node of the label:
        label.setGraphic(checkBox);

        // Put the box on top of the label:
        label.setContentDisplay(ContentDisplay.TOP);

        // Toggle selected property of checkbox on label primary mouse button click:
        label.setOnMouseClicked(mouseEvent -> {
            if (mouseEvent.getButton() == MouseButton.PRIMARY)
                checkBox.setSelected(!checkBox.isSelected());
        });

        // If you want to have the last word "toate" in a second line:
        label.setWrapText(true);
        label.setMaxWidth(80);
        label.setTextAlignment(TextAlignment.CENTER);

        // Create scene and show:
        Pane root = new Pane();
        root.getChildren().add(label);
        stage.setScene(new Scene(root, 120, 60));
        stage.show();
    }
    

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

预览:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2015-03-20
    相关资源
    最近更新 更多