简体   繁体   中英

Hi! setting ImageIcon to a Jframe and other component dont seem to work. I need help pls

I'm trying to set a Icon on the top left corner for my Window. but don't seem to work.. what do you think seems to be the problem? I also tried setting an Icon and Text on an instance of JLabel but only the text came out.

The link is a screenshot for the output of the JFrame I made.

https://i.stack.imgur.com/1FDh6.png

public class Window extends JFrame{
    
    private final int WIDTH  = 600;

    public Window(){
    // Setting the Window
        setTitle("Module Organizer");
        ImageIcon img = new ImageIcon("checkmark-square.png");
        setIconImage(img.getImage());
        setSize(WIDTH,(WIDTH*3)/4);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // adding components
        
        add(new NavPane(), BorderLayout.WEST);
        
        setVisible(true);
    }

}

Here is the code

 import javax.swing.JFrame;
 import javax.swing.ImageIcon;
 import javax.swing.JPanel;
 import javax.swing.JLabel;
 public class Window{
 JFrame f=new JFrame("Test");
 public Window(){
 JPanel j=new JPanel();

 f.setSize(1000,500);
 ImageIcon img = new ImageIcon("Image.jpg");
 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 JLabel l=new JLabel(img);
 j.add(l);
 f.add(l);
 f.setVisible(true);
}
 public static void main(String[] args) {
    new Window();
}
 }

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