0

因此,我有一个扩展 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);
  }    
}

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

4

3 回答 3

2

为什么不直接调整标签的垂直和水平属性呢?

在此处输入图像描述

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

    }

}
于 2013-04-12T00:58:50.027 回答
1

您需要覆盖标签的绘制方法:

@Override
protected void paintComponent(Graphics g) {
    g.drawImage(imgPath, 0, 0, this);
    super.paintComponent(g);
}
于 2013-04-12T00:49:39.750 回答
1

img = new ImageIcon(imgPath);

尝试这个

image = img.getImage()
graphics = image.getGraphics()
graphics.drawString("Text", 0, 0) 
于 2013-04-12T00:49:42.070 回答