我需要将2个单选按钮组成一组,然后检索所选按钮的值。

最佳答案

只需使用ToggleGroup

RadioButton radioButton1 = ...
RadioButton radioButton2 = ...

// TODO: add RadioButtons to scene

ToggleGroup toggleGroup = new ToggleGroup();

radioButton1.setToggleGroup(toggleGroup);
radioButton2.setToggleGroup(toggleGroup);

// listen to changes in selected toggle
toggleGroup.selectedToggleProperty().addListener((observable, oldVal, newVal) -> System.out.println(newVal + " was selected"));

您还可以使用以下方法从ToggleGroup中检索选定的单选按钮:
toggleGroup.getSelectedToggle()

万一你想从一个提交按钮或类似的东西的处理程序做到这一点...

07-28 03:36