[英]get the name of the selected radio button from button group
我想從java中的按鈕組返回選定的單選按鈕名稱:
ButtonGroup bg = new ButtonGroup();
bg.add(radiobutton1);
bg.add(radiobutton2);
bg.add(radiobutton3);
我想你可以遍歷單選按鈕,如果isSelected()為true,則為每個按鈕循環,然后調用button.getText()來獲取名稱。
for (AbstractButton button : bg.getElements())
if (button.isSelected())
return button.getText();
為什么要按鈕名稱? 它用處不大。 你可以,但是,使用獲得的所有按鈕引用bg.getElements()
你可以得到每一個與引用的文本button.getText()
這兩者都是比你給的名字更多的使用按鈕。 如果您真的想要名稱,可以創建按鈕,然后調用radiobutton1.setName("radiobutton1")
,然后在引用上使用getName()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.