Reputation: 1352
How can I specify the location on JFrame that a component (JLabel specifically) is placed? I created a JFrame object and added a JLabel and a JList to the frame but both objects are being placed on top of each other. I have tried using
label.setBounds(10,10,10,10);
list.setBounds(20,20,100,100);
and
label.setLocation(10,10);
list.setLocation(10, 50);
Neither of these are working. Any help is appreciated! Thanks.
Upvotes: 1
Views: 3487
Reputation: 9122
In Java, layout managers are used which determine the way the components will be placed. You can find more information about layout managers here: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
If you definitely want to use coordinates to put your components, you could try:
JFrame frame = new JFrame();
frame.setLayout(null);
Otherwise, a very good GUI editor for Java is NetBeans which is using the GroupLayout by default.
Upvotes: 2
Reputation: 55594
Layout Managers will do that for you.
With the default Layout BorderLayout
, try
setLayout(new BorderLayout());
getContentPane().add(yourLabel, BorderLayout.NORTH);
getContentPane().add(yourList, BorderLayout.CENTER);
Upvotes: 1