Java 中的 JButton 中可以有 JCheckBox 吗?
我有一个上传程序,其中有一个显示一组上传行的 JTable。我有一个标记为“清除已完成”的 JButton,单击该按钮会删除已完成上传的行。现在我想要一个 JCheckBox/JButton 组合(就像 gmail 中的复选框/下拉组合),这样如果选中该复选框,这些行将被自动删除。
我创建了一个扩展 JButton 的类并重写构造函数以添加 JCheckBox。为 JCheckBox 和 JButton 添加了 ActionListener。看起来像这样。
https://i.sstatic.net/KcBHv.png
一切正常。但我很纠结到底用还是不用。因为它不是 JButton API 的预期用途,而且我从未在任何 Java 应用程序中见过这样的控制(到目前为止),所以我担心它是否会给用户带来任何问题。
这会导致运行时破坏 UI 等问题吗?或者它是否可能不适用于所有操作系统? (我使用的是Windows7)
或者这不是很好的设计方法? (“用户不得感到惊讶”规则。我想他们会理解。我还为该复选框提供了一个工具提示)
请分享您的意见。 谢谢。
I have a uploader program that has a JTable displaying a set of rows of uploads. I have a JButton labeled "Clear Completed" which when clicked removes the rows of completed uploads. Now I'd like to have a JCheckBox/JButton combination (like the checkbox/dropdown combination in gmail) so that if the checkbox is checked, the rows will be removed automatically.
I have created a class that extends JButton and overridden the constructor to add a JCheckBox. Added ActionListeners for both JCheckBox and JButton. It looks like this.
https://i.sstatic.net/KcBHv.png
Everything works perfectly. But I'm in a dilemma whether to use it or not. Since it's not an intended use of JButton API and I've never seen such control (so far) in any Java app so I'm afraid if it'll cause any problems for users.
Will this cause any problems like breaking UI during runtime? Or is it possible it won't work in all OSes? (I'm using Windows7)
Or it is not good designing approach? ("Users must not be surprised" rule.. I guess they'll understand. I've also included a tooltip for that checkbox)
Please share your opinions.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
吗?例如,如果使用本机小部件来呈现按钮,您可能会遇到呈现问题。或者复选框可能不会响应单击,因为按钮吞没了事件。
并不真地。大多数用户会认为您的布局有错误,因为他们以前从未见过按钮内的复选框。除了任何渲染问题(当我按下按钮时复选框会是什么样子?它会消失还是以奇怪的颜色渲染?),我认为您应该避免这种情况。
使用标准方法:按钮上方带有文本(“自动清除”)的复选框。当复选框被选择为“关闭”时,可能会更改按钮文本。
Maybe. You can, for example, get rendering problems if a native widget is used to render the button. Or the checkbox might not respond to clicks because the button swallows the event.
Not really. Most users will think that your layout has a bug because they've never seen a checkbox inside of a button before. Along with any rendering issues (what will the checkbox look like when I press the button? Will it vanish or render in odd colors?), I feel that you should avoid this.
Use the standard approach: Checkbox with text ("Clear automatically") above button. Maybe change the button text when the checkbox gets selected to "Close".
我认为这不会成为问题。
我个人认为将复选框放在 JButton 的左侧会更清晰。无论如何,这取决于你。
此外,执行的快速移动可能会让用户单击按钮而不是复选框。
I think this won't be a problem.
Personally I think that putting the checkbox at left of the JButton would be more clear. Anyway it's up to you.
In addition, quick movements performed may let the user click on the button rather than on the checkbox.
万一有人想知道你会如何做到这一点。您可能需要添加 EmptyBorder(4,4,4,4) 以保持与按钮相似的大小。我通常将它们与其他按钮一起包装在网格布局中。
In case any one is wondering how you might do this. You will probably have to add an EmptyBorder(4,4,4,4) to keep the size similar to that of a button. I usually wrap them in a grid layout with other buttons.