我的JavaFx应用程序中有此方法来创建RadioButton。
private HBox createModesRadios(IntegerProperty count, Mode... modes) {
ToggleGroup group = new ToggleGroup();
HBox result = new HBox(50);
result.setPadding(new Insets(20, 0, 0, 0));
result.setAlignment(Pos.CENTER);
for (Mode mode : modes) {
RadioButton radio = new RadioButton(mode.getText());
radio.setToggleGroup(group);
radio.setUserData(mode);
result.getChildren().add(radio);
}
if (modes.length > 0) {
group.selectToggle((Toggle) result.getChildren().get(0));
count.bind(Bindings.createIntegerBinding(() -> ((Mode) group.getSelectedToggle().getUserData()).getCount(), group.selectedToggleProperty()));
} else {
count.set(0);
}
return result;
}
它initialize()通过以下方式在Controller类中的方法中调用HBox radioBox = createModesRadios(elementCount, modes);。
这是助手类模式:
public class Mode {
private final String text;
private final int count;
public Mode(String text, int count) {
this.text = text;
this.count = count;
}
public String getText() {
return text;
}
public int getCount() {
return count;
}
}
如何保存用户选择的按钮?将所选按钮String的mode.getText()方法存储在变量中会很棒。另外,我想重新设置先前选择的按钮,以便应用程序可以记住选择。
精慕HU
相关分类