0

我只想为 JButton 设置不同的样式。所以我重写了paintComponent来实现。它工作成功,但此按钮不支持 HTML 格式。

你能建议我吗

paintComponent()
~~~~~~~~~~~~~~~~
this is below my override code:
// prep for painting.  
      Graphics2D g2D = (Graphics2D)g;
      if(buttonStyle != 0 && buttonStyle != IMAGES){
        if(g == null)
            return;
        if(getBackground() == null)
          setBackground(acrcolor);
        if(getFadedBackgroundColor() == null)
          setFadedBackgroundColor(Color.white);

        g2D.clearRect(0, 0, getWidth()+getX(), getHeight()+getY());
        // end prep for painting.
      }
      switch (buttonStyle){
      case SKINZ:
        paintSkinz(g2D);
        return;
      case IMAGES:
        paintImages(g2D);
        break;
      case ROLLOVER:
        System.err.println("Rollover look of FuelButton not yet implemented.");
        break;
      case JAVA_LIKE:
        paintJavaLike(g2D);
        return;
      case GRADIENCE:
        paintGradience(g2D);
      case CLASSIC:
      case JAVA_DEFAULT:
      default:          
        super.paintComponent(g);
        m_originalborder = getBorder();
        m_originalfont = getFont();
        return;
      }
      paintText(g2D,0,0,getWidth(),getHeight());


paintJavaLike(g2D):
~~~~~~~~~~~~~~~~~~~~
g2D.setColor(getBackground());
      g2D.fill3DRect(0,0,getWidth(),getHeight(),true);
      if(getIcon()==null)
        paintText(g2D,0,0,getWidth(),getHeight());
      else if (getText() == null || getText().length() == 0)
        this.paintCenteredIcon(g2D, ((ImageIcon)getIcon()).getImage());
      else {
        int w = getWidth() - getIconTextGap() - getIcon().getIconWidth() - (borderWidth*2)-4;
        int h = getHeight()-(borderWidth*2);
        g2D.drawImage(((ImageIcon)getIcon()).getImage(), 2, (getHeight()/2)-(getIcon().getIconHeight()/2), this);
        paintText(g2D,2+getIcon().getIconWidth()+this.getIconTextGap(),0,w,h);
      }

谢谢帕拉尼萨米

4

1 回答 1

1

您可以提供自定义ButtonUI,并将其用于按钮,而不是覆盖paintComponent(). SwingTextLayout用于拉弦,这会变得很复杂。但是,BasicButtonUI有一个受保护的paintText()方法,可以做你想做的事(你不一定需要手动调用它。UIpaint()调用它,除非你也覆盖它)。因此,如果您扩展BasicButtonUI,您可以简单地让它绘制字符串。

于 2013-10-07T12:00:14.007 回答