简体   繁体   English

关于JButton和ImageIcon

[英]Regarding a JButton and an ImageIcon

I am currently reading a book on java, and I am currently studying the swing graphical user interface components. 我目前正在阅读一本关于java的书,我目前正在研究swing图形用户界面组件。 While I was doing so, I stumbled upon a code example, where the author was setting an image on a JButton with a very unusual way, depicted below: 在我这样做的时候,我偶然发现了一个代码示例,其中作者正在以非常不寻常的方式在JButton上设置图像,如下所示:

Icon bug1 = new ImageIcon( getClass().getResource( "bug1.gif" ) );

In order for the above to work, you need to have the image on the same folder as the .class files. 为了使上述工作正常,您需要将图像放在与.class文件相同的文件夹中。 Can someone explain to me why is he using this particular code (which as far as I know, it must be reflection code, but then again, I am not particularly sure about this one) and if there is one way for me to do the same thing, without getting things as complicated as he does? 有人可以向我解释为什么他会使用这个特定的代码(据我所知,它必须是反射代码,但是再一次,我不是特别肯定这个代码)如果我有一种方法可以做到这一点同样的事情,没有像他那样复杂的事情?

Things are complicated only if you don't understand them. 只有你不理解它们才会变得复杂。 Once you have understood what the above code does, it will be extremely simple. 一旦你理解了上面代码的作用,就会非常简单。

getClass() returns the Class object of the current object ( this ). getClass()返回当前对象的Class对象( this )。 getResource() called with a relative path as above, looks for a file, in the classpath, named bug1.gif, and in the same package as the Class object being called. 使用上面的相对路径调用getResource() ,在类路径中查找名为bug1.gif的文件,并在与被调用的Class对象相同的包中查找。 So it looks for bug1.gif in the same package as the class containing the above code. 所以它在与包含上述代码的类相同的包中查找bug1.gif。 getResource() returns a URL. getResource()返回一个URL。 And the ImageIcon constructor takes a URL as argument, loads the image bytes from this URL, and constructs an ImageIcon from these bytes. ImageIcon构造函数将URL作为参数,从此URL加载图像字节,并从这些字节构造ImageIcon。

So the whole thing just creates an ImageIcon from a file available from the classpath, in the same package as the class calling this code. 所以整个事情只是从类路径中可用的文件创建一个ImageIcon,与调用此代码的类在同一个包中。 And this makes sense: you put the images used by a given class in the same package as the class, and you release a jar containing the classes and the images of the application. 这是有道理的:您将给定类使用的图像放在与该类相同的包中,然后释放包含应用程序的类和图像的jar。

You would have figured all this by yourself by reading the javadoc of all these methods. 你可以通过阅读所有这些方法的javadoc来自己想出这一切。

Class#getResource(String) returns an URL to resource from the classpath. Class#getResource(String)从类路径返回资源的URL。 This is a convinient way of loading resources that are stored inside your application JAR file. 这是加载存储在应用程序JAR文件中的资源的一种方便方法。 If the image lies somewhere on the HDD, you can load it using: 如果图像位于HDD上的某个位置,您可以使用以下命令加载它:

new ImageIcon(new File("/path/to/the/image").toURI().toURL());

which creates the File object, and gets it's path as URL (which will look like file:///path/to/the/image ). 它创建了File对象,并将其路径作为URL(看起来像file:///path/to/the/image )。

Or even easier, as ImageIcon has a contructor that takes a fileName: 甚至更容易,因为ImageIcon有一个带有fileName的构造函数:

new ImageIcon("/path/to/the/image");

Java's Swing can get over complicated really fast and I think he's actually using this code for simplicity. Java的Swing可以非常快速地复杂化,我认为他实际上是在使用这段代码来简化。

If the image path is relative (the path in your example is), the image has to be located in the same location as the compiled byte code of your program, the .class files. 如果图像路径是相对的(示例中的路径是),则图像必须位于与程序的编译字节代码相同的位置,即.class文件。 If the image was anywhere else, your program simply couldn't find it. 如果图像在其他任何地方,您的程序根本找不到它。

Relative paths like this are very useful especially when you want to compile your finished project into a JAR file. 像这样的相对路径非常有用,尤其是当您想要将已完成的项目编译为JAR文件时。 Your image will be included in the JAR with all your .CLASS files. 您的图像将包含在JAR中,包含所有.CLASS文件。 You will be able to download your compile program, run it, and your images will be right there in your GUI as you would expect. 您将能够下载您的编译程序,运行它,您的图像将在您的GUI中正如您所期望的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM