943,881 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 686
  • Java RSS
Sep 27th, 2009
0

BorderLayout Problem

Expand Post »
I can't figure out why this won't put buttons 1, 2, and 3, on the top of the frame, with buttons 4, 5 and 6 on the bottom? I realize there should be space between the top row and the bottom row.



import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;


public class layoutExample
{
public static void main(String[] args)
{

JFrame frame = new JFrame();
frame.setLayout(new BorderLayout(20, 40));




JButton jn1 = new JButton("Button 1");
JButton jn2 = new JButton("Button 2");
JButton jn3 = new JButton("Button 3");

JPanel panelCenter = new JPanel();
panelCenter.add(jn1);
panelCenter.add(jn2);
panelCenter.add(jn3);
frame.add(panelCenter, BorderLayout.CENTER);



JButton jn4 = new JButton("Button 4");
JButton jn5 = new JButton("Button 5");
JButton jn6 = new JButton("Button 6");


JPanel panelCenter2 = new JPanel();
panelCenter.add(jn4);
panelCenter.add(jn5);
panelCenter.add(jn6);
frame.add(panelCenter2, BorderLayout.SOUTH);

frame.setSize(570, 80);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
george21 is offline Offline
11 posts
since Sep 2009
Sep 27th, 2009
1

Re: BorderLayout Problem

You are declared panelCenter2, but you still add buttons to panelCenter.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Sep 27th, 2009
0

Re: BorderLayout Problem

As quuba said, you have declared panelCenter2 but you are still adding the JButtons jn4, jn5 and jn6 to panelCentre1 - which is positioned using BorderLayout.CENTER. Hence why your last three buttons are not positioned in the SOUTH. One of the pitfalls of "copy and paste" ! P.S. also, please use [CODE=java] tags - it makes reading code so much easier.
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JSlider program to display one out of a possible 10 pictures
Next Thread in Java Forum Timeline: Design a drawing application using java JSlider





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC