java 尝试导入我自己的包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10283265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to import my own packages
提问by The One that asks alot
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.
}
}
I'm creating a game and I'm having an import problem. As you can see I want to import JavaGame.src.resources, as I'm trying to import an img. Here's how my Directory stands:
我正在创建一个游戏,但我遇到了导入问题。如您所见,我想导入 JavaGame.src.resources,因为我正在尝试导入 img。这是我的目录的立场:
I don't need to know the code on resourcesmanager.java its blank at the moment. So basically, this class here is in packages mainClasses, but i want to access the resources package. What gives?
我现在不需要知道 resourcesmanager.java 上的代码是空白的。所以基本上,这里的这个类在包 mainClasses 中,但我想访问资源包。是什么赋予了?
回答by Lukas Eder
Your package name is resources
, so write this:
你的包名是resources
,所以写这个:
import resources.*; // No-Problem Code
The remaining parts of the directory structure is specific to Eclipse and doesn't have anything to do with Java classpaths
目录结构的其余部分特定于 Eclipse,与 Java 类路径无关
回答by maksimov
Your source folder is src
, so this is the root of your class hierarchy. You should do
您的源文件夹是src
,因此这是您的类层次结构的根。你应该做
import resources.*
However using *
is bad form and you should try and import only classes that you need in this particular class, like you've done with javax.swing.JButton
for example. So:
但是使用*
是不好的形式,您应该尝试只导入您在这个特定类中需要的类,就像您所做的那样javax.swing.JButton
。所以:
import resources.ResourceManager;
回答by Tony the Pony
Resources aren't imported like packages. Have a look here: http://docs.oracle.com/javase/1.5.0/docs/guide/lang/resources.html.
资源不像包那样被导入。看看这里:http: //docs.oracle.com/javase/1.5.0/docs/guide/lang/resources.html。
Specifically, here's an example how to load images from resources: http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource.
具体来说,这里有一个如何从资源加载图像的示例:http: //docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource。
回答by Adam Miller
Eclipse will automatically import stuff for you if you copy and paste something-maybe you could write a short clip that uses something in JavaGame.src.resources, and then copy paste that-eclipse will do the rest.
如果您复制和粘贴某些内容,Eclipse 会自动为您导入内容——也许您可以编写一个使用 JavaGame.src.resources 中的内容的短片,然后复制粘贴——Eclipse 将完成剩下的工作。
回答by TechSpellBound
I think this could be a problem with Eclipse configuration. Sometimes Eclipse considers the source folder in the project as part of the package. Just delete the project from the Eclipse workspace without deleting it from the file system and import it again.
我认为这可能是 Eclipse 配置的问题。有时 Eclipse 会将项目中的源文件夹视为包的一部分。只需从 Eclipse 工作区中删除该项目,而无需将其从文件系统中删除并再次导入。
That should help.
那应该有帮助。
Deleting project from eclipse without deleting it from the file system:
从 Eclipse 中删除项目而不从文件系统中删除它:
Importing existing project into eclipse:
将现有项目导入 Eclipse:
http://agile.csc.ncsu.edu/SEMaterials/tutorials/import_export/
http://agile.csc.ncsu.edu/SEMaterials/tutorials/import_export/
回答by yuyu5
What you want to do is access the files stored in your resources folder. To do this, you don't import the folder as you tried to do. Instead, do what Tony the Pony advised and use
您要做的是访问存储在资源文件夹中的文件。为此,您不要像尝试那样导入文件夹。相反,按照小马托尼的建议和使用
getResource("FileName.ext")
As an example, to turn a file into an icon, you'll have to use the resource path:
例如,要将文件转换为图标,您必须使用资源路径:
java.net.URL path = this.getClass().getResource("FileName.jpg");
ImageIcon icon = new ImageIcon(path);
I should also point out that if you're using NetBeans, you need to add the resource folder as a source folder; otherwise the project won't compile how you want it to, resulting in a .jar file that can't access the resource files. Eclipse might be similar. To add the folder as a source:
我还应该指出,如果您使用的是 NetBeans,则需要将资源文件夹添加为源文件夹;否则项目将无法按照您希望的方式进行编译,从而导致无法访问资源文件的 .jar 文件。Eclipse 可能类似。要将文件夹添加为源:
- Right click on project
- Select properties
- Click "Sources" in categories pane
- Click "Add Folder" and select the resource folder you made
- 右键单击项目
- 选择属性
- 单击类别窗格中的“来源”
- 单击“添加文件夹”并选择您创建的资源文件夹