我目前正在学习Java,目前我仍然坚持使用.
我一直在寻找一种方法来将图像添加到我的JFrame中.
我在网上发现了这个:
ImageIcon image = new ImageIcon("path & name & extension");
JLabel imageLabel = new JLabel(image);
在将其实现到我自己的代码之后,它看起来像这样(这只是相关部分):
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
{
}
运行此代码时,它不会给我任何错误,但它也不显示图片.我看到了一些问题和人们有同样的问题,但他们的代码与我的完全不同,他们使用其他方式来显示图像.