禁用 JPanel 的视觉效果

发布于 2024-08-25 06:35:04 字数 221 浏览 3 评论 0原文

我正在寻找一种禁用 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 技术交流群。

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

发布评论

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

评论(5

七禾 2024-09-01 06:35:04

您研究过玻璃板吗?它们对于在已经包含组件的区域上进行绘制非常有用。
看一下

Have you looked into Glass Panes? They are useful for painting over areas which already contain components.
Take a look

捶死心动 2024-09-01 06:35:04

默认情况下,禁用 JPanel 不会禁用其子组件,只是阻止面板。我建议的解决方案是创建一个 JPanel 子类并重写 setEnabled 方法,如下所示:

class JDisablingPanel extends JPanel {
    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        this.setEnabledRecursive(this, enabled);
    }

    protected void setEnabledRecursive(Component component, boolean enabled) {
        if (component instanceof Container) {
            for (Component child : ((Container) component).getComponents()) {
                child.setEnabled(enabled);

                if (!(child instanceof JDisablingPanel)) {
                    setEnabledRecursive(child, enabled);
                }
            }
        }
    }
}

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:

class JDisablingPanel extends JPanel {
    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        this.setEnabledRecursive(this, enabled);
    }

    protected void setEnabledRecursive(Component component, boolean enabled) {
        if (component instanceof Container) {
            for (Component child : ((Container) component).getComponents()) {
                child.setEnabled(enabled);

                if (!(child instanceof JDisablingPanel)) {
                    setEnabledRecursive(child, enabled);
                }
            }
        }
    }
}
软甜啾 2024-09-01 06:35:04

JPanel 在禁用时不会出现任何不同,您必须重写 PaintComponent() 方法才能在禁用时以不同的方式(或使用不同的颜色)绘制它。
像这样的事情可能会起作用:

protected void paintComponent(Graphics g) {
    if (this.isOpaque()) {
        Color color = (this.isEnabled()) ? this.getBackground() : this.getBackground().brighter();
        g.setColor(color);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
}

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:

protected void paintComponent(Graphics g) {
    if (this.isOpaque()) {
        Color color = (this.isEnabled()) ? this.getBackground() : this.getBackground().brighter();
        g.setColor(color);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
}
夜夜流光相皎洁 2024-09-01 06:35:04

由于您想要应用视觉效果,因此最好使用 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.

二智少女 2024-09-01 06:35:04

另一种解决方案是使用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

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