Search code examples
javaswingjframeimageicon

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.sstatic.net/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);
    }

}

Solution

  • 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();
    }
     }