【问题标题】:Trying to import my own packages尝试导入我自己的包
【发布时间】:2012-04-23 15:14:23
【问题描述】:
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。 我的目录是这样的:

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

【问题讨论】:

  • 包名只是resources,不包括src文件夹和JavaGame文件夹

标签: java import


【解决方案1】:

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

import resources.*; // No-Problem Code

目录结构的其余部分特定于 Eclipse,与 Java 类路径无关

【讨论】:

  • 嗯,这很奇怪。你能发布更多信息吗?从您的屏幕截图中应该足够了......
【解决方案2】:

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

import resources.*

但是使用* 是一种不好的形式,您应该尝试只导入您在这个特定类中需要的类,就像您使用javax.swing.JButton 所做的那样。所以:

import resources.ResourceManager;

【讨论】:

    【解决方案3】:

    资源不像包那样被导入。看看这里:http://docs.oracle.com/javase/1.5.0/docs/guide/lang/resources.html

    具体来说,这是一个如何从资源中加载图像的示例:http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource

    【讨论】:

    • 公平点,但是在类路径上拥有资源并没有错。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      我认为这可能是 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/

      【讨论】:

        【解决方案6】:

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

        getResource("FileName.ext")
        

        例如,要将文件转换为图标,您必须使用资源路径:

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

        我还应该指出,如果您使用的是 NetBeans,则需要将资源文件夹添加为源文件夹;否则项目将无法按照您的要求进行编译,从而导致无法访问资源文件的 .jar 文件。 Eclipse 可能类似。要将文件夹添加为源:

        1. 右键单击项目
        2. 选择属性
        3. 点击类别窗格中的“来源”
        4. 点击“添加文件夹”,选择你创建的资源文件夹

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多