本文介绍了如何从 ButtonGroup 中选择哪个 JRadioButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含表单上的单选按钮的 Swing 应用程序.我有 ButtonGroup,但是,查看可用的方法,我似乎无法获得所选 JRadioButton 的名称.以下是我目前能说的:

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 the ButtonModel. From there, I can perform a getActionCommand, 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:34