For loop 如何查找已单击单选按钮的索引或信息

For loop 如何查找已单击单选按钮的索引或信息,for-loop,arraylist,javafx,radio-button,For Loop,Arraylist,Javafx,Radio Button,我想知道,当按下某个单选按钮或传递myBuns时,是否有找到索引的方法。当点击RadioButton时获取(I)。使用下面的代码创建单选按钮 RadioButton rButton; for (i = 0; i < myBuns.size(); i ++){ rButton = new RadioButton("" + myBuns.get(i)); rButton.setToggleGroup(bunGroup); rButton.setO

我想知道,当按下某个单选按钮或传递myBuns时,是否有找到索引的方法。当点击RadioButton时获取(I)。使用下面的代码创建单选按钮

RadioButton rButton;
for (i = 0; i < myBuns.size(); i ++){
        rButton = new RadioButton("" + myBuns.get(i));
        rButton.setToggleGroup(bunGroup);
        rButton.setOnAction(this);
        this.getChildren().add(rButton);
    }
RadioButton按钮;
对于(i=0;i

谢谢你的帮助和建议

您可以使用以下方法获取所选切换的索引:

toggleGroup.getToggles().indexOf(toggleGroup.getSelectedToggle());
 ((RadioButton) toggleGroup.getSelectedToggle()).getText();
和所选切换的文本,使用:

toggleGroup.getToggles().indexOf(toggleGroup.getSelectedToggle());
 ((RadioButton) toggleGroup.getSelectedToggle()).getText();
通过将代码放置在所选切换属性的更改侦听器中,您可以监视所选切换何时更改,并根据需要采取操作

示例应用程序

导入javafx.application.application;
导入javafx.collections.*;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.Font;
导入javafx.stage.stage;
导入java.util.stream.*;
公共类ToggleIndexer扩展应用程序{
@凌驾
public void start(final Stage)引发异常{
ToggleGroup ToggleGroup=新的ToggleGroup();
ObservableList按钮=IntStream.range(0,5)
.mapToObj(i->新单选按钮(“单选“+i))
.collect(Collectors.toCollection(FXCollections::observearraylist));
toggleGroup.getToggles().setAll(按钮);
标签selectedIndex=新标签();
选择dex.setFont(Font.Font(“monospace”));
标签selectedItem=新标签();
选择editem.setFont(Font.Font(“monospace”));
toggleGroup.selectedToggleProperty().addListener((可观察、旧值、新值)->{
if(newValue==null){
选择的索引设置文本(“”);
选择editem.setText(“”);
}否则{
最终int selectedIndex值=
toggleGroup.getToggles().indexOf(newValue);
selectedIndex.setText(“Selected Index:+selectedIndexValue”);
最终字符串selectedItemText=
((RadioButton)toggleGroup.getSelectedToggle()).getText();
selectedItem.setText(“所选项目:+selectedItemText”);
}
});
VBox布局=新的VBox(8);
布局。设置填充(新插图(10));
布局。设置宽度(250);
layout.getChildren().setAll(按钮);
layout.getChildren().addAll(selectedItem,selectedIndex);
舞台场景(新场景(布局));
stage.show();
}
公共静态void main(字符串[]args)引发异常{
发射(args);
}
}

您可以使用以下方法获取所选切换的索引:

toggleGroup.getToggles().indexOf(toggleGroup.getSelectedToggle());
 ((RadioButton) toggleGroup.getSelectedToggle()).getText();
和所选切换的文本,使用:

toggleGroup.getToggles().indexOf(toggleGroup.getSelectedToggle());
 ((RadioButton) toggleGroup.getSelectedToggle()).getText();
通过将代码放置在所选切换属性的更改侦听器中,您可以监视所选切换何时更改,并根据需要采取操作

示例应用程序

导入javafx.application.application;
导入javafx.collections.*;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.Font;
导入javafx.stage.stage;
导入java.util.stream.*;
公共类ToggleIndexer扩展应用程序{
@凌驾
public void start(final Stage)引发异常{
ToggleGroup ToggleGroup=新的ToggleGroup();
ObservableList按钮=IntStream.range(0,5)
.mapToObj(i->新单选按钮(“单选“+i))
.collect(Collectors.toCollection(FXCollections::observearraylist));
toggleGroup.getToggles().setAll(按钮);
标签selectedIndex=新标签();
选择dex.setFont(Font.Font(“monospace”));
标签selectedItem=新标签();
选择editem.setFont(Font.Font(“monospace”));
toggleGroup.selectedToggleProperty().addListener((可观察、旧值、新值)->{
if(newValue==null){
选择的索引设置文本(“”);
选择editem.setText(“”);
}否则{
最终int selectedIndex值=
toggleGroup.getToggles().indexOf(newValue);
selectedIndex.setText(“Selected Index:+selectedIndexValue”);
最终字符串selectedItemText=
((RadioButton)toggleGroup.getSelectedToggle()).getText();
selectedItem.setText(“所选项目:+selectedItemText”);
}
});
VBox布局=新的VBox(8);
布局。设置填充(新插图(10));
布局。设置宽度(250);
layout.getChildren().setAll(按钮);
layout.getChildren().addAll(selectedItem,selectedIndex);
舞台场景(新场景(布局));
stage.show();
}
公共静态void main(字符串[]args)引发异常{
发射(args);
}
}

虽然我想我已经明白了你的问题,但将来最好为这样的问题提供一个解决方案(例如,可以复制和粘贴的可编译样本,无需更改或添加即可复制问题),因为这将使您获得对您更有用的答案的可能性更高。虽然我认为我已经了解您的问题,但在将来,最好为这样的问题提供一个解决方案(例如,可以复制和粘贴的可编译样本,以复制问题,而无需更改或添加),因为这会让你更有可能得到一个对你更有用的答案。谢谢我明白了。谢谢