禁用 JPanel 的视觉效果
我正在寻找一种禁用 JPanel 的好方法。我正在为 Java Swing GUI 使用 MVC 设计。我希望在模型处理内容时禁用 JPanel。我尝试过setEnabled(false)。这会禁用 JPanel 上的用户输入,但我希望将其变灰以添加更多视觉效果。
提前致谢!
I'm looking for a good way to disable a JPanel. I'm using a MVC design for a Java Swing GUI. I want the JPanel to be disabled while the model is processing stuff. I've tried setEnabled(false). That disables user input on the JPanel, but I'd like it to be grayed out to add a more visual effect.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您研究过玻璃板吗?它们对于在已经包含组件的区域上进行绘制非常有用。
看一下
Have you looked into Glass Panes? They are useful for painting over areas which already contain components.
Take a look
默认情况下,禁用 JPanel 不会禁用其子组件,只是阻止面板。我建议的解决方案是创建一个 JPanel 子类并重写 setEnabled 方法,如下所示:
Disabling JPanel does not disable its children components by default, just blocks the panel. Solution I recommend is to create a JPanel subclass and override the
setEnabled
method like this:JPanel 在禁用时不会出现任何不同,您必须重写 PaintComponent() 方法才能在禁用时以不同的方式(或使用不同的颜色)绘制它。
像这样的事情可能会起作用:
JPanel doesn't appear any different when disabled, you'll have to override the paintComponent() method to draw it differently (or with a different color) when it is disabled.
Something like this might work:
由于您想要应用视觉效果,因此最好使用 Glasspane。查看这篇文章。
SwingX 已经提供了您需要的组件以及更多功能。查看网站上的演示,了解可用的各种组件。
Since you want to apply a visual effect its better to use Glasspane. Check this article.
SwingX already provides the components you need and much more. Check out the demo on the site for various components available.
另一种解决方案是使用JXLayer框架。它比玻璃板柔韧得多。查看该项目和这篇文章
Another solution is to use JXLayer framework. It is much more flexible then glass pane. Take a look at the project and this article