Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.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 正在尝试导入我自己的包_Java_Import - Fatal编程技术网

Java 正在尝试导入我自己的包

Java 正在尝试导入我自己的包,java,import,Java,Import,我正在制作一个游戏,我遇到了一个重要的问题。 正如您所看到的,我想要导入JavaGame.src.resources,因为我正在尝试导入一个img。 以下是我的目录: 我不需要知道resourcesmanager.java上的代码,目前它是空白的。 基本上,这个类在包MainClass中,但是我想访问resources包。提供了什么?您的包名是resources,因此请写以下内容: package mainClasses; /* * Frame Info and all th

我正在制作一个游戏,我遇到了一个重要的问题。 正如您所看到的,我想要导入JavaGame.src.resources,因为我正在尝试导入一个img。 以下是我的目录:

我不需要知道resourcesmanager.java上的代码,目前它是空白的。
基本上,这个类在包MainClass中,但是我想访问resources包。提供了什么?

您的包名是
resources
,因此请写以下内容:

package mainClasses;
    /*
     * Frame Info and all that ****,
     * mainFrame is the actual frame itself
     * it will refer to MainC.java a lot Main class = Main Class
     */
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import JavaGame.src.resources.*; //Problem Code
    import javax.swing.JButton;
    import javax.swing.JFrame;


    public class mainFrame extends JFrame {


private static final long serialVersionUID = 1L;

public mainFrame() {
    JButton playButton = new JButton();
    JButton infoButton = new JButton();
    JButton exitButton = new JButton();
    int x = 300, y = 300;
    setSize(x, y);
    setVisible(true);
    setLayout(new FlowLayout());
    setTitle("Kingdom Raider");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    /*Buttons and Properties*/

     playButton.setBounds(x, y, 200, 100);
     playButton.setText("Play!");
    add(playButton);

     infoButton.setBounds(x, y, 200, 100);
     infoButton.setText("Information");
    add(infoButton);

     exitButton.setBounds(x, y, 200, 100);
     exitButton.setText("Exit");
    add(exitButton);
            /* Add image here */

}

public void Painting (Graphics g) {
    //Me.
}
   }

目录结构的其余部分是特定于Eclipse的,与Java类路径无关。您的源文件夹是
src
,因此这是类层次结构的根。你应该这样做

import resources.*; // No-Problem Code
然而,使用
*
是一种糟糕的形式,您应该尝试只导入这个特定类中需要的类,就像您使用
javax.swing.JButton
所做的那样。因此:

import resources.*

资源不像包那样导入。请看这里:


具体来说,这里有一个如何从参考资料中加载图像的示例:。

如果您复制并粘贴了一些内容,Eclipse将自动为您导入内容。也许您可以编写一个使用JavaGame.src.resources中的内容的短剪辑,然后复制粘贴,eclipse将完成其余部分。

我认为这可能是eclipse配置的问题。有时Eclipse会将项目中的源文件夹视为包的一部分。只需从Eclipse工作区中删除项目,而不从文件系统中删除项目,然后再次导入

这应该会有帮助

从eclipse中删除项目而不将其从文件系统中删除:

将现有项目导入eclipse:


您要做的是访问存储在资源文件夹中的文件。要执行此操作,请不要像尝试导入文件夹那样导入文件夹。相反,按照小马托尼的建议去做,并使用它

import resources.ResourceManager;
例如,要将文件转换为图标,必须使用资源路径:

getResource("FileName.ext")
我还应该指出,如果使用NetBeans,则需要将资源文件夹添加为源文件夹;否则,项目将无法按照您希望的方式编译,从而导致.jar文件无法访问资源文件。Eclipse可能与此类似。要将文件夹添加为源,请执行以下操作:

  • 右键单击项目
  • 选择属性
  • 单击类别窗格中的“源”
  • 单击“添加文件夹”并选择您创建的资源文件夹

  • 包名只是
    resources
    src
    文件夹和
    JavaGame
    文件夹并不包括在内,但在类路径上拥有资源没有什么错。嗯,那会很奇怪。你能发布更多信息吗?从你的截图上看应该足够了。。。
    java.net.URL path = this.getClass().getResource("FileName.jpg");
    ImageIcon icon = new ImageIcon(path);