2013-04-12 26 views
0

所以我有一個擴展JLabel的類,我使用它作爲按鈕(使用clicklistener類),我試圖給標籤添加一個背景圖像(請參見下文)但是當我使用這個標籤時,文字就在圖像的右邊。有沒有辦法將圖像用作實際背景?我想避免爲每個按鈕製作單獨的圖像。Java - 用背景作爲按鈕創建JLabel

public class DButton 
    extends JLabel 
{ 

    URL imgPath; 
    ImageIcon img; 

    public DButton(int width, int height) 
    { 
     this.setOpaque(true); 
     imgPath = getClass().getResource("/img/MainMenuButton.png"); 
     if(imgPath != null) 
     { 
      img = new ImageIcon(imgPath); 
      this.setIcon(img); 
     }else 
     { 
      System.err.println("Cant find file"); 
     } 
    } 
    public DButton(int width, int height, String text) 
    { 
     this(width, height); 
     this.setText(text); 
    }  
} 

編輯:感謝大家的快速反應,我想通了(設置垂直和水平對齊)

+0

就個人而言,我會使用'ImageIO'來讀取你的圖像。如果沒有其他的東西,它會拋出一個異常,如果讀取圖像時出錯 – MadProgrammer

回答

2

爲什麼不調整標籤的垂直和水平屬性?

enter image description here

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class BackgroundLabel { 

    public static void main(String[] args) { 
     new BackgroundLabel(); 
    } 

    public BackgroundLabel() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new FlowLayout(FlowLayout.CENTER)); 
      try { 
       BufferedImage icon = ImageIO.read(new File("Pony.png")); 
       JLabel label = new JLabel("Flying Ponies"); 
       label.setIcon(new ImageIcon(icon)); 
       label.setHorizontalAlignment(JLabel.CENTER); 
       label.setVerticalAlignment(JLabel.CENTER); 
       label.setHorizontalTextPosition(JLabel.CENTER); 
       label.setVerticalTextPosition(JLabel.CENTER); 
       add(label); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 

    } 

} 
1

你需要重寫你的標籤的paint方法:

@Override 
protected void paintComponent(Graphics g) { 
    g.drawImage(imgPath, 0, 0, this); 
    super.paintComponent(g); 
} 
+0

我改變了我的文章。 – Stefan

+0

你說得對,0x0更好。 ;) – Stefan

+0

不錯的編輯.. +1 :) –

1

img = new ImageIcon(imgPath); 

試試這個

image = img.getImage() 
graphics = image.getGraphics() 
graphics.drawString("Text", 0, 0) 
+0

使用標籤的文本定位比較好,但對'drawString()'爲+1;因爲它使用_baseline_座標,請嘗試'drawString(「Text」,0,16)'或檢查字體度量高度。 –