Java 为什么此GUI应用程序不向我显示图像?

Java 为什么此GUI应用程序不向我显示图像?,java,swing,embedded-resource,Java,Swing,Embedded Resource,我正在尝试用JFrame对象编写一个java应用程序,它必须显示三个标签对象,包括文本和图像,我的文本“北”和“南”在执行时显示,但我的图像没有显示,甚至我将图像文件放入src文件夹 package deitel9; import java.awt.BorderLayout; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JFrame; public class LabelDem

我正在尝试用JFrame对象编写一个java应用程序,它必须显示三个标签对象,包括文本和图像,我的文本“北”和“南”在执行时显示,但我的图像没有显示,甚至我将图像文件放入src文件夹

package deitel9;

import java.awt.BorderLayout;

import javax.swing.ImageIcon;

import javax.swing.JLabel;

import javax.swing.JFrame;


public class LabelDemo {

    public static void main(String[] args)
    {
         //crate a label with a plain text
        JLabel northLabel = new JLabel("North");

        //crate an icon from an image so we can put it on a JLabel
        ImageIcon labelIcon = new ImageIcon("maldive.jpg");

        //crate a label with an Icon instead of text
        JLabel centerLabel = new JLabel(labelIcon);

        //create another label with an Icon
        JLabel southLabel = new JLabel(labelIcon);

        //set the label to display text (as well as an icon)
        southLabel.setText("South");

        //create a frame to hold the labels
        JFrame application = new JFrame();

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //add the labels to the frame; the second argument specifies
        //where on the frame to add the label

        application.add(northLabel,BorderLayout.NORTH);
        application.add(centerLabel,BorderLayout.CENTER);
        application.add(southLabel,BorderLayout.SOUTH);

        application.setSize(300,300);
        application.setVisible(true);
    }//end main


}//end class LabelDemo

由于您的图像存储在存储
LabelDemo
的同一个包中,请尝试以下操作:

ImageIcon labelIcon = new ImageIcon(LabelDemo.class.getResource("/deitel9/maldive.jpg").getFile());


由于您的图像存储在存储
LabelDemo
的同一个包中,请尝试以下操作:

ImageIcon labelIcon = new ImageIcon(LabelDemo.class.getResource("/deitel9/maldive.jpg").getFile());

根据,您选择的
ImageIcon
构造函数需要文件名或文件路径,因此映像需要位于文件系统上,而不是类路径上

从指定的文件创建图像图标。[…]指定的字符串可以是文件名或文件路径

根据您的描述,当您的项目布局如下所示时

\---src
    \---deitel9
        LabelDemo.java
        maldive.jpg
然后,您应该能够检索位于类路径上的图像作为资源,如下所示:

ImageIcon labelIcon = new ImageIcon(LabelDemo.class.getResource("maldive.jpg"));
根据,您选择的
ImageIcon
构造函数需要文件名或文件路径,因此映像需要位于文件系统上,而不是类路径上

从指定的文件创建图像图标。[…]指定的字符串可以是文件名或文件路径

根据您的描述,当您的项目布局如下所示时

\---src
    \---deitel9
        LabelDemo.java
        maldive.jpg
然后,您应该能够检索位于类路径上的图像作为资源,如下所示:

ImageIcon labelIcon = new ImageIcon(LabelDemo.class.getResource("maldive.jpg"));

要想知道在这种情况下你做错了什么,你只需打个电话

File file = new File ("maldive.jpg");
System.out.println(file.getAbsolutePath());
这将打印出它为您的文件查看的绝对路径,这可能会指示您做错了什么


当然,如果您知道如何使用调试器,您不需要第二行(从技术上讲,甚至不需要第一行,但这有点棘手;)

要找出在这种情况下您做错了什么,只需调用

File file = new File ("maldive.jpg");
System.out.println(file.getAbsolutePath());
这将打印出它为您的文件查看的绝对路径,这可能会指示您做错了什么


当然,如果您知道如何使用调试器,您不需要第二行(从技术上讲,甚至不需要第一行,但这有点棘手;)

文件在包中的位置?文件在包中的位置?Java文件和图像在包
deitel9
Java文件中,图像在包
deitel9