简体   繁体   English

Java-JFrame没有显示多张图片

[英]Java - JFrame not showing more than one picture

So I've been trying to teach myself the basics of Java (2D) gaming. 因此,我一直在尝试自学Java(2D)游戏的基础知识。 After a long, annoying process trying to get an image to display, I got it working. 经过漫长而烦人的尝试来显示图像后,我开始工作了。 Unfortunately, when I tried to add a second image, it replaced the first. 不幸的是,当我尝试添加第二张图像时,它替换了第一张。 I know that I'm making some obvious, noob mistake, but hey, I'm a noob at this. 我知道我犯了一些显而易见的菜鸟错误,但是,嘿,我对此很菜鸟。 Anyway, here's my code: 无论如何,这是我的代码:

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame {

    public static void main(String[] args0) {

        JFrame frame = new JFrame();
        ImageIcon background = new ImageIcon("background.png");
        JLabel backgroundLabel = new JLabel(background);
        frame.add(backgroundLabel);
        backgroundLabel.setVisible(true);

        ImageIcon title = new ImageIcon("title.png");
        JLabel titleLabel = new JLabel(title);
        frame.add(titleLabel);
        titleLabel.setVisible(false);

        frame.setVisible(true);
        frame.setTitle("Fastball");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();

    }

}

When I run this, the newly added "title.png" section overrides the first image, replacing it. 当我运行此命令时,新添加的“ title.png”部分将覆盖第一个图像,并将其替换。 Please, just tell me the simplest way to fix this with a brief explanation of my mistake. 请告诉我最简单的方法,并简要说明我的错误。

(PS I'm using Eclipse Mars and the latest of all the Java stuff.) (PS我正在使用Eclipse Mars和所有Java的最新版本。)

Start by taking a look at How to Use BorderLayout to understand why the problem is occurring and then have a look at Laying Out Components Within a Container for some possible solutions 首先看一下如何使用BorderLayout来了解为什么会出现问题,然后再看一下在容器布置组件以获取一些可能的解决方案

JFrame frame = new JFrame();
//...
frame.setContentPane(backgroundLabel);
frame.setLayout(new GridBagLayout());

ImageIcon title = new ImageIcon("title.png");
JLabel titleLabel = new JLabel(title);
frame.add(titleLabel);
//...

You should be adding your titleLabel to the backgroundLabel , but you can "cheat" a little with JFrame , you can set the backgroundLabel as the "content pane" of the frame, this means that anything you add to the frame is actually added to the backgroundLabel . 您应该将titleLabel添加到backgroundLabel ,但是您可以使用JFrame进行“欺骗”,可以将backgroundLabel设置为框架的“内容窗格”,这意味着添加到框架的所有内容实际上都已添加到框架中。 backgroundLabel

Cavets 洞穴

  • JLabel does not have a layout manage applied by default, this means that unless you apply one, anything you add to it will not be sized or positioned and will appear "invisible" JLabel默认情况下没有应用布局管理,这意味着除非您应用一个布局管理,否则添加到其中的任何内容都将不会调整大小或位置,并且将显示为“不可见”
  • JLabel does not use the layout manager to calculate it's preferred size, instead, it relies on the size of the image and it's text. JLabel不使用布局管理器来计算其首选大小,而是依赖于图像和文本的大小。 This may not be an issue in most cases, but if your content, for some reason, exceeds the size of the image (either horizontally or vertically), the content will be clipped. 在大多数情况下,这可能不是问题,但是如果您的内容由于某种原因超出了图像的大小(水平或垂直),则内容将被剪切。 You could have a look at this example which shows a couple of different ways to achieve the same result 您可以看一下这个示例该示例显示了几种获得相同结果的不同方法

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

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