1
在我的应用程序中,我使用SwingX库中的JXButton,我非常喜欢画家方法。现在,我必须将我的简单按钮更改为切换按钮,并为未选定/选定状态设置不同的画家。不幸的是我找不到JXToggleButton。是否有解决方案来保持画家方法的好处?SwingX和JToggleButton
在我的应用程序中,我使用SwingX库中的JXButton,我非常喜欢画家方法。现在,我必须将我的简单按钮更改为切换按钮,并为未选定/选定状态设置不同的画家。不幸的是我找不到JXToggleButton。是否有解决方案来保持画家方法的好处?SwingX和JToggleButton
创建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());
}
}
}