我需要制作一组2个单选按钮,然后检索所选按钮的值。
答案 0 :(得分:9)
只需使用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()
如果你想从提交按钮的处理程序或类似的东西......
执行此操作