【问题标题】:JLabel text positionJLabel 文本位置
【发布时间】:2016-03-29 06:08:42
【问题描述】:

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

像这样:

所以文本是“HI”

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

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

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

对不起我的英语不好 提前谢谢^^

【问题讨论】:

  • 这是您要找的吗? label.setBounds(x, y, width, height);
  • 不,我想定位在特定位置包含图像的 JLabel 的文本。我的英语不太好,所以很难解释我
  • 像素完美布局是现代 ui 设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

标签: java swing jlabel


【解决方案1】:

通过重写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
    }
};

【讨论】:

  • 谢谢,这正是我想要的
【解决方案2】:

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

但是,您应该避免使用null 布局,像素完美布局是现代 UI 设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 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);
        }

    }

}

【讨论】:

    【解决方案3】:

    您可以关注此Example,也可以查看ComponentssetLocation() 方法,例如JLabel,也可以尝试使用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);
    

    你必须知道xy 你想把JLabel 放在那里。

    如果您想这样做,只需执行 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);
            }
        });
    
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2023-03-25
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多