Java 有人能告诉我它是从哪里采集图像的吗?

Java 有人能告诉我它是从哪里采集图像的吗?,java,eclipse,user-interface,Java,Eclipse,User Interface,这是我从oracle获得的代码,但我不知道它需要从何处拾取图像,因为每次我创建包含图像的源文件夹时,它仍然无法拾取图像。或者如果你能告诉我代码的哪一部分正在提取图像?我需要在我的工作中使用相同的代码 在Demo.java所在的文件夹中创建一个名为images的文件夹。把你的照片放在那里。根据您的代码,图像名称是“Bird.png”、“Cat.png”、“Dog.png”、“Rabbit.png”、“Pig.png” 因此,您的项目结构如下所示: package demo; import jav

这是我从oracle获得的代码,但我不知道它需要从何处拾取图像,因为每次我创建包含图像的源文件夹时,它仍然无法拾取图像。或者如果你能告诉我代码的哪一部分正在提取图像?我需要在我的工作中使用相同的代码


Demo.java
所在的文件夹中创建一个名为
images
的文件夹。把你的照片放在那里。根据您的代码,图像名称是“Bird.png”、“Cat.png”、“Dog.png”、“Rabbit.png”、“Pig.png”

因此,您的项目结构如下所示:

package demo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*`enter code here`
 * ComboBoxDemo.java uses these additional files:`enter code here`
 *   images/Bird.gif
 *   images/Cat.gif
 *   images/Dog.gif
 *   images/Rabbit.gif
 *   images/Pig.gif
 */
public class Demo extends JPanel
implements ActionListener {
    JLabel picture;

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public Demo() {
        super(new BorderLayout());

        String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };

        //Create the combo box, select the item at index 4.
        //Indices start at 0, so 4 specifies the pig.
        JComboBox petList = new JComboBox(petStrings);
        petList.setSelectedIndex(4);
        petList.addActionListener(this);

        //Set up the picture.
        picture = new JLabel();
        picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
        picture.setHorizontalAlignment(JLabel.CENTER);
        updateLabel(petStrings[petList.getSelectedIndex()]);
        picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));

        //The preferred size is hard-coded to be the width of the
        //widest image and the height of the tallest image + the border.
        //A real program would compute this.
        picture.setPreferredSize(new Dimension(177, 122+10));

        //Lay out the demo.
        add(petList, BorderLayout.PAGE_START);
        add(picture, BorderLayout.PAGE_END);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }

    /** Listens to the combo box. */
    public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox)e.getSource();
        String petName = (String)cb.getSelectedItem();                                                          
        updateLabel(petName);
    }

    protected void updateLabel(String name) {
        ImageIcon icon = createImageIcon("images/" + name + ".png");
        picture.setIcon(icon);
        picture.setToolTipText("A drawing of a " + name.toLowerCase());
        if (icon != null) {
            picture.setText(null);
        } else {
            picture.setText("Image not found");
        }
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = Demo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("ComboBoxDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JComponent newContentPane = new Demo();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);


        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
此行提供的图像路径是
images/name.png
,而
name
的值可以是
{“鸟”、“猫”、“狗”、“兔”、“猪”}

上行将
图标设置为
JLabel
图片


在项目的根目录中创建一个名为
images
的文件夹,并将所有
.png
图像放在
images
文件夹中。

请参见:

我终于找到了问题所在,但我无法理解为什么他们用Oracle的方式编写的代码不起作用

返回在发布的原始代码

您需要转到项目所在的文件夹。你会看到: 箱子 图像 src

“图像”文件夹中可能包含所有预期的图像文件,但程序无法以某种方式访问它们。将gif文件复制到包含bin、images和src的文件夹中

现在您将看到: 箱子 图像 src Bird.gif Cat.gif 等等

现在返回并更改Oracle的代码:

//ImageIcon icon=createImageIcon(“图像/”+name+“.gif”)//这样不行


如果要调试程序获取文件的位置,可以使用以下命令创建
文件
对象并请求其绝对路径


user.dir
中创建一个
images
文件夹,并将图像放入其中。您可以看到
user.dir
打印出的
System.getProperty(“user.dir”)
是什么意思。您所说的“不工作”是什么意思?您是否将java文件和images文件夹放在名为
demo
的文件夹中?我希望我可以在这里发布屏幕截图,但是的,我有主项目名称demo,下面有两个文件夹名称src,分别是demo.java和images文件夹,其中包含应以png格式显示的图像。感谢删除第一行,上面写着
packagedemo。这应该可以解决您的问题。也尝试过了,它给了我这个错误“线程中的异常”main“java.lang.error:未解决的编译问题:at demo.demo.main(demo.java:98)”编辑您的问题并添加项目结构的一个片段。你的代码在我的机器上运行得很好。
demo
    src
    └───demo
        │   Demo.java
        └───images
                Bird.png
                Cat.png
                Dog.png
                Rabbit.png
                Pig.png
ImageIcon icon = createImageIcon("images/" + name + ".png");
picture.setIcon(icon);
    ImageIcon icon = createImageIcon(name + ".gif");
System.out.println(new File("images/Bird.png").getAbsolutePath()); // C:\... or /...