I need an help for positioning a JPanel in a specific position into a Jframe.
I have a JPanel in a class who extends JFrame, and I need to put this JPanel in a specific x,y position.
It's something like this:
public class Frame extends JFrame {
public Frame(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocation(250, 250);
this.setSize(300,300);
this.setVisible(true);
JPanel panel = new JPanel();
this.add(panel);
panel.setLocation(150, 150);
panel.add(new JButton("Hello!")); // just to show JPanel
}//Frame()
}//Frame
I don't need a LayoutManager for the position of the JPanel, because I need to put that JPanel in a specific position (like 150,150 of the example). But if I do panel.setLocation(150,150) like in the code above, nothing happens, and JPanel remain in the north-centre of the frame (also if I change x,y number instead of 150,150).
And it shows like this:
A JFrame object uses borderlayout manager by default and JPanels use framelayout manager by default. If you want to use absolute positioning then you MUST use null layout because any other layout manager will use setLocation() and setSize() methods according to its own moods and not according to how YOU want it.
Summary: Use setLayout(null) and use null manager and then use setBounds(X,Y,width,height) method.
using setBounds(new Rectangle(96, 67, 98, 41)); you can do this... just see the example
}
Of the frame's content panel, the layout manager by default is a BorderLayout (NWSE+C). Do: