图像不显示在jframe中

mu0hgdu0  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(377)

我刚刚开始学习javagui,我对图像有很多问题。我在这个网站和其他网站上浏览了多个主题,但由于某些原因,我不能让它工作(虽然我可能犯了很多错误,我只是没有意识到)。我只想从在屏幕上显示图像开始。添加一些信息-我正在使用intellij;该图像存储在一个资源文件夹中,我已将其标记为“library root”(此外,该图像非常小-16x16,但我也尝试过使用更大的图像,但它对我没有帮助)。

import javax.swing.*;
import java.awt.*;

public class Frame {

    public static final int WIDTH = 1024;
    public static final int HEIGHT = 768;

    public Frame()
    {
        JFrame frame = new JFrame();
        frame.setTitle("Shady Path");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.pack();
        frame.setSize(WIDTH, HEIGHT);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().setBackground(Color.BLACK);
        frame.setResizable(false);

        //Font font = new Font(Font.MONOSPACED, Font.PLAIN, 10);
        JLabel human = new JLabel(new ImageIcon(getClass().getResource("/human.jpg")));
        Dimension humanDimension = new Dimension(150, 150);
        human.setMinimumSize(humanDimension);
        human.setPreferredSize(humanDimension);
        human.setMaximumSize(humanDimension);
        human.setLocation(100, 100);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.add(human);
        frame.add(panel);

        frame.setVisible(true);
    }
}

相关问题