Java - 使用背景作为按钮创建JLabel

时间:2013-04-12 00:27:03

标签: java image swing jlabel

所以我有一个扩展JLabel的类,我正在使用它作为按钮(使用clicklistener类),我正在尝试向标签添加背景图像(见下文),但是当我使用此标签时文本关闭在图像的右侧。有没有办法将图像用作实际背景?我想避免为每个按钮制作单独的图像。

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);
  }    
}

编辑:感谢大家的快速回复,我弄明白了(设置垂直和水平对齐)

3 个答案:

答案 0 :(得分: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 :(得分:1)

您需要覆盖标签的paint方法:

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

答案 2 :(得分:1)

img = new ImageIcon(imgPath);

试试这个

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