我的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"文件夹是项目文件的根目录
我的组件没有显示出来.我该如何解决?
码:
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,目前我仍然坚持使用.
我一直在寻找一种方法来将图像添加到我的JFrame中.我在网上发现了这个:
ImageIcon image = new ImageIcon("path & name & extension");
JLabel imageLabel = new JLabel(image);
Run Code Online (Sandbox Code Playgroud)
在将其实现到我自己的代码之后,它看起来像这样(这只是相关部分):
class Game1 extends JFrame
{
public static Display f = new Display();
public Game1()
{
Game1.f.setSize(1000, 750);
Game1.f.setResizable(false);
Game1.f.setVisible(true);
Game1.f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Game1.f.setTitle("Online First Person Shooter");
ImageIcon image = new ImageIcon("C:\\Users\\Meneer\\Pictures\\image.png");
JLabel imageLabel = new JLabel(image);
add(imageLabel);
}
}
class Display extends JFrame
{
}
Run Code Online (Sandbox Code Playgroud)
运行此代码时,它不会给我任何错误,但它也不显示图片.我看到了一些问题和人们有同样的问题,但他们的代码与我的完全不同,他们使用其他方式来显示图像.