需要使用JLabel设置背景图像的帮助吗? [英] Need help setting the background image using JLabel?

查看:47
本文介绍了需要使用JLabel设置背景图像的帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Java进行GUI编程,但是在使用 JLabel 将背景图像设置为 JFrame 时遇到问题.我已经在这个网站上读到了很多关于同一问题的答案,但是对于初学者来说,代码太复杂了.

I just started GUI programming in Java and I am having trouble setting the background image to a JFrame using JLabel. I have read many answers to the same question on this website but the code is too complicated for a beginner.

我的源代码如下,并且我正在使用的图像已经在 src 文件夹中(我得到了输出窗口,但其中没有图像):

My source code is as follows and the image I'm using is already in src folder (I get the output window but there is no image in it):

public class staffGUI extends JFrame {
private JLabel imageLabel = new JLabel(new ImageIcon("staff-directory.jpg"));

private JPanel bxPanel = new JPanel();

public staffGUI(){
    super("Staff Management");

    bxPanel.setLayout(new GridLayout(1,1));
    bxPanel.add(imageLabel);

    this.setLayout(new GridLayout(1,1));
    this.add(bxPanel);

    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.pack();
}

推荐答案

.实际的物理映像是在后台线程中加载的,因此即使调用可能立即返回,实际的加载仍可能在后台运行.

ImageIcon(String) "Creates an ImageIcon from the specified file". The actual physical image is loaded in a background thread, so even though the call might return immediately, the actually loading could still be running in the background.

这意味着如果无法加载图像, ImageIcon 不会引发任何错误,这有时会令人讨厌.我更喜欢使用 ImageIO.read ,因为它由于某种原因而无法读取图像时会抛出 IOException .

This means that ImageIcon does not throw any errors if the image can't be loaded, making sometimes annoying to work with. I prefer to use ImageIO.read where possible, as it will throw an IOException when it can't read a image for some reason.

未加载映像的原因是因为该映像实际上不在JVM上下文中存在,而JVM上下文正在该映像的当前工作目录中查找.

The reason you image is not loading is because the image doesn't actually exist from the context of the JVM, which is looking in the current working directory of the image.

当您在程序上下文中包含资源时,它们将不再被视为文件,而需要通过使用 Class#getResource Class#getResourceAsStream 进行加载代码>,具体取决于您的需求.

When you included resources within the context of the program, they can no longer be addressed as files and need to be loaded through the use of Class#getResource or Class#getResourceAsStream, depending on your needs.

例如

imageLabel = new JLabel(getClass().getResource("/staffdirectory/staff-directory.jpg"));

在可能的情况下,您应该从源根目录的上下文中提供图像的路径

Where possible, you should supply the path to the image from the context of the source root

能否举个例子,说明如何在代码中使用"ImageIO.read"?

Can you give an example how I can use the "ImageIO.read" in my code?

import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class staffGUI extends JFrame {

    private JLabel imageLabel;

    private JPanel bxPanel = new JPanel();

    public staffGUI() {
        super("Staff Management");

        imageLabel = new JLabel();
        try {
            BufferedImage img = ImageIO.read(getClass().getResource("/staffdirectory/staff-directory.jpg"));
            imageLabel.setIcon(new ImageIcon(img));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        bxPanel.setLayout(new GridLayout(1, 1));
        bxPanel.add(imageLabel);

        this.setLayout(new GridLayout(1, 1));
        this.add(bxPanel);

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.pack();

    }
}

这篇关于需要使用JLabel设置背景图像的帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆