Results 1 to 3 of 3
Thread: BorderLayout for 2 Panels
- 03-31-2011, 03:34 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
BorderLayout for 2 Panels
I see "Button 3" in the Center and "Button 6" in the South but, missing the rest of the buttons. How do I fix it so that I see all 3 buttons in each panel for each quadrant? Help would be greatly appreciated
Java Code:import java.awt.*; import javax.swing.*; public class ShowBorderLayout { public static void main(String[] a) { JFrame frame = new JFrame("BorderFlow Frame"); Container pane = frame.getContentPane(); //pane.setLayout(new BorderLayout()); pane.add(getMainPanel(),BorderLayout.CENTER); pane.add(getSubPanel(),BorderLayout.SOUTH ); frame.setSize(400,300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } ///Center panel private static JPanel getMainPanel() { JPanel mp = new JPanel(); mp.setLayout(new BorderLayout()); mp.add(new JButton("Button 1")); mp.add(new JButton("Button 2")); mp.add(new JButton("Button 3")); return mp; } //South panel private static JPanel getSubPanel() { JPanel sp = new JPanel(); sp.setLayout(new BorderLayout()); sp.add(new JButton("Button 4")); sp.add(new JButton("Button 5")); sp.add(new JButton("Button 6")); return sp; } }
Last edited by collwill; 03-31-2011 at 03:49 AM. Reason: code tags added
-
don't repost, just edit the original post (which I've done for you).
-
Read the layout API and you'll see what happens when you add a component to a BorderLayout-using container without specifying a constant. In other words what's the default location for adding a component to a BorderLayout-using container. The API states this:
As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER:
Java Code:Panel p2 = new Panel(); p2.setLayout(new BorderLayout()); p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER);
So you are adding all three buttons to the BorderLayout.CENTER position of your container, and only the last button will show. A solution: don't use BorderLayout for these containers. Use perhaps a GridLayout.
Similar Threads
-
Panels with BorderLayout Question
By collwill in forum New To JavaReplies: 6Last Post: 03-31-2011, 04:36 AM -
Need some help with panels inside panels
By kakefjes in forum AWT / SwingReplies: 0Last Post: 03-17-2011, 11:36 AM -
BorderLayout problem
By TGH in forum New To JavaReplies: 3Last Post: 05-27-2010, 09:51 PM -
BorderLayout
By oneself in forum New To JavaReplies: 3Last Post: 08-06-2009, 10:59 PM -
Help with BorderLayout
By lenny in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 07:26 AM
Bookmarks