JLabel 文本位置

JLabel text position

我是 JLabel 的新手,我想知道是否可以在图像上设置特定坐标 (x,y) 中的文本。

像这样:

所以正文是"HI"

 label.setText("HI");
 label.setIcon(icon);

我想说 label 包含图像和文本,但我想将其定位在特定位置,如上图。

我不想用label.setHorizontalTextPosition(i);setVerticalTextPosition(i);

抱歉我的英语不好 提前致谢^^

您可以像 JLabel 一样对 Components 使用此 Example, also take a look on setLocation() 方法,也可以尝试使用 Layouts 更好地管理每个 Component 的位置。

这个 guy 也与 JLabel 一起工作,你可以看看他是如何工作的,并将其应用到你的项目中。

例子

JLabel label = new JLabel("Hi");
panel.add(label);
//This is to get the width and height
Dimension size = label.getPreferredSize();
//You can change 100(x) and 100(y) for your likes, so you can put that JLabel wherever you want
label.setBounds(100, 100, size.width, size.height);

您必须知道要将 JLabel 放在那里的 xy

如果你想这样做,就按照 tong1 做的那样,但要显示 xy 才能知道你想要它的确切位置。

创建 Container

Container container= getContentPane();

加上JLabel就可以了

container.add(label);

添加一个 MouseListener() 以获得 xy

container.addMouseListener(new MouseAdapter(){ 
        public void mouseClicked(MouseEvent e){
        //x and y are ints.
            x = e.getX();
            y = e.getY();
            label.setBounds(x,y,150,50);
        }
    });

通过覆盖 paintComponent 方法,可以在任何位置绘制文本。

JLabel label = new JLabel(icon) {
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("Hi", 10, 10); //these are x and y positions
    }
};

有许多合理的方法可以做到这一点。

但是,您应该避免使用 null 布局,像素完美布局是现代 ui 设计中的一种错觉。影响组件个体大小的因素太多,none 是您可以控制的。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

GridBagLayoutGridBagConstraints#insets

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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 Test {

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

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

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setLayout(new GridBagLayout());
            BufferedImage img = ImageIO.read(...);
            JLabel background = new JLabel(new ImageIcon(img));
            background.setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(1, 46, 9, 0);
            gbc.anchor = GridBagConstraints.SOUTH;
            gbc.weighty = 1;

            JLabel message = new JLabel("Go that way!");
            message.setVerticalAlignment(JLabel.BOTTOM);
            background.add(message, gbc);

            add(background);
        }

    }

}

Graphics2D

您可以将文本直接绘制到图像上,但如您所见,它变得相当复杂

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FontMetrics;
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 Test {

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

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

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            JLabel background = new JLabel();

            BufferedImage img = ImageIO.read(...);
            Graphics2D g2d = img.createGraphics();
            g2d.setFont(background.getFont());
            String text = "Go that way!";
            FontMetrics fm = g2d.getFontMetrics();

            int x = 46 + (((img.getWidth() - 46) - fm.stringWidth(text)) / 2);
            int y = (((img.getHeight() - fm.getHeight())) + fm.getAscent()) - 9;
            g2d.setColor(Color.BLACK);
            g2d.drawString(text, x, y);
            g2d.dispose();

            background.setIcon(new ImageIcon(img));
            add(background);
        }

    }

}