我目前正在使用GUI进行基于文本的RPG,并且我似乎无法获取想要用于加载的图像。从项目中的文件夹中加载图像
class BackgroundPanel extends Panel
{
// The Image to store the background image in.
Image img;
public BackgroundPanel(String location)
{
// Loads the background image and stores in img object.
img = Toolkit.getDefaultToolkit().createImage(location);
}
public void paintComponent(Graphics g)
{
// Draws the img to the BackgroundPanel.
g.drawImage(img, 0, 0, null);
img.setAccelerationPriority(SCALE_FAST);
}
}
这是我用于面板本身的代码。我试图把图像文件放在我的项目的根目录中,但它似乎没有帮助。我在我打算用于所有图像的项目中创建了一个文件夹。
我不确定这里出现了什么问题。我知道我尝试过使用paint()
而不是paintComponent()
,但随后出于某种原因,按钮和其他组件将不会绘制,直到您将它们放在上面为止。
任何想法?
它应该是'extends JPanel' – Braj
将@Override注释添加到paintComponent方法。这应该有助于确定问题。从来没有任何理由为什么paintComponent应该公开 – MadProgrammer