Jump to content
  • Advertisement
Sign in to follow this  

Problem with Java program?

This topic is 3964 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


import javax.swing.*;

public class Button2 extends JFrame {
    JButton Load , Save ,Subscribe , Unsubscribe;
    ImageIcon load , save , subscribe , unsubscribe;
    public Button2 (){
        load = new ImageIcon("subscribe.gif");
        save = new ImageIcon("save.gif");
        subscribe = new ImageIcon("subscribe.gif");
        JButton buttonsave = new JButton( "LoadIcon" ,save);
        JButton buttonsubscribe = new JButton(" LoadIcon ",subscribe);
        JButton buttonload = new JButton("LoadIcon" , load);
        JPanel panel = new JPanel();
    public static void main(String args[]){
        Button2 a = new Button2();

The code brings up the three buttons but doesnt fully display the name and graphic. It's even worse without the "pack()" method. Maybe it's something about the images. I dont know but I would appreciate your help. Thanks

Share this post

Link to post
Share on other sites
It sounds like the images aren't actually being loaded. My guess would be that you don't have the right path to the file. You must have the entire path to the file in order for the ImageIcon to correctly load it.

So, you have two choices:

1. Manually add the entire path (not the relative path), which is the way you are doing it now. For example, if you put "subscribe.gif" in the base directory of your C drive, the path would be "C:\\subscribe.gif".

2. Load the pictures relative to the folder your class is in. So, if you have "subscribe.gif" in the same folder as your class, you would use the following code:

java.net.URL subscribeUrl = this.getClass().getResource("subscribe.gif");
subscribe = new ImageIcon(subscribeUrl);

I would suggest that you use the second.

One more thing, when you use JFrames you should always use "getContentPane().add()" to add a component to the frame. So instead of "add(panel)" you should have


Share this post

Link to post
Share on other sites
Thanks for your guys' advice. The wierd thing is that the images are in the same folder as the source. I'll try adding the paths in.

Thanks again.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!