2012-04-23 79 views
4
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. 
} 
    } 

我正在创建一个游戏,我有一个导入问题。 正如你所看到的,我想导入JavaGame.src.resources,因为我试图导入一个img。 这里是我的目录如何站:试图导入我自己的包

enter image description here

我不需要知道代码上resourcesmanager.java其目前的空白。 所以基本上,这个类在mainClasses包中,但是我想访问资源包。是什么赋予了?

+0

软件包名称只是'resources','src'文件夹和'JavaGame'文件夹不包括在内 – 2012-04-23 15:16:52

回答

4

你的包名是resources,所以这样写:

import resources.*; // No-Problem Code 

的目录结构的其余部分是专门针对Eclipse并没有什么关系的Java类路径

+0

Didnt work。仍然说无效输入 – 2012-04-23 15:20:22

+0

嗯,那会很奇怪。你能发布更多信息吗?从你的屏幕截图应该是足够的... – 2012-04-23 15:24:24

1

源文件夹是src,所以这是你的类层次结构的根。你应该做

import resources.* 

但是使用*是坏的形式,你应该尝试只导入你在这个特殊的班级需要,就像你与javax.swing.JButton例如完成课程。因此:

import resources.ResourceManager; 
1

如果您复制并粘贴一些东西,Eclipse会自动导入东西 - 也许您可以编写一个使用JavaGame.src.resources中的某个东西的短片,然后复制粘贴eclipse将完成剩下的工作。

0

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

这应该有所帮助。

删除从Eclipse项目,而不从文件系统删除它:

http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Ftasks%2Ftasks-42b.htm

导入现有项目导入Eclipse:

http://agile.csc.ncsu.edu/SEMaterials/tutorials/import_export/

0

你想要做的是什么访问存储在资源文件夹中的文件。要做到这一点,你不要像你试图做的那样导入文件夹。相反,做什么托尼小马劝和使用

getResource("FileName.ext") 

举个例子,把一个文件分成一个图标,你将不得不使用资源路径:

java.net.URL path = this.getClass().getResource("FileName.jpg"); 
ImageIcon icon = new ImageIcon(path); 

我也应该指向如果您使用NetBeans,则需要将资源文件夹添加为源文件夹;否则项目将无法编译您想要的文件,导致无法访问资源文件的.jar文件。 Eclipse可能类似。要添加文件夹作为源:

  1. 在项目上右键点击
  2. 选择性能
  3. 点击“源”,在类别窗格
  4. 点击“添加文件夹”,然后选择你做
  5. 资源文件夹