Angry Red Panda
Angry Red Panda

Reputation: 439

JFrame - setLocation

I am trying to relocate my panels with the command 'panel.setLocation(int, int);', but it is not working for me.

public static void createWindow()
{
    JFrame frame = new JFrame("JFrame Examinierung");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(1000, 600));
    frame.setLocation(300, 300);
    frame.pack();
    frame.setResizable(false);
    frame.setVisible(true);
    //Frame_Menu.java
    Frame_Menu frame_Menu = new Frame_Menu();
    frame.setMenuBar(frame_Menu.getMenubar());

    JPanel jpanel_mother = new JPanel();
    jpanel_mother.setPreferredSize(new Dimension(0, 0));
    jpanel_mother.setBackground(Color.WHITE);
    jpanel_mother.setVisible(true);
    frame.add(jpanel_mother);

    JPanel jpanel_titel = new JPanel();
    jpanel_titel.setPreferredSize(new Dimension(500, 50));
    jpanel_titel.setVisible(true);
    jpanel_titel.setBackground(Color.BLUE); 

    jpanel_titel.setLocation(100, 100); //not working relocation

    jpanel_mother.add(jpanel_titel);

    JLabel jlabel_jpanel_titel = new JLabel("JFrame Examinierung");
    jlabel_jpanel_titel.setFont(new Font("Segoe UI", Font.BOLD, 20));
    jlabel_jpanel_titel.setForeground(Color.white);
    jpanel_titel.add(jlabel_jpanel_titel);

}

I want to move the JPanel freely, anyone got suggestions on how to fix this little problem?

Upvotes: 1

Views: 1848

Answers (1)

Roy van Rijn
Roy van Rijn

Reputation: 850

The setLocation-method only works if you are using absolute positioning. To do this you'll need to set a null layout manager, like this:

panel.setLayout(null);

But this is not recommended and very hard to get right, because you'll also need to manually set the bounds.

Check this link for more information: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

I'd recommend finding another more suitable way of doing your layout.

Upvotes: 2

Related Questions