在Jlabel (Label)框中设置带有动态文本的背景图像



如何设置动态文本标签框的背景图像。

例如:

JLabel label = new JLabel();
label.setIcon("/image/image.png");
label.setText("Text1");

这个代码不工作,因为文本没有显示在图像的中心

这很简单。

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

您还没有定义文本应该如何绘制相对于图像。以下是不涉及自定义绘画的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();
            }
        });
    }
}

有几个可用的选项。例如,您可以将JLabel放在另一个JLabel的顶部(尽管首先为容器JLabel提供一个可行的布局管理器),或者您可以直接在JLabel的paintComponent方法中绘制图像。如果使用后者,请确保将JLabel设置为不透明的。

使用此代码

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

使用这个图标对齐中心作为JLabel的背景

最新更新