gpt4 book ai didi

JavaFX - 无法将 ToggleButtons 放入 ToggleGroup

转载 作者:行者123 更新时间:2023-11-29 08:41:37 26 4
gpt4 key购买 nike

我有一个应用程序,用户应该只能选择一个按钮。

我的问题是,我无法将 ToggleButton 放入 ToggleGroup 中,这应该可以解决我的问题。

我使用 if 语句创建了 ToggleButton。我将代码缩减为重要部分。有帮助吗?

我的代码:

public SymbolTableCell() {
}

@FXML
public void initialize() {
collectFonts();
buildGridPaneAddButtons();
listView.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
buildGridPaneAddButtons();
@FXML
public void buildGridPaneAddButtons() {
gridPane.getChildren().clear();
int numRows = 14;
int numColumns = 14;
for (int row = 0; row < numRows; row++) {
RowConstraints rc = new RowConstraints();
rc.setFillHeight(true);
rc.setVgrow(Priority.ALWAYS);
gridPane.getRowConstraints().add(rc);
}
for (int col = 0; col < numColumns; col++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setFillWidth(true);
cc.setHgrow(Priority.ALWAYS);
gridPane.getColumnConstraints().add(cc);
}

String selectedFamily = listView.getSelectionModel().getSelectedItem();
if (selectedFamily != null) {
System.out.println("selected Font: '" + selectedFamily + "'");
for (int i = 0; i < 196; i++) {
Font selectedFont = Font.font(selectedFamily, 18.0);
Character character = new Character((char) (i));
java.awt.Font awtFont = new java.awt.Font(selectedFamily, 0, 40);

if (awtFont.canDisplay(character.charValue()) {
ToggleButton buttonForSymbols = createButton("" + character);
buttonForSymbols.setFont(selectedFont);
buttonForSymbols.setAlignment(Pos.CENTER);

// Here I try to get the ToggleButtons into the Group.
ToggleGroup groupForToggleButtons = new ToggleGroup();
buttonForSymbols.setToggleGroup(groupForToggleButtons);

HBox symbolHBox = new HBox();
symbolHBox.getChildren().add(buttonForSymbols);

Label symbolLabel = new Label();
symbolLabel.setText("" + i);

Separator symbolSeparator = new Separator();

BorderPane symbolBorderPane = new BorderPane();
symbolBorderPane.setTop(symbolHBox);
symbolBorderPane.setCenter(symbolSeparator);
symbolBorderPane.setBottom(symbolLabel);
symbolLabel.setAlignment(Pos.CENTER);
symbolBorderPane.setAlignment(symbolLabel, Pos.CENTER);

// when ToggleButton selected, set new Style of BorderPane
symbolBorderPane.styleProperty()
.bind(Bindings.when(buttonForSymbols.selectedProperty())
.then("-fx-border-color: red; -fx-border-width: 1.5;")
.otherwise("-fx-border-color: black; -fx-border-width: 1;"));
gridPane.add(symbolBorderPane, i % numRows, i / numColumns);

}
}
}
}

// ToggleButton to use .then .otherwise style properties
private ToggleButton createButton(String text) {
ToggleButton buttonPanelForSymbols = new ToggleButton(text);
buttonPanelForSymbols.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
buttonPanelForSymbols.setOnAction(e -> System.out.println("ok"));
buttonPanelForSymbols.setStyle(
"-fx-border-color: transparent; -fx-border-width: 0; -fx-background-radius: 0; -fx-background-color: transparent;");
return buttonPanelForSymbols;
}

最佳答案

在这一行:

ToggleGroup groupForToggleButtons = new ToggleGroup();

您正在创建一个新的 ToggleGroup对于每个 ToggleButton当您在 for loop 中执行此命令时生成按钮。

创建 ToggleGroup循环外:

ToggleGroup groupForToggleButtons = new ToggleGroup();
for (int i = 0; i < 196; i++) {
...
}

然后在循环内使用这个 ToggleGroup每个实例 ToggleButton (删除循环内的 ToggleGroup 初始化)。

注意:您甚至可以将其封装在 createButton 中方法:

private ToggleButton createButton(String text, ToggleGroup group) {
ToggleButton buttonPanelForSymbols = new ToggleButton(text);
// Current code here
buttonPanelForSymbols.setToggleGroup(group)
return buttonPanelForSymbols;
}

然后在循环中:

ToggleButton buttonForSymbols = createButton("" + character, groupForToggleButtons);

同时删除每个 ToggleGroup与循环相关。

关于JavaFX - 无法将 ToggleButtons 放入 ToggleGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39719483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com