java 将 JLabel 放在带有图像的 JLabel 顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12253979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:11:32  来源:igfitidea点击:

Place JLabel on top of JLabel with image in

javaswingjpaneljlabel

提问by Excalibur

I am pretty sure that this question has been asked before, but my case is slightly different as in i am trying to place a JLabel on top of a JLabel acting as a background, I want to display changing numbers using the JLabels and the numbers need to display over the background, however i am a bit of a swing n00b, thanks in advance, Jonathan

我很确定之前已经问过这个问题,但我的情况略有不同,因为我试图将 JLabel 放在作为背景的 JLabel 之上,我想使用 JLabels 显示不断变化的数字,并且需要的数字显示在背景上,但我有点摇摆不定,提前致谢,乔纳森

回答by MadProgrammer

Without fully appreciating your requirements, if you simply need to display text over a background image, you'd be better off placing the label on top a custom panel which is capable of painting your background.

在不完全了解您的要求的情况下,如果您只需要在背景图像上显示文本,您最好将标签放在能够绘制背景的自定义面板的顶部。

You get the benefit of a layout manager without the mess.

您可以轻松获得布局管理器的好处。

I'd start by having a read trough Performing Custom Paintingand Graphics2D Trail.

我首先阅读Performing Custom Paintingand Graphics2D Trail

If that seems to daunting, JLabelis actually a type of Container, meaning it can actually 'contain' other components.

如果这看起来令人生畏,JLabel实际上是一种Container,这意味着它实际上可以“包含”其他组件。

EXAMPLE

例子

Background pane...

背景窗格...

public class PaintPane extends JPanel {

    private Image background;

    public PaintPane(Image image) {     
        // This is just an example, I'd prefer to use setters/getters
        // and would also need to provide alignment options ;)
        background = image;            
    }

    @Override
    public Dimension getPreferredSize() {
        return background == null ? new Dimension(0, 0) : new Dimension(background.getWidth(this), background.getHeight(this));            
    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        if (background != null) {                
            Insets insets = getInsets();

            int width = getWidth() - 1 - (insets.left + insets.right);
            int height = getHeight() - 1 - (insets.top + insets.bottom);

            int x = (width - background.getWidth(this)) / 2;
            int y = (height - background.getHeight(this)) / 2;

            g.drawImage(background, x, y, this);                
        }

    }

}

Constructed with...

用...建造

public TestLayoutOverlay() throws IOException { // Extends JFrame...

    setTitle("test");
    setLayout(new GridBagLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    PaintPane pane = new PaintPane(ImageIO.read(new File("fire.jpg")));
    pane.setLayout(new BorderLayout());
    add(pane);

    JLabel label = new JLabel("I'm on fire");
    label.setFont(label.getFont().deriveFont(Font.BOLD, 48));
    label.setForeground(Color.WHITE);
    label.setHorizontalAlignment(JLabel.CENTER);
    pane.add(label);

    pack();
    setLocationRelativeTo(null);
    setVisible(true);

}

And just to show that I'm not bias ;), an example using labels...

只是为了表明我没有偏见;),一个使用标签的例子......

public TestLayoutOverlay() {

    setTitle("test");
    setLayout(new GridBagLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JLabel background = new JLabel(new ImageIcon("fire.jpg"));
    background.setLayout(new BorderLayout());
    add(background);

    JLabel label = new JLabel("I'm on fire");
    label.setFont(label.getFont().deriveFont(Font.BOLD, 48));
    label.setForeground(Color.WHITE);
    label.setHorizontalAlignment(JLabel.CENTER);
    background.add(label);

    pack();
    setLocationRelativeTo(null);
    setVisible(true);

}

On fire

着火

回答by Excalibur

At runtime:

在运行时:

  • remove the label from its parent
  • add a container, which supports layers
  • add the 2x layer, but keep the Z order
  • 从其父标签中删除标签
  • 添加一个支持图层的容器
  • 添加 2x 层,但保持 Z 顺序

Enjoy. (No full code given for copy-paste)

享受。(没有给出复制粘贴的完整代码)

回答by Victor Mukherjee

you can do that by this:

你可以这样做:

JLabel l1=new JLabel();
JLabel l2=new JLabel();
l1.add(l2, JLabel.NORTH);