Hello I have really just started Coding in Java a few weeks ago and I thought my first project would be an MP3Player. Then code more and more to get a better grasp. One question I would like to ask is how do I like setup my res folder in my project, then load pics from it for my JFrame’s background or a buttons background? Thanks for any help guys. I have my JFrame’s background set and my custom buttons but I can’t jar it for friends to test until I get this problem fixed.
It has a little bit to do with how your project is setup in whatever IDE you’re using. It also has to do with how you’re loading the images, as some methods do not work in jar files. However as long as the resources are in your classpath (and thus included in your jar file) something like this should load the image both in a jar and simply running it from the class files.
BufferedImage img = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("path/to/image.png");
I set up most of my projects in Eclipse using a top level folder named src that contains a folder for code and a folder for resources. That way both the code and resources get included in the classpath and jar file. Roughly like this:
src
-org.zman.game (code)
-resources
--images
Can I make a whole seperate class for all these images, then call them into my JFrames class? Like
Close.setIcon(new ImageIcon("C:/Users/Tony/Desktop/res/Title Bar/close/button_CLOSE_DEMO.png"));
this is my JButton’s default look. Is there anyway I can just write
Close.setIcon(new ImageIcon("/res/button_CLOSE_DEMO.png"));
and have it load from my res like that?
add the “res” folder to the build path (properties->java build path->add folder) and use
URL url = this.getClass().getResource("/button_CLOSE_DEMO.png");
Close.setIcon(new ImageIcon( url );
“Cannot use this in a static context”
You are trying to use it in a static method. If you want to do that do
URL url = <Class name>.class.getResource("/button_CLOSE_DEMO.png");
Close.setIcon(new ImageIcon( url );
Where is the name of the class that this code is in.
Then don’t use it in a static context. If you want to get anywhere, you have to learn what error messages like that mean: go through the java tutorials, learn about OOP, learn what ‘this’ is and when you can use it. Don’t just react helplessly or randomly to error messages like they’re something out of your control.
Thank you for your help. You guys have fixed it for me! Oh also, I know. I don’t approve of other people helping my troubles but I’m VERY new to Java. Anyway, guys I have one more question. I made my own custom button Layout:
JButton Close = new JButton();
Close.setIcon(new ImageIcon(bCD));
Close.setSelectedIcon(new ImageIcon(bCCD));
Close.setDisabledIcon(new ImageIcon(bCH));
Close.setDisabledSelectedIcon(new ImageIcon(bCH));
Close.setPressedIcon(new ImageIcon(bCCD));
Close.setRolloverIcon(new ImageIcon(bCH));
Close.setRolloverSelectedIcon(new ImageIcon(bCH));
Close.setBorderPainted(isDefaultLookAndFeelDecorated());
and when I go to another Class for a PatchNotes GUI, I try the same code and the
BUTTONHERE.setBorderPainted(isDefaultLookAndFeelDecorated());
code comes with "The method “isDefaultLookAndFeelDecorated() is undefined for the type PatchNotesGUI. Any ideas?”
Use built-in GUI designer in your IDE - it will make your life much easier.
If you really have to program this manually, make sure that “isDefaultLookAndFeelDecorated()” is defined in your class (any IDE should mark this as error if it is not). I would suggest using true or false instead.
I would suggest reading this article - it should be useful for you.