2012-07-18 52 views
1

我寫了一個擴展JToggleButton的類。自創JToggleButton:更改圖標

一切工作正常,除了我不能更改按鈕的圖標。

這裏是我的類代碼:

package be.blauweregen.lichtsturing; 

import javax.swing.*; 
import java.awt.*; 

class MyToggleButton extends JToggleButton { 
    private static final long serialVersionUID = 1L; 
    String s; 

    public MyToggleButton(String str) { 
     super(str); 
     s = str; 
    } 

    public MyToggleButton(String str, Boolean sel) { 
     super(str, sel); 
     s = str; 
    } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     if (this.isSelected() && this.isEnabled()) { // manueel en aan 
      int w = getWidth(); 
      int h = getHeight(); 
      g.setColor(Color.green); // selected color 
      g.fillRect(0, 0, w, h); 
      g.setColor(Color.black); // selected foreground color 
      g.drawString(s, (w - g.getFontMetrics().stringWidth(s))/2 + 1, 
        (h + g.getFontMetrics().getAscent())/2 - 1); 
      setFont(new Font("Tahoma", Font.BOLD, 11)); 
     } else if (this.isSelected() && !this.isEnabled()) { // automatisch en 
                   // aan 
      int w = getWidth(); 
      int h = getHeight(); 

      g.setColor(Color.green); // selected color 
      g.fillRect(0, 0, w, h); 
      g.setColor(Color.black); // selected foreground color 
      g.drawString(s, (w - g.getFontMetrics().stringWidth(s))/2 + 1, 
        (h + g.getFontMetrics().getAscent())/2 - 1); 
      setFont(new Font("Tahoma", Font.PLAIN, 11)); 
     } else if (!this.isSelected() && this.isEnabled()) { // manueel en uit 
      int w = getWidth(); 
      int h = getHeight(); 
      g.setColor(Color.red); // selected color 
      g.fillRect(0, 0, w, h); 
      g.setColor(Color.black); // selected foreground color 
      g.drawString(s, (w - g.getFontMetrics().stringWidth(s))/2 + 1, 
        (h + g.getFontMetrics().getAscent())/2 - 1); 
      setFont(new Font("Tahoma", Font.BOLD, 11)); 
     } else if (!this.isSelected() && !this.isEnabled()) { // automatisch en 
                   // uit 
      int w = getWidth(); 
      int h = getHeight(); 
      g.setColor(Color.red); // selected color 
      g.fillRect(0, 0, w, h); 
      g.setColor(Color.black); // selected foreground color 
      g.drawString(s, (w - g.getFontMetrics().stringWidth(s))/2 + 1, 
        (h + g.getFontMetrics().getAscent())/2 - 1); 
      setFont(new Font("Tahoma", Font.PLAIN, 11)); 
     } 

    } 
} 

我使用的代碼以這種方式在我的計劃:

btnGangSanitair = new MyToggleButton("Gang sanitair"); 
btnGangSanitair.setSelectedIcon(new ImageIcon("Z:\\Development\\Java\\BlauweRegen\\famfamfam_silk_icons_v013\\icons\\application_edit.png")); 
btnGangSanitair.setIcon(new ImageIcon(Client.class.getResource("/be.blauweregen.icons/arrow_refresh.png"))); 

我在做什麼錯?

圖標沒有出現在程序中。

+1

爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-07-18 13:25:56

回答

4
  1. 不要重新創建並運行時重新加載ImageIcon S,加載所有ImageIcon S作爲局部變量一次,只有一次,

  2. 使用setBackground()而不是paintComponent()

  3. 使用proper setXxxIcon methods for JToggleButton

+0

良好的信息。已經解決了這個問題。 – 2012-07-18 17:24:18

+0

不用客氣 – mKorbel 2012-07-18 17:27:27

2

除了mKorbel的評論,你所有精彩的cu stom painting畫完了超級通話所做的事情,因此在你的圖標上繪畫

+0

感謝您的評論。只是打字,我用這種方法..你10秒太快了。謝謝你的答案。 – 2012-07-18 17:23:37