JCheckBoxMenuItem 只选择了一个 [英] JCheckBoxMenuItem only one selected

查看:78
本文介绍了JCheckBoxMenuItem 只选择了一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想要一个包含三个 JCheckBoxMenuItem 的 JMenu Level,比如 Easy、normal 和 Expert.当然,只能选中一个,如果选中了,则不能取消选中(启用(false)),因为它是唯一的.我希望当一个 JCheck 被选中时,所有其他人都没有被选中.

So I want to have a JMenu Level with three JCheckBoxMenuItem like Easy, normal and expert. Of course, only one can be checked and if one is checked, it can't be unchecked (enabled(false)) because it's the only one. I want when one JCheck is checked, all others are unchecked.

所以看起来很简单,但我找到的唯一解决方案是使用大量if"条件来实现.

So it seems easy, but the only solution I found is to do it with a lot of "if" conditions.

有没有更好的算法来做到这一点?

Is there a way to do it with a better algorithm ?

推荐答案

听起来您最好使用 JRadioButton,因为复选框通常用于多选选项,而单选按钮用于单选.可以使用 ButtonGroup 将 JRadioButton 组合在一起,该 ButtonGroup 一次只允许选择一个.

It sounds like you'd be better off using a JRadioButton since check boxes are generally used for multiple-choice options and radio buttons for a single selection out of many. JRadioButtons can be grouped together using a ButtonGroup which allows only one selected at a time.

这篇关于JCheckBoxMenuItem 只选择了一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆