问题描述
我有一个包含表单上的单选按钮的swing应用程序。我有 ButtonGroup
,但是,查看可用的方法,我似乎无法获得所选 JRadioButton $ c $的名称C>。这是我到目前为止所能说的:
I have a swing application that includes radio buttons on a form. I have the ButtonGroup
, however, looking at the available methods, I can't seem to get the name of the selected JRadioButton
. Here's what I can tell so far:
-
从ButtonGroup,我可以执行
getSelection()
返回ButtonModel
。从那里,我可以执行getActionCommand
,但这似乎并不总是有效。我尝试了不同的测试并获得了不可预测的结果。
From ButtonGroup, I can perform a
getSelection()
to return theButtonModel
. From there, I can perform agetActionCommand
, but that doesn't seem to always work. I tried different tests and got unpredictable results.
同样来自 ButtonGroup
,我可以从中获取枚举的getElements()
。但是,然后我必须遍历每个按钮,只是为了检查它是否是所选择的。
Also from ButtonGroup
, I can get an Enumeration from getElements()
. However, then I would have to loop through each button just to check and see if it is the one selected.
有没有更简单的方法来找出选择了哪个按钮?我正在Java 1.3.1和Swing中编程。
Is there an easier way to find out which button has been selected? I'm programing this in Java 1.3.1 and Swing.
推荐答案
我只是循环你的 JRadioButtons
并调用 isSelected()
。如果你真的想从 ButtonGroup
开始,你只能进入模型。您可以将模型与按钮匹配,但如果您可以访问按钮,为什么不直接使用它们?
I would just loop through your JRadioButtons
and call isSelected()
. If you really want to go from the ButtonGroup
you can only get to the models. You could match the models to the buttons, but then if you have access to the buttons, why not use them directly?
这篇关于如何从ButtonGroup中选择哪个JRadioButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!