BorderLayout Problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 11
Reputation: george21 is an unknown quantity at this point 
Solved Threads: 0
george21 george21 is offline Offline
Newbie Poster

BorderLayout Problem

 
0
  #1
Sep 27th, 2009
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);

}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: BorderLayout Problem

 
1
  #2
Sep 27th, 2009
You are declared panelCenter2, but you still add buttons to panelCenter.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: BorderLayout Problem

 
0
  #3
Sep 27th, 2009
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.
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 330 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC