简体   繁体   English

在另一个JLabel上显示一个JLabel

[英]Displaying a JLabel on top of another JLabel

I have the following code: 我有以下代码:

try {
        File file_background = new File(
                "C:\\Users\\xxxx\\Desktop\\background.png");
        ImageIcon icon_background = new ImageIcon(
                ImageIO.read(file_background));
        JLabel background = new JLabel(icon_background);
        window.setContentPane(background);

        File file_car = new File(
                "C:\\Users\\xxxxxx\\Desktop\\car.png");
        ImageIcon icon_car = new ImageIcon(ImageIO.read(file_car));

        JLabel car = new JLabel(icon_car);
        car.setVisible(true);

        background.add(car);
        // TODO Get car showing on top of the background label

    } catch (IOException e) {

        e.printStackTrace();
    }

Where I'm attempting to have the car label show on TOP of the background label. 我试图将汽车标签显示在背景标签的顶部的位置。 But I'm only getting the background JLabel showing. 但是我只得到JLabel的背景显示。 I'm new to SWING so any suggestions to what steps I'm missing would be great. 我是SWING的新手,因此对于我缺少的步骤的任何建议都很好。

..I want to move it a later stage. ..我想将其移至下一个阶段。 But right now I want it to show first :) 但是现在我希望它首先显示:)

There are two ways, 有两种方法,

1st. 1。

  • Put JLabel car to JPanel , drawing an Image by using paintComponent , instead of JLabel background (advantage JPanel is container with proper notifications for LayoutManager ). JLabel car放置在JPanel ,使用paintComponent而不是JLabel background绘制Image (优点是JPanel是带有LayoutManager正确通知的容器)。

  • Put JLabel car to JLabel background , but JLabel haven't implemented any LayoutManager , have to set desired. JLabel car置于JLabel background ,但JLabel尚未实现任何LayoutManager ,必须设置所需。

    1. Advantage all images in JLabel are static, with zero CPU and GPU inpact ad consumption in compare with paintComponent . 优势JLabel中的所有图像都是静态的,与paintComponent相比,CPU和GPU的广告消耗为零。
    2. Disadvantage JLabel isn't container and with proper notifications for LayoutManager , required a few code lones moreover in compare with JLabel placed in JPanel , for movement (AbsoluteLayout) is quite good solution. 缺点JLabel不是容器,并且具有LayoutManager正确通知,而且与放置在JPanel JLabel相比,还需要一些代码克隆,因为移动(AbsoluteLayout)是一个很好的解决方案。

2nd. 第2位。

Draw both Images by using BufferedImage and Graphics . 使用BufferedImage和Graphics绘制两个Images

Add them both to a JPanel that uses OverlayLayout . 将它们都添加到使用OverlayLayout的JPanel中。 It is not ok to add a JLabel to another JLabel . 不能将JLabel添加到另一个JLabel

This code has not gone through a compiler so take it for what it is :) 这段代码没有经过编译器,因此请按原样使用:)

JPanel panel = new JPanel(new OverlayLayout());
panel.add(background);
panel.add(car);

Works.. Added the following to make it display: Works ..添加了以下内容使其显示:

car.setBounds(200, 200, 200, 200);

Apparently it's because by default a null layout manager is used. 显然是因为默认情况下使用的是空布局管理器。 So setting the bounds of the label will enable it to display since the default size is 0. 因此,设置标签的边界将使其能够显示,因为默认大小为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM