SwingX和JToggleButton

时间:2011-09-07 15:06:52

标签: swing swingx jtogglebutton

在我的应用程序中,我正在使用SwingX库中的JXButton,我非常喜欢画家方法。现在我必须更改我的简单按钮以切换按钮,并为未选择/选择状态设置不同的画家。不幸的是我找不到JXToggleButton。是否有解决方案来保持画家方法的好处?

1 个答案:

答案 0 :(得分:0)

创建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());
        }

    }
}