按下/选择时操作 JToggleButton 的 ImageIcon

发布于 2024-10-10 01:39:32 字数 603 浏览 0 评论 0原文

我(非常奇迹般地)在写这个问题时回答了我自己的问题,但这是我想与大家分享的一个发现。我知道这些应该是真正的“问题”,但这对我来说是一个巨大的发现。不过,下面有一个可以回答的“问题”。

我创建了一个扩展 JToggleButton 的自定义类。在此类中,我删除了所有默认的 MouseListener,因此当我认为对象准备就绪时,它只会接受鼠标操作。我遇到的问题是操作 JToggleButtons ImageIcon(使用 setIcon() 设置)。如果当我告诉要选择该按钮时该按钮未显示在屏幕上(由于 JScrollPane 而不可见),则 ImageIcon 将消失。同样,如果我在选择按钮时修改 ImageIcon,则 ImageIcon 也会消失。

事实证明,在我的自定义 MouseListener 中,我使用的是 getModel.setSelected(true) 而不是 getModel.setPressed(true)。显然我对选择和按下之间的区别没有清楚的了解,但足以说这解决了我的问题。因此,我的“问题”是对这两个术语的澄清。

总而言之,当您打算使用 setPressed() 时,不要在按钮上使用 setSelected()。追踪这个人是多么痛苦啊!我真诚地希望这对其他人有帮助。电子邮件有效,但容易被垃圾邮件拦截。

I (very miraculously) answered my own question while writing this question , but it was such a find I wanted to share with everyone. I understand these should be true "questions" but it was a monumental find for me. There is a "question" below that can be answered, however.

I created a custom class that extends a JToggleButton. In this class, I remove all default MouseListeners so it will only accept mouse actions when I deem the object ready. The problem I had was manipulating the JToggleButtons ImageIcon (set with setIcon()). If the button was not displayed on screen (not visible due to a JScrollPane) when I told the button to be selected, the ImageIcon would disappear. Similarly, if I modified the ImageIcon while the button was selected, the ImageIcon would also disappear.

It turns out that in my custom MouseListener, I was using getModel.setSelected(true) instead of getModel.setPressed(true). Apparently I don't have a clear understanding of the difference between selected and pressed, but suffice to say this fixed my issue. My "question" would therefore be clarification on these two terms.

To summarize, don't use setSelected() on a button when you mean to use setPressed(). What a pain to track this one down! I sincerely hope this helps someone else. Email is valid but a spam-catcher.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2024-10-17 01:39:32

好吧,我再次找到了自己的答案,所以我想我应该分享并关闭这个问题。当按钮具有复选框/单选图标时,该按钮被“选中”。当按钮被按下时,按钮被“按下”。 JToggleButtons 似乎结合使用这两个属性来正确“单击并按下”按钮。

ImageIcons 消失的原因是因为我无意中混合了 setPressedIcon()/setPressed() 和 setSelectedIcon()/setSelected()。因为我混淆了术语并且没有意识到其中的区别,所以我的用法不一致。

Ok, I found my own answer again, so I thought I'd share and close this question. A button is "selected" when it has a checkbox/radio icon. A button is "pressed" when the button has been pushed. JToggleButtons appear to use both attributes in combination to properly "click and press" the button.

The reason the ImageIcons were disappearing was because I had inadvertently mixed setPressedIcon()/setPressed() and setSelectedIcon()/setSelected(). Because I had confused the terminology and didn't recognize the difference, I was inconsistent in my usage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文