Java将ImageIcon添加到JLabel

use*_*826 8 java swing jpanel jframe imageicon

我正在尝试使用Java制作一个非常基本的游戏,而我在显示图像时遇到问题JFrame.它曾经为我工作过,现在不是,我看不出我做错了什么.

我已经尝试打印当前的工作目录并更改我的图像匹配的位置.问题很可能是没有得到图像,因为我的(文件查找器或文件阅读器或类似的东西)可以毫无问题地找到它,但我无法正确地将它(或ImageIcon)添加到JLabel,或者那个JFrame.

这是我的代码......

JFrame frame = new JFrame("no image");
ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
JLabel imagelabel = new JLabel(image);
frame.add(imagelabel);
Run Code Online (Sandbox Code Playgroud)

JFrame已经setVisible(true)pack().

请有人帮我理解出了什么问题.

Gui*_*let 11

你的问题在于:

   ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
   JLabel imagelabel = new JLabel(character);
Run Code Online (Sandbox Code Playgroud)

您创建了一个ImageIcon"图像",但您使用"character"创建了JLabel.

它应该是:

JLabel imagelabel = new JLabel(image);
Run Code Online (Sandbox Code Playgroud)