您实际上有几个选项,您选择哪一个将取决于您想要的功能。我将在下面展示每个选项的代码。
-
使用
toggleGroup.getToggles() 列表 - 这已经为ObservableList<Toggle> 中的所有元素提供了索引
- 为每个
RadioButton添加一个UserData属性
-
创建您自己的
RadioButton - 这将允许添加任意数量的属性,类似于选项 2,但代码更简洁(在我看来)
- 创建您自己的属性绑定到所选
RadioButton的索引
使用toggleGroup.getToggles() 列表
如果您将每个 RadioButton 添加到 ToggleGroup,则它们已经在列表中。您可以使用 toggleGroup.getToggles() 访问该列表。然后,您可以简单地在列表中获取它们的索引。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple interface
VBox root = new VBox(10);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
// Create the ToggleGroup
ToggleGroup group = new ToggleGroup();
// Add some RadioButtons to the group
group.getToggles().addAll(
new RadioButton("One"),
new RadioButton("Two"),
new RadioButton("Three"),
new RadioButton("Four"),
new RadioButton("Five")
);
// Add all the RadioButtons to the scene
for (Toggle radioButton : group.getToggles()) {
root.getChildren().add((RadioButton) radioButton);
}
// Now we can get the index any time a RadioButton is selected
group.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
System.out.println("Index #" + group.getToggles().indexOf(newValue) + " selected!");
}
});
// Show the Stage
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
为每个RadioButton添加一个UserData属性
由于RadioButton 是Node,它继承了一个有用的便捷方法[setUserData()][1]。这允许您将单个 Object 属性添加到 Node 以供以后检索。
如果您只想添加这个值,这是一个可行的选择,并且使用起来非常简单。对于每个RadioButton,只需调用该方法并传递一个Object:
rdo1.setUserData(1);
rdo2.setUserData(2);
rdo3.setUserData(3);
rdo4.setUserData(4);
rdo5.setUserData(5);
当您需要时,只需调用getUserData() 方法即可检索该值。
当然,这里的危险在于它不是真正的类型安全的。由于您可以将任何 Java Object 传递给此方法(并会得到一个 Object 作为回报),因此您需要严格控制此处设置的内容并在检索时正确转换:
int radio1Value = (int) rdo1.getUserData();
创建您自己的RadioButton
但是,如果您希望能够为每个 RadioButton 手动分配一个属性值,并且可能包含多个自定义值,您可以简单地创建一个扩展 RadioButton 的类并提供附加属性(即):
class MyRadioButton extends RadioButton {
private int myIndex;
public MyRadioButton(String text, int myIndex) {
super(text);
this.myIndex = myIndex;
}
public int getMyIndex() {
return myIndex;
}
public void setMyIndex(int myIndex) {
this.myIndex = myIndex;
}
}
创建您自己的属性绑定到所选 RadioButton 的索引
同样,如果您只对跟踪所选RadioButton 的索引感兴趣,您可以创建一个IntegerProperty 并将其绑定到一个IntegerBinding,该IntegerBinding 会在RadioButton 选择更改时更新。
// Create a property to hold the index value of the currently selected RadioButton
IntegerBinding selectedRadioButtonIndexBinding = Bindings.createIntegerBinding(() ->
group.getToggles().indexOf(group.getSelectedToggle()), group.getToggles(), group.selectedToggleProperty());
IntegerProperty selectedRadioButtonIndex = new SimpleIntegerProperty();
selectedRadioButtonIndex.bind(selectedRadioButtonIndexBinding);
此时,您只需在新属性中添加Listener:
selectedRadioButtonIndex.addListener((observable, oldValue, newValue) -> {
System.out.println(newValue);
});