FX - 在 Grid 中对齐 CheckBox 的建议
FX - Suggestions for aligning CheckBox in Grid
在此处从 Swing 转换为 FX。我们有一些 CheckBox,其中 Label 出现在 CheckBox 的左侧。我们通过调用
来实现这一点
setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
问题在于,在某些 GridPanes 中,我们将在第 0 列中有一个标签,在第 1 行的第 1 列中有一个节点,然后在第二行中有一个复选框。理想情况下,CheckBox 和 Node 彼此对齐。我目前通过将 CheckBox 的 columnSpan 设置为 2 并添加一些右填充来对齐字段来完成此操作。有更简单的方法吗?
我们之前的解决方案只是将标签与复选框分开,但这导致我们在单击标签时失去了 selecting/deselecting 复选框的功能。
编辑:
我正在尝试找出使 CheckBox 与 Field 对齐的最佳方法。
我看不到 "nice" 方法来做你想做的事:我能想出的最好办法是将标签与复选框分开,并注册一个带有标签的鼠标侦听器以进行切换复选框的状态。也许其他人可以看到更优雅的方式来做到这一点。
SSCCE:
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class AlignedCheckBox extends Application {
@Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
Label checkboxLabel = new Label("Selected:");
CheckBox checkBox = new CheckBox();
checkboxLabel.setLabelFor(checkBox);
checkboxLabel.setOnMouseClicked(e -> checkBox.setSelected(! checkBox.isSelected()));
Label textFieldLabel = new Label("Enter text:");
TextField textField = new TextField();
grid.addRow(0, checkboxLabel, checkBox);
grid.addRow(1, textFieldLabel, textField);
ColumnConstraints leftCol = new ColumnConstraints();
leftCol.setHalignment(HPos.RIGHT);
leftCol.setHgrow(Priority.SOMETIMES);
ColumnConstraints rightCol = new ColumnConstraints();
rightCol.setHalignment(HPos.LEFT);
rightCol.setHgrow(Priority.ALWAYS);
grid.getColumnConstraints().addAll(leftCol, rightCol);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20));
Scene scene = new Scene(grid);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
在此处从 Swing 转换为 FX。我们有一些 CheckBox,其中 Label 出现在 CheckBox 的左侧。我们通过调用
来实现这一点setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
问题在于,在某些 GridPanes 中,我们将在第 0 列中有一个标签,在第 1 行的第 1 列中有一个节点,然后在第二行中有一个复选框。理想情况下,CheckBox 和 Node 彼此对齐。我目前通过将 CheckBox 的 columnSpan 设置为 2 并添加一些右填充来对齐字段来完成此操作。有更简单的方法吗?
我们之前的解决方案只是将标签与复选框分开,但这导致我们在单击标签时失去了 selecting/deselecting 复选框的功能。
编辑:
我正在尝试找出使 CheckBox 与 Field 对齐的最佳方法。
我看不到 "nice" 方法来做你想做的事:我能想出的最好办法是将标签与复选框分开,并注册一个带有标签的鼠标侦听器以进行切换复选框的状态。也许其他人可以看到更优雅的方式来做到这一点。
SSCCE:
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class AlignedCheckBox extends Application {
@Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
Label checkboxLabel = new Label("Selected:");
CheckBox checkBox = new CheckBox();
checkboxLabel.setLabelFor(checkBox);
checkboxLabel.setOnMouseClicked(e -> checkBox.setSelected(! checkBox.isSelected()));
Label textFieldLabel = new Label("Enter text:");
TextField textField = new TextField();
grid.addRow(0, checkboxLabel, checkBox);
grid.addRow(1, textFieldLabel, textField);
ColumnConstraints leftCol = new ColumnConstraints();
leftCol.setHalignment(HPos.RIGHT);
leftCol.setHgrow(Priority.SOMETIMES);
ColumnConstraints rightCol = new ColumnConstraints();
rightCol.setHalignment(HPos.LEFT);
rightCol.setHgrow(Priority.ALWAYS);
grid.getColumnConstraints().addAll(leftCol, rightCol);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20));
Scene scene = new Scene(grid);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}