Results 1 to 7 of 7
- 03-30-2011, 06:21 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
Panels with BorderLayout Question
I have created a frame which has 2 panels. The frame and panels has to use BorderLayout. 1 panel with the 3 buttons need to display in South and the other panel with 3 buttons in the Center. I understand about adding buttons in the panel and the panels are the ones that is need to be set in the cordinates but I am having problems. Could someone please show me how I can get my panels to go to their respective spot? I tried -
JPanel mp = new JPanel(new BorderLayout(BorderLayout.SOUTH));, but getting error. Help would be greatly appreciated. If not maybe someone can show me how to get 1 panel with the multi buttons to 1 quandrant.
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()); pane.add(getSubPanel()); frame.setSize(400,300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private static JPanel getMainPanel() { JPanel mp = new JPanel(new BorderLayout); mp.setLayout(new BorderLayout()); mp.add(new JButton("Button 1")); mp.add(new JButton("Button 2")); mp.add(new JButton("Button 3")); return mp; } private static JPanel getSubPanel() { JPanel sp = new JPanel(new BorderLayout()); sp.setLayout(new BorderLayout()); sp.add(new JButton("Button 4")); sp.add(new JButton("Button 5")); sp.add(new JButton("Button 6")); return sp; } }
- 03-30-2011, 06:27 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
There is a bit wrong with the panel creating code. When you initially create the panel you give it an argument of new BorderLayout, which is incorrect. You need to use a default constructor and then the next line correctly sets the layout. When you add the buttons you should be specifying where to place them.
this adds a button to the center of the panel, it's nearly identical for all other border layout locations. Finally you would add the panels to the frames panel in a similar fashion.Java Code:panel.add(new JButton("hi"), BorderLayout.CENTER);
- 03-31-2011, 02:10 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
Ok did that, but now all I see is the "Button 6" on the SOUTH quadrant and missing all the buttons in the Center and 4-5 for South. What am I missing?
Java Code:public static void main(String[] a) { JFrame frame = new JFrame("BorderFlow Frame"); Container pane = frame.getContentPane(); //pane.setLayout(new BorderLayout); pane.add(getMainPanel()); pane.add(getSubPanel()); frame.setSize(400,300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private static JPanel getMainPanel() { JPanel mp = new JPanel(); mp.setLayout(new BorderLayout()); mp.add(new JButton("Button 1"),BorderLayout.CENTER ); mp.add(new JButton("Button 2"),BorderLayout.CENTER); mp.add(new JButton("Button 3"),BorderLayout.CENTER); return mp; } private static JPanel getSubPanel() { JPanel sp = new JPanel(); sp.setLayout(new BorderLayout()); sp.add(new JButton("Button 4"),BorderLayout.SOUTH); sp.add(new JButton("Button 5"),BorderLayout.SOUTH); sp.add(new JButton("Button 6"),BorderLayout.SOUTH); return sp; }
- 03-31-2011, 02:24 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
You can only add 1 component to "SOUTH", 1 component to the NORTH etc...
If you want multiple components in the "SOUTH", then create a panel, add components to the panel and then add the panel to the SOUTH.
- 03-31-2011, 02:54 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
I didn't think it sounded right but, did what was on reply. I have tow panels main & sub with one (sub) sp.setLayout(new BorderLayout());...but am confused by "add the panel to the SOUTH". Could someone please give me an example or steer me in the right path:confused::confused::confused:?
Java Code:public static void main(String[] a) { JFrame frame = new JFrame("BorderFlow Frame"); Container pane = frame.getContentPane(); //pane.setLayout(new BorderLayout()); pane.add(getMainPanel()); pane.add(getSubPanel()); frame.setSize(400,300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } 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; } 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; }
- 03-31-2011, 04:05 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Read the Swing tutorial: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container). There are plenty of working examples.Could someone please give me an example or steer me in the right path
-
as suggested in his new post on this same subject. Collwill -- please don't double post the same question!
Similar Threads
-
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 -
BorderLayout Demo
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:51 PM -
Help with BorderLayout
By lenny in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 07:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks