我的JavaFx應(yīng)用程序中有此方法來創(chuàng)建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類中的方法中調(diào)用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()方法存儲在變量中會很棒。另外,我想重新設(shè)置先前選擇的按鈕,以便應(yīng)用程序可以記住選擇。
1 回答

精慕HU
TA貢獻1845條經(jīng)驗 獲得超8個贊
您可以在控制器類內(nèi)的變量聲明中添加以下內(nèi)容: private List<RadioButton> radioButtonsList = new ArrayList<>();
然后,您可以for在您提到的方法中在循環(huán)內(nèi)添加類似的內(nèi)容
...
radioButtonsList.add(radio);
...
之后,您可以調(diào)用所需的按鈕 radioButtonList.get()
添加回答
舉報
0/150
提交
取消