SwingX和JToggleButton

SwingX和JToggleButton,swing,swingx,jtogglebutton,Swing,Swingx,Jtogglebutton,在我的应用程序中,我使用的是SwingX库中的JXButton,我非常喜欢painter方法。现在,我必须将我的简单按钮更改为切换按钮,并为未选择/已选择状态使用不同的画师。不幸的是,我找不到JXToggleButton。有没有一种解决方案可以保持painter方法的优势?创建JXType组件实际上相当容易。您可能需要额外检查边界、焦点等。。但这是你通常会采取的方法 public class JXToggleButton extends JToggleButton implements Focu

在我的应用程序中,我使用的是SwingX库中的JXButton,我非常喜欢painter方法。现在,我必须将我的简单按钮更改为切换按钮,并为未选择/已选择状态使用不同的画师。不幸的是,我找不到JXToggleButton。有没有一种解决方案可以保持painter方法的优势?

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

    }
}
公共类JXToggleButton扩展了JToggleButton,实现了FocusListener,可绘制{
私人画家背景画家;
私人画家;前沿画家;
公共空间背景画家(画家){
这个。背景画家=画家;
}
公共场地油漆工(油漆工){
this.foregroundPainter=画家;
}
@凌驾
受保护组件(图形g){
if(backgroundPainter!=null){
this.backgroundPainter.paint((Graphics2D)g,this,getWidth(),getHeight());
}
超级组件(g);
if(foregroundPainter!=null){
foregroundtPainter.paint((Graphics2D)g,this,getWidth(),getHeight());
}
}
}