java - Regarding a JButton and an ImageIcon -


i reading book on java, , studying swing graphical user interface components. while doing so, stumbled upon code example, author setting image on jbutton unusual way, depicted below:

icon bug1 = new imageicon( getclass().getresource( "bug1.gif" ) ); 

in order above work, need have image on same folder .class files. can explain me why using particular code (which far know, must reflection code, again, not particularly sure one) , if there 1 way me same thing, without getting things complicated does?

things complicated if don't understand them. once have understood above code does, extremely simple.

getclass() returns class object of current object (this). getresource() called relative path above, looks file, in classpath, named bug1.gif, , in same package class object being called. looks bug1.gif in same package class containing above code. getresource() returns url. , imageicon constructor takes url argument, loads image bytes url, , constructs imageicon these bytes.

so whole thing creates imageicon file available classpath, in same package class calling code. , makes sense: put images used given class in same package class, , release jar containing classes , images of application.

you have figured reading javadoc of these methods.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -