简体   繁体   中英

Java - JFrame not displaying ImageIcon

public Vue(String title) {
    super(title);



    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    this.getContentPane().setLayout(new GridLayout(4, 1));
    JPanel p1 = createPanel1();
    this.getContentPane().add(p1);
    JPanel p2bis = createPanel2bis();
    this.getContentPane().add(p2bis);
    JPanel p3 = createPanel3();
    this.getContentPane().add(p3);




    this.setJMenuBar(createMenuBar());

    this.setPreferredSize(new Dimension(750,400));
    this.pack();
    this.setVisible(true);


    ImageIcon image = new ImageIcon(getClass().getResource("voile.png"));
    this.setIconImage(image.getImage());
    System.out.println(image.getDescription());

}

Hi,

I spent time to find the solution to display my image as a JFrame icon image... And tryed a lot of things like

ImageIcon image = new ImageIcon(Constants.LOGO_ABSOLUTE_PATH);
this.setIconImage(image.getImage());
System.out.println(image.getDescription());

I put "voile.png" file everywhere in my directories : root,src,bin and inside bin and src packages. I also tryed with an online logo and its URI.

However, it actually doesn't work. If anyone can help me ?

I'm running it on Eclipse Oxygen and Windows 10

PS : System.out.println(image.getDescription()); always showing a path where my image is located (Anyway I put it everywhere)

ImageIcon doesn't support PNG transparency. I had to create a blank jpg image and put logo inside. My final code is

ImageIcon image = new ImageIcon(Constants.LOGO);
this.setIconImage(image.getImage());

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