在我的应用程序中,我使用的是SwingX库中的JXButton,我真的很喜欢它的绘制方法。现在,我必须将我的简单按钮更改为切换按钮,并为未选中/选中状态设置不同的画笔。不幸的是,我找不到JXToggleButton。有没有办法保留painter方法的好处?
发布于 2011-09-08 00:23:52
创建一个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());
}
}
}https://stackoverflow.com/questions/7336167
复制相似问题