相关疑难解决方法(0)

加载图片资源

我的GUI出错了.尝试设置标题栏图标,然后将其包含在Runnable JAR中.

BufferedImage image = null;
try {
    image = ImageIO.read(getClass().getClassLoader().getResource("resources/icon.gif"));
} 
catch (IOException e) {
    e.printStackTrace();
}

frame.setIconImage(image);
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at GUI.<init>(GUI.java:39)
    at GUI.main(GUI.java:351)
Run Code Online (Sandbox Code Playgroud)

图像位于正确的目录中,"resources"文件夹是项目文件的根目录

java swing awt nullpointerexception embedded-resource

54
推荐指数
3
解决办法
6万
查看次数

JComponents没有出现图片背景?

我的组件没有显示出来.我该如何解决?

码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class login implements ActionListener{
    JTextField gusername;
    JTextField gpassword;
    static String username;
    static String password;

    void logini() throws IOException {
        JFrame window = new JFrame("Login");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(300, 250);
        window.setResizable(false);
        window.setVisible(true);

        JPanel mainp = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        window.add(mainp);

        BufferedImage myPicture = ImageIO.read(new File("c:\\bgd.png"));
        JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
        mainp.add(picLabel, c);

        c.gridx = 0;
        c.gridy = …
Run Code Online (Sandbox Code Playgroud)

java swing bufferedimage jpanel jframe

4
推荐指数
1
解决办法
4189
查看次数