SwingX 和 JToggleButton

发布于 2024-12-03 07:09:24 字数 128 浏览 3 评论 0原文

在我的应用程序中,我使用 SwingX 库中的 JXButton,我真的很喜欢画家方法。现在我必须将我的简单按钮更改为切换按钮,并为未选择/选择状态使用不同的画家。不幸的是我找不到 JXToggleButton。有没有办法保持画家方法的优势?

In my application i'm using JXButton from the SwingX library, i really like the painter methods. Now i have to change my simple button to toggle button and have different painter for the unselected/selected state. Unfortunately i couldn't find JXToggleButton. Is there a solution to keep the benefit of painter methods ?

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

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

发布评论

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

评论(1

且行且努力 2024-12-10 07:09:24

创建 JXType 组件实际上相当容易。您可能需要对边界、焦点等进行一些额外的检查。但这是您将采取的一般方法

public class JXToggleButton extends JToggleButton implements FocusListener, Paintable {
    private Painter<JTextField> backgroundPainter;
    private Painter<JTextField> foregroundPainter;

    public void setBackgroundPainter(Painter<JTextField> painter) {
        this.backgroundPainter = painter;
    }

    public void setForegroundPainter(Painter<JTextField> painter) {
        this.foregroundPainter = painter;
    }

    @Override
    protected void paintComponent(Graphics g) {

        if (backgroundPainter != null) {
            this.backgroundPainter.paint((Graphics2D) g, this, getWidth(), getHeight());
        }

        super.paintComponent(g);

        if (foregroundPainter != null) {
            foregroundtPainter.paint((Graphics2D) g, this, getWidth(), getHeight());
        }

    }
}

It's actually fairly easy to create a JXType component. You may need to do some extra checks for borders, focus, etc.. but this is the general approach you would take

public class JXToggleButton extends JToggleButton implements FocusListener, Paintable {
    private Painter<JTextField> backgroundPainter;
    private Painter<JTextField> foregroundPainter;

    public void setBackgroundPainter(Painter<JTextField> painter) {
        this.backgroundPainter = painter;
    }

    public void setForegroundPainter(Painter<JTextField> painter) {
        this.foregroundPainter = painter;
    }

    @Override
    protected void paintComponent(Graphics g) {

        if (backgroundPainter != null) {
            this.backgroundPainter.paint((Graphics2D) g, this, getWidth(), getHeight());
        }

        super.paintComponent(g);

        if (foregroundPainter != null) {
            foregroundtPainter.paint((Graphics2D) g, this, getWidth(), getHeight());
        }

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