beyerss
beyerss

Reputation: 1352

jframe component layout

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

Answers (2)

Alex Ntousias
Alex Ntousias

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

Peter Lang
Peter Lang

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

Related Questions