在JavaFx中,是否可以将方框(Box)与复选框中的文本内容上对齐?

huangapple go评论107阅读模式
英文:

JavaFx : Is it possible to align the Box above the Text content in a checkbox?

问题

我想要实现类似于以下的效果:

在JavaFx中,是否可以将方框(Box)与复选框中的文本内容上对齐?

英文:

I want to achieve somthing like this :

在JavaFx中,是否可以将方框(Box)与复选框中的文本内容上对齐?

答案1

得分: 5

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

App:

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.control.CheckBox;
  4. import javafx.scene.control.ContentDisplay;
  5. import javafx.scene.control.Label;
  6. import javafx.scene.input.MouseButton;
  7. import javafx.scene.layout.Pane;
  8. import javafx.scene.text.TextAlignment;
  9. import javafx.stage.Stage;
  10. public class App extends Application {
  11. @Override
  12. public void start(Stage stage) {
  13. // 首先创建一个分离的标签:
  14. Label label = new Label("Am efectuat toate");
  15. // 创建一个没有文本的复选框
  16. CheckBox checkBox = new CheckBox();
  17. // 将复选框放置为标签的图形节点:
  18. label.setGraphic(checkBox);
  19. // 将框放在标签的顶部显示:
  20. label.setContentDisplay(ContentDisplay.TOP);
  21. // 在标签的主鼠标按钮单击时切换复选框的选择属性:
  22. label.setOnMouseClicked(mouseEvent -> {
  23. if (mouseEvent.getButton() == MouseButton.PRIMARY)
  24. checkBox.setSelected(!checkBox.isSelected());
  25. });
  26. // 如果您想要在第二行中有最后一个词“toate”:
  27. label.setWrapText(true);
  28. label.setMaxWidth(80);
  29. label.setTextAlignment(TextAlignment.CENTER);
  30. // 创建场景并显示:
  31. Pane root = new Pane();
  32. root.getChildren().add(label);
  33. stage.setScene(new Scene(root, 120, 60));
  34. stage.show();
  35. }
  36. public static void main(String[] args) {
  37. launch();
  38. }
  39. }

预览:

在JavaFx中,是否可以将方框(Box)与复选框中的文本内容上对齐?

英文:

Below is one simple example of achieving what you want. The more professional way would be to implement a custom skin. Maybe someone else will share it.

App:

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.control.CheckBox;
  4. import javafx.scene.control.ContentDisplay;
  5. import javafx.scene.control.Label;
  6. import javafx.scene.input.MouseButton;
  7. import javafx.scene.layout.Pane;
  8. import javafx.scene.text.TextAlignment;
  9. import javafx.stage.Stage;
  10. public class App extends Application {
  11. @Override
  12. public void start(Stage stage) {
  13. // Create a separated label at first:
  14. Label label = new Label("Am efectuat toate");
  15. // Create a check box without a text
  16. CheckBox checkBox = new CheckBox();
  17. // Put the check box as the graphic node of the label:
  18. label.setGraphic(checkBox);
  19. // Put the box on top of the label:
  20. label.setContentDisplay(ContentDisplay.TOP);
  21. // Toggle selected property of checkbox on label primary mouse button click:
  22. label.setOnMouseClicked(mouseEvent -> {
  23. if (mouseEvent.getButton() == MouseButton.PRIMARY)
  24. checkBox.setSelected(!checkBox.isSelected());
  25. });
  26. // If you want to have the last word "toate" in a second line:
  27. label.setWrapText(true);
  28. label.setMaxWidth(80);
  29. label.setTextAlignment(TextAlignment.CENTER);
  30. // Create scene and show:
  31. Pane root = new Pane();
  32. root.getChildren().add(label);
  33. stage.setScene(new Scene(root, 120, 60));
  34. stage.show();
  35. }
  36. public static void main(String[] args) {
  37. launch();
  38. }
  39. }

Preview:

在JavaFx中,是否可以将方框(Box)与复选框中的文本内容上对齐?

huangapple
  • 本文由 发表于 2020年8月13日 18:20:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63393020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定