简体   繁体   中英

JFrame don't work when adding an Image

I'm having a problem that seems a little bit strange. When I'm adding a new ImageIcon and try to run the program it just gives me a gray screen and no objects are added.

public class Ctester {

public Ctester(){
    Frame();
}

public void Frame(){

    JFrame fr = new JFrame();
    fr.setVisible(true);
    fr.setSize(500, 500);
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fr.setResizable(false);

    JPanel p = new JPanel(new GridBagLayout());
    ImageIcon icon = new ImageIcon(getClass().getResource("zippo.jpg"));

    JLabel l = new JLabel(icon)
    JButton bm1 = new JButton("hellu");

    p.add(l);
    p.add(bm1);
    fr.add(p);


}

public static void main(String[]args){
    new Ctester();
}
}

But if I remove the line:

    ImageIcon icon = new ImageIcon(getClass.getResource("zippo.jpg"));

then it works perfect. I'm not getting any error messages and i been looking for a while but I could only find that the problem might be something with the gridbaglayout.

How can i solve it or do I have to change layout? (this is just a simple code based of the original as an example so any solutions that not include having to change layout is highly appreciated)

Most of the code is wrong:

  1. Swing components should be create on the Event Dispatch Thread (EDT).

  2. The frame should be made visible AFTER all the components have been added to the frame.

  3. You are attempting to use a GridBagLayout, but you aren't using any GridBagConstraints when you add the components.

  4. Method names (Frame) should NOT start with an upper case character.

Read the Swing Tutorial for Swing basics.

You can find working examples in:

  1. How to Use GridBagLayout
  2. How to Use Icons

so any solutions that not include having to change layout is highly appreciated

Start with the working examples and make changes for your requirements. If you start with better structured code you will have less problems.

If something draws correctly after a window resize or minimize/maximize that is a sure sign of a race condition because you are not starting your GUI on the event dispatcher thread. Your main problem is you are calling setVisible() way to early, don't call setVisible() until after you have added all components to your top-level container. The other problem is you are not starting your GUI on the event dispatcher thread. Please see the main method below in the fixed code:

public class Ctester {
    public Ctester() {
        Frame();
    }

    public void Frame() {
        JFrame fr = new JFrame();
        fr.setSize(500, 500);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setResizable(false);

        JPanel p = new JPanel(new GridBagLayout());

        JLabel l = new JLabel("label");
        JButton bm1 = new JButton("hellu");

        p.add(l);
        p.add(bm1);
        fr.add(p);

        fr.setVisible(true);
    }

     public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 new Ctester();
            }
        });
    }
}

Try this code you might want to put that first line of code in a try catch just in case that it doesn't find the image.

URL iconURL = getClass().getResource("/some/package/favicon.png");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
fr.setIconImage(icon.getImage());

Also use a .ico file if you are only using this program on Windows but use a .png if it is going to be multi-platform

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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