【问题标题】:SwingX and JToggleButtonSwingX 和 JToggleButton
【发布时间】:2011-09-07 15:06:51
【问题描述】:

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

【问题讨论】:

    标签: swing swingx jtogglebutton


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 2011-12-14
      • 2018-06-26
      • 1970-01-01
      • 2011-04-28
      相关资源
      最近更新 更多