Java 中有标准的 GUI 切换开关吗?
Swing 中是否有提供 GUI 切换开关的标准实现或库?我知道 Swing 提供了一个切换按钮,但用户体验还有一些不足之处。我正在寻找这个:
另外,这种类型的控件有规范术语吗? Apple HIG 将其称为 UISwitch。我也尝试过搜索“切换开关”,但运气不佳。 (大量 JavaScript 结果,但没有原生的。)
Is there a standard implementation or library that provides a GUI toggle switch in Swing? I know Swing provides a toggle button, but the UX leaves a bit to be desired. I'm looking for this:
Also, is there a canonical term for this type of control? The Apple HIG refer to it as a UISwitch. I also tried searching for "toggle switch", but I didn't have much luck. (Plenty of JavaScript results, but nothing native.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过使用两个图标来代表 on & 来模仿它。关闭,然后将它们设置为 JToggleButton。
顺便说一句,用户希望看到逻辑一致且一致的 GUI,代表“最不令人惊讶的路径”,开发人员认为用户想要一个“漂亮、聪明”的 GUI(并且他们可以设计一个)。他们到底为什么想要对标准切换按钮进行这样的控制?
You might mimic it by using two icons to represent on & off, then set those to a
JToggleButton
.As an aside, users want to see logical and consistent GUIs that represent the 'path of least surprise', it's developers that think users want a 'beautiful, clever' GUI (and that they can design one). Why on earth would they want such a control over a standard toggle-button?
我不知道标准的,但创建 Steel Series 组件的 Gerrit Grunwald 创建了该组件的实现他称之为 Steel Checkbox
I don't know of a standard one, but Gerrit Grunwald who creates the Steel Series components created an implementation of this that he calls a Steel Checkbox
我猜我迟到了 6 年,但对于那些仍在寻找简单解决方案的人来说:
只需将其复制到 ToggleSwitch.java 中即可。
您可以将其添加到 JFrame 中,如下所示:
Guess I am 6 years late, but nevertheless for those who still search for a simple solution:
Just copy it in a ToggleSwitch.java.
You can add it to your JFrame like this:
Swing 没有像您所描述的那样的标准开关。如果您找不到第三方,最好的选择就是简单地编写一个。我处理它的方式将是一个像这样的简单结构:
•
JLabel
• 覆盖
paintComponent
• 使用诸如
isOn()
之类的方法检查状态
• 添加
MouseListener
来切换状态。• 定制绘画将考虑标签值和尺寸。
我可以向您发送我不久前写的一个,但您可能对自己想要的东西有一个非常具体的想法,因此可能值得花半个小时来构建它。
Swing doesn't have a standard switch like the one you described. Your best bet if you can't find a third party one would be to simply write one. The way I'd approach it would be a simple structure like this:
•
JLabel
• Override
paintComponent
• Check for state with something like
isOn()
• Add a
MouseListener
to toggle state.• Custom painting would take into account label values and sizes.
I could send you one that I wrote some time ago, but you probably have a very specific idea of what you want so might be worth the half an hour constructing it.