2011-11-05 28 views

回答

2

有幾個選項可用。例如,您可以將JLabel放在另一個JLabel的頂部(儘管首先給容器JLabel一個可行的佈局管理器),或者您可以直接在JLabel的paintComponent方法中繪製圖像。如果你使用後者,請確保JLabel設置爲不透明。

4

這很容易。

import javax.swing.*; 


public class YourFrame extends JFrame { 

    private ImageIcon icon; 
    private JLabel label; 


    public YourFrame(){ 

    icon = new ImageIcon("/image/image.png"); 

    label= new JLabel() { 
     public void paintComponent(Graphics g) { 
     g.drawImage(icon.getImage(), 0, 0, null); 
     super.paintComponent(g); 
     } 
    }; 

    label.setOpaque(false); 
    getContentPane().add(label); 

    label.setText("Text1"); 

    } 

    public static void main(String[] args) { 
    YourFrame frame = new YourFrame(); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
    } 
} 
+0

感謝ü斯蒂芬。 –

+0

有沒有辦法將JLabel的寬度和高度設置爲與image.png相同的尺寸,並將中間的文字對齊? – Joyce

+0

'setPreferredSize'的作品,但有沒有其他方式來做到這一點,而無需手動設置寬度和高度? (即,當我更改.png尺寸時,我不需要修改代碼以適合.png) – Joyce

4

您尚未定義文本應該如何與圖像相關。以下是不涉及風俗畫4點不同的方法:

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

public class LabelImageText extends JPanel 
{ 
    public LabelImageText() 
    { 
     JLabel label1 = new JLabel(new ColorIcon(Color.ORANGE, 100, 100)); 
     label1.setText("Easy Way"); 
     label1.setHorizontalTextPosition(JLabel.CENTER); 
     label1.setVerticalTextPosition(JLabel.CENTER); 
     add(label1); 

     // 

     JLabel label2 = new JLabel(new ColorIcon(Color.YELLOW, 200, 150)); 
     label2.setLayout(new BoxLayout(label2, BoxLayout.Y_AXIS)); 
     add(label2); 

     JLabel text = new JLabel("More Control"); 
     text.setAlignmentX(JLabel.CENTER_ALIGNMENT); 
     label2.add(Box.createVerticalGlue()); 
     label2.add(text); 
     label2.add(Box.createVerticalStrut(10)); 

     // 

     JLabel label3 = new JLabel(new ColorIcon(Color.GREEN, 200, 150)); 
     add(label3); 

     JLabel text3 = new JLabel(); 
     text3.setText("<html><center>Text<br>over<br>Image<center></html>"); 
     text3.setLocation(20, 20); 
     text3.setSize(text3.getPreferredSize()); 
     label3.add(text3); 

     // 

     JLabel label4 = new JLabel(new ColorIcon(Color.CYAN, 200, 150)); 
     add(label4); 

     JTextPane textPane = new JTextPane(); 
     textPane.setText("Add some text that will wrap at your preferred width"); 
     textPane.setEditable(false); 
     textPane.setOpaque(false); 
     SimpleAttributeSet center = new SimpleAttributeSet(); 
     StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); 
     StyledDocument doc = textPane.getStyledDocument(); 
     doc.setParagraphAttributes(0, doc.getLength(), center, false); 
     textPane.setBounds(20, 20, 75, 100); 
     label4.add(textPane); 
    } 

    public static class ColorIcon implements Icon 
    { 
     private Color color; 
     private int width; 
     private int height; 

     public ColorIcon(Color color, int width, int height) 
     { 
      this.color = color; 
      this.width = width; 
      this.height = height; 
     } 

     public int getIconWidth() 
     { 
      return width; 
     } 

     public int getIconHeight() 
     { 
      return height; 
     } 

     public void paintIcon(Component c, Graphics g, int x, int y) 
     { 
      g.setColor(color); 
      g.fillRect(x, y, width, height); 
     } 
    } 

    private static void createAndShowUI() 
    { 
     JFrame frame = new JFrame("LabelImageText"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new LabelImageText()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 
0

使用此代碼

label.setIcon(new ImageIcon("background.png")); 
label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
label.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 

使用該圖標居中對齊作爲background to JLabel

相關問題