需要帮助使用JLabel设置背景图像?

时间:2015-03-17 04:54:54

标签: java image swing

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

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

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();
}

2 个答案:

答案 0 :(得分:1)

ImageIcon(String) "从指定的文件" 创建一个ImageIcon。实际的物理图像加载在后台线程中,因此即使调用可能立即返回,实际加载仍可能在后台运行。

这意味着ImageIcon如果无法加载图像,则不会引发任何错误,这有时会令人讨厌。我希望尽可能使用ImageIO.read,因为当它因某种原因无法读取图片时会抛出IOException

图像未加载的原因是因为图像实际上并不存在于JVM的上下文中,而JVM正在查看图像的当前工作目录。

当您在程序的上下文中包含资源时,它们将无法再作为文件进行处理,需要通过使用Class#getResourceClass#getResourceAsStream来加载,具体取决于您的需求。

例如

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

如果可能,您应该从源根目录的上下文提供图像的路径

  

您能举例说明如何使用" ImageIO.read"在我的代码中?

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();

    }
}

答案 1 :(得分:0)

试试这个例子我相信它会起作用。确保src文件夹中有一个images文件夹,并将图像放入其中。

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

将不起作用,eclipse将给出错误,因为未定义JLabel(URL);其次,私人修饰符不允许在这里。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;

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

class BackgroundImageJFrame extends JFrame {

    private static final long serialVersionUID = 6337428393053702097L;
    JButton b1;
    JLabel l1;

    public BackgroundImageJFrame() {
        setTitle("Background Color for JFrame");
        setSize(400, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        setLayout(new BorderLayout());

        JLabel background = new JLabel(
                new ImageIcon(Toolkit.getDefaultToolkit().getImage(
                        getClass().getResource("/images/free-wallpaper-4.jpg"))));
        add(background);
        background.setLayout(new FlowLayout());
        l1 = new JLabel("Here is a button");
        b1 = new JButton("I am a button");
        background.add(l1);
        background.add(b1);

        setSize(400, 400);
    }

    public static void main(String args[]) {
        new BackgroundImageJFrame();
    }
}