创建允许 N 个复选框的按钮组
我正在使用 Swing 使用 Java 进行编程。
我目前正在开发一个应用程序,它允许用户显示 2 个或更少的爱好。爱好的清单是有限的。我想为用户提供一个复选框列表来选择这些项目,允许他们最多检查 2 个选项,但不能再多了。
我有哪些实施方案?是否有类似 ButtonGroup 的对象可以容纳这些项目?
我试图避免为此使用 2 个组合框,就好像任意 2 个限制增加(到大小 n)一样,缩放会很痛苦。
I am programming in Java, using Swing.
I am currently working with an application which allows the user to display 2 or less hobbies. The list of hobbies is finite. I would like to provide a user with a list of checkboxes to select these items from, allowing them to check up to 2 options, but no more.
What are my options for implementing this? Is there a ButtonGroup like object that can hold these items?
I'm trying to avoid having 2 Combo boxes for this, as if the arbitrary 2 limit is increased (to size n) It'd be a pain to scale.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
连接每个复选框的操作事件并计算每次调用此处理程序时检查了多少个复选框。如果选中了允许的数量,则禁用每个未选中的复选框,否则启用它。
否则,只需禁用提交按钮并添加一个标签向用户解释情况 - 并在复选框的活动数量降至阈值以下后立即重新启用它。
Hook up the action event of each checkbox and count how many are checked every time this handler is invoked. If as many are checked as it is allowed, disable every unchecked checkbox, else enable it.
Otherwise, just disable the submittion button and add a label explaining the situation to the user - and re-enable it as soon as the active number of checkboxes drops below the threshhold.
考虑使用复选框列表(每个项目旁边带有复选框的列表控件)。这种方法比为每个项目生成单独的复选框具有更好的扩展性。
Consider using checkbox list (list control with checkbox next to each item). This approach scales better than producing a separate checkbox per item.