Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在Java上加载图像(BlueJ)_Java_Image_Awt_Bluej - Fatal编程技术网

无法在Java上加载图像(BlueJ)

无法在Java上加载图像(BlueJ),java,image,awt,bluej,Java,Image,Awt,Bluej,我正试图将我的图像加载到Java中,但在运行代码时,JFrame中没有显示任何内容 我这样做的方式是从我的main调用我的Image函数: import java.awt.*; // Graphics stuff from the AWT library, here: Image import java.io.File; // File I/O functionality (for loading an image) imp

我正试图将我的图像加载到Java中,但在运行代码时,JFrame中没有显示任何内容

我这样做的方式是从我的main调用我的Image函数:

    import java.awt.*;              // Graphics stuff from the AWT library, here: Image
    import java.io.File;            // File I/O functionality (for loading an image)
    import javax.swing.ImageIcon;   // All images are used as "icons"



  public class GameImage
    {
  public static Image loadImage(String imagePathName) {

    // All images are loades as "icons"
    ImageIcon i = null;

    // Try to load the image
    File f = new File(imagePathName);


    if(f.exists()) {  // Success. Assign the image to the "icon"
        i = new ImageIcon(imagePathName);
    }
    else {           // Oops! Something is wrong.
        System.out.println("\nCould not find this image: "+imagePathName+"\nAre file       name and/or path to the file correct?");            
        System.exit(0);
    }

    // Done. Either return the image or "null"
    return i.getImage();

} // End of loadImages method

}
然后在这里叫它:

        GI_Background = GameImage.loadImage("Images//background.jpg");
    GI_DuskyDolphin = GameImage.loadImage("Images//DuskyDolphin.jpg");
如果这还不够,我很乐意提供其余的代码:)
谢谢

我不太确定你想达到什么目的。。。 要使用方法加载图像,这就足够了:

private Image loadImage(String fileName){
    return (new ImageIcon(fileName)).getImage();
}
之后,我将创建一个以图像为背景的JLabel

ImageIcon image = getImage( filename );    

JLabel imageLabel = new JLabel( image );
imageLabel.setSize( image.getIconHeight, image.getIconWidth );

JFrame frame = new JFrame();
frame.add( imageLabel );
如果我不为您工作,或者您不想要aks,请尝试此操作并再次使用aks:)

如果映像是应用程序的一部分,请不要使用文件,而是使用java资源机制

    URL imageUrl = getClass().getResource("/Images/background.jpg");
    return new ImageIcon(imageURL).getImage();
未找到资源URL时将返回null。
如果应用程序打包在一个.jar中,您可以使用7zip/WinZip左右打开它,并检查路径。它必须区分大小写,并使用
/
(而不是反斜杠)。

显示您对图像所做的操作:如何尝试显示图像。因为(大概)程序没有退出,所以这个代码可能没问题。好吧,你有这个图像
GI_Background
,但是你怎么处理它呢?你会把它添加到标签上吗,你会画画吗?我看不到你在用它做任何事情。到部署时,这些资源很可能会成为一个重要的资源。在这种情况下,必须通过
URL
而不是
File
访问资源。有关形成
URL
的方法,请参阅标签的。