如何为选中的 JChechbox 赋值?
我正在制作一个简单的货币转换器,它采用 JTextField 中的初始值,然后用户单击与所需货币相对应的 JCheckBox ,转换后的值将显示在另一个中JTextField
。基本上我想知道是否有任何方法可以为已检查的 JCheckBox
分配一个值,我已经环顾四周,但找不到明确的答案,任何帮助将不胜感激。
例如,如果当前从英镑到欧元的兑换率为 1.12244,则在选中 JCheckBox 时将分配该值,因此原始值将乘以兑换率。
I am making a simple currency convertor which takes the initial value in a JTextField
then the user clicks the JCheckBox
corresponding to their desired currency, the converted value will then be displayed in another JTextField
. Basically i would like to know is there any way of assigning a value to a checked JCheckBox
i have looked around and cannot find a clear answer to this, any help would be appreciated.
For example if the current conversion rate from Sterling to euro is 1.12244 this value would be assigned when the JCheckBox is checked, so the the original value would be multiplied by the conversion rate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
认为如果您为
JCheckBox
分配一个action-listener
并在触发此事件时进行转换,会更容易。要检查JCheckBox
是否已选中,您可以使用isSelected()
方法EDIT
实际上我认为您需要使用
JRadioButton
位于ButtonGroup
中,就好像您使用的是复选框一样,有可能会选择多个复选框。 这里是如何使用 ButtonGroup 和触发操作的示例在单选按钮上Think it'd be easier if you assign an
action-listener
to yourJCheckBox
and make the conversion on trigger of this event. To check is aJCheckBox
is checked or not you can use theisSelected()
methodEDIT
Actually i think you need to use
JRadioButton
's in aButtonGroup
for this, as if you are using a checkbox then there is a chance that more than one is selected. Here is an example of how to do use ButtonGroup and trigger action on the radio button这将为您提供复选框的值。
您可以更改 JCheckBox 的操作侦听器上的值
This would give you the value of the check box.
You can change the value on the action-listener of the JCheckBox
// 这是完整的工作代码我希望这会有所帮助
// this is whole working code i hope this this will help
这可能不是最好的解决方案,但您可以尝试一下。
您可以按照 @Balanivash 的建议使用 JRadioButton,而不是使用 JCheckBox。更简单和正确的解决方案将使用 JComboBox。[我在这方面与 @Riduidel 在一起。]
This could not be the best solution but you can try this.
Instead of using JCheckBox you can use JRadioButton as suggested by @Balanivash. More simpler and proper solution will be using JComboBox.[I'm with @Riduidel in this.]
我还认为 JCheckBox 不是执行您想要的操作的最佳选择,但是...
为什么不将 JCheckBox 类扩展为CurrencyConverterCheckBox,您可以在其中
传递货币和当前转换值作为参数。
例如:
那么当用户单击复选框时,您将能够进行转换。您还可以在复选框旁边提供标签(美元到欧元)。您还可以在新复选框中提供一种方法来翻转货币并计算另一个方向的乘数。
亲切的问候
I also think that JCheckBox isn't the best option to do what you want, however...
Why don't you extend the JCheckBox class to a CurrencyConverterCheckBox where you can
pass as arguments the currencies and the current value of the conversion.
e.g.:
Then you will be able to do the conversion when the user clicks the checkbox. You can also provide labels next to the checkboxes (USD to EUR). And you can also provide a method in your new checkbox to flip the currencies and calculate the multiplication factor in the other direction.
kind regards
最简单的做法是这样的:
What it's easiest to do is something like this: