JLabel setLocation and setBounds methods not working

152 views Asked by At

First of all, I am well aware of the existence of Layout Managers, I just don't want to use it in this case.

I'm writing a simple main menu for my application, but no matter what I do the Image (JLabel) always sets itself in left upper corner of screen. I've tried using both methods (setLocation, setBounds), but it does not make any difference.

I'm sure it is some stupid mistake, but I can't seem to figure it out.

Here's my code:

import javax.swing.*;

public class Main extends JFrame{

    protected ImageIcon createImageIcon(String path,
                                        String description) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    Main() {
        ImageIcon image1=createImageIcon("/monopoly.jpg","");
        JLabel image1l=new JLabel(image1);
        image1l.setLocation(200,200);
        image1l.setBounds(330, 300, 140, 60);
        add(image1l);
    }

    public static void main(String[] args) {
        Main f=new Main();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.pack();
        f.setTitle("Monopoly");
        f.setSize(800,800);
        f.setLocationRelativeTo(null);
        f.setLayout(null);
    }
}
0

There are 0 answers