Sign in to follow this  

Problem with Java program?

This topic is 3629 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

code:

import javax.swing.*;

public class Button2 extends JFrame {
    JButton Load , Save ,Subscribe , Unsubscribe;
    ImageIcon load , save , subscribe , unsubscribe;
    public Button2 (){
        super("Button");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        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();
        panel.add(buttonsave);
        panel.add(buttonsubscribe);
        panel.add(buttonload);
        
        add(panel);
        setVisible(true);
        pack();
    }
    
    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
"getContentPane().add(panel);"

Greggles

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

This topic is 3629 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this