Results 1 to 9 of 9
Thread: need help with simple layout
- 01-12-2013, 05:09 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 5
- Rep Power
- 0
-
Re: need help with simple layout
Don't use FlowLayout then. I'd suggest that the JFrame's contentPane use its default BorderLayout and that you place the red JPanel in the BorderLayout.NORTH (also known as BorderLayout.PAGE_START) position and the JLabel in the BorderLayout.CENTER position. If you don't want the buttons to expand the width of the GUI, then nest the JPanel in another JPanel that uses FlowLayout (the default for JPanel) and place this wrapper JPanel in the JFrame's BorderLayout.NORTH slot.
- 01-12-2013, 05:40 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 5
- Rep Power
- 0
-
Re: need help with simple layout
Please clarify -- what are you seeing currently, and what are you trying to see. Also consider creating and posting an SSCCE, a small compilable and runnable code that contains only enough code to compile, run, and show your problem. In other words, it will contain the JButtons, JLabels and JPanels but the buttons should have no listeners attached to them since this would have nothing to do with the problem at hand.
- 01-12-2013, 06:23 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 5
- Rep Power
- 0
- 01-12-2013, 06:32 PM #6
Member
- Join Date
- Jan 2013
- Posts
- 5
- Rep Power
- 0
Re: need help with simple layout
this is
Java Code:frame.setLayout(new FlowLayout()); JPanel panel=new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); panel.setBackground(Color.red); panel.setBorder(BorderFactory.createLineBorder(Color.red)); frame.add(panel,BorderLayout.LINE_START); JButton button1 = new JButton("Button1");panel.add(button1); button1 = new JButton("Button1");panel.add(button1); button1 = new JButton("Button2");panel.add(button1); button1 = new JButton("Button3");panel.add(button1); button1 = new JButton("Button4");panel.add(button1); button1 = new JButton("Button5");panel.add(button1); JLabel textLabel = new JLabel("Label",SwingConstants.CENTER); textLabel.setPreferredSize(new Dimension(300, 100));//300,300 textLabel.setBorder(BorderFactory.createLineBorder(Color.black)); frame.add(textLabel, BorderLayout.CENTER);
-
Re: need help with simple layout
One way:
- Consider using BorderLayout for the entire GUI, which by the way is the default layout for top level windows such as JFrames, JDialogs, and JApplets.
- Create a JPanel, say called westPanel and give it a BorderLayout.
- Add your red-bordered JButton holding JPanel to this westPanel in the BorderLayout.NORTH position.
- Add the westPanel JPanel to the GUI in the BorderLayout.WEST position.
- Create a JPanel, say called centerPanel and also give it a BorderLayout.
- Add your JLabel to the centerPanel in the BorderLayout.NORTH position.
- Add the centerPanel to the GUI in the BorderLayout.CENTER position.
- This way everything will try to stick to the north or top of your GUI.
- 01-12-2013, 07:13 PM #8
Member
- Join Date
- Jan 2013
- Posts
- 5
- Rep Power
- 0
Re: need help with simple layout
I'm sorry, but this is what I get:

Java Code:frame.setLayout(new BorderLayout()); JPanel westPanel=new JPanel(new BorderLayout()); westPanel.setBorder(BorderFactory.createLineBorder(Color.red)); JButton button1 = new JButton("Button1");westPanel.add(button1); button1 = new JButton("Button1");westPanel.add(button1); button1 = new JButton("Button2");westPanel.add(button1); button1 = new JButton("Button3");westPanel.add(button1); button1 = new JButton("Button4");westPanel.add(button1); button1 = new JButton("Button5");westPanel.add(button1); frame.add(westPanel,BorderLayout.WEST); JPanel centerPanel=new JPanel(new BorderLayout()); JLabel textLabel = new JLabel("Label",SwingConstants.CENTER); textLabel.setPreferredSize(new Dimension(300, 300)); textLabel.setBorder(BorderFactory.createLineBorder(Color.black)); centerPanel.add(textLabel,BorderLayout.NORTH); frame.add(centerPanel,BorderLayout.CENTER);
-
Re: need help with simple layout
Please re-read my recommendations carefully as you're not following them and I think that they spell things out pretty explicitly.
Similar Threads
-
Grid Layout change layout alignment of control s
By rellicott in forum SWT / JFaceReplies: 1Last Post: 02-13-2012, 05:11 PM -
Layout manager for a very simple problem!
By Anoyz in forum AWT / SwingReplies: 2Last Post: 11-10-2011, 06:13 PM -
how do i link one page to a other page and how do i add simple text to my layout
By gunner1711 in forum AndroidReplies: 0Last Post: 08-23-2011, 12:38 PM -
Simple Layout Question
By Guy in forum AWT / SwingReplies: 7Last Post: 07-13-2011, 01:57 PM -
Simple GUI layout manager
By globo in forum New To JavaReplies: 3Last Post: 11-16-2010, 01:17 AM


LinkBack URL
About LinkBacks
Reply With Quote






Bookmarks