2

I am trying to add to label to north and south of a panel and add the panel to the centre of my frame. If I don't specify the size of the labels by myself the program is perfect but when I give them specific dimension:

label.setPreferredSize(di);
label2.setPreferredSize(di);

it gets messy! However the size of panel and frame are larger and (100,100) Any idea?

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;


import javax.swing.JFrame;

public class BorderLayoutDemo2 {

    public static void main(String args[])
    {
        Frame frame = new Frame();
    }
}

class Frame extends JFrame
{

    private static final long serialVersionUID = 1L;

    public Frame(){
        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Dimension di = new Dimension(50,50);
        Dimension dim = new Dimension(200,200);

        JPanel panel = new JPanel();
        panel.setPreferredSize(dim);

        JLabel label = new JLabel("Here is my label");
        JLabel label2 = new JLabel("Here is my label2");



        JMenuBar menu = new JMenuBar();
        JMenu setting = new JMenu("Setting");
        JMenuItem exit = new JMenuItem("Exit");
        JMenuItem add = new JMenuItem("Add");
        setting.add(add);
        setting.add(exit);
        menu.add(setting);

        label.setPreferredSize(di);
        label2.setPreferredSize(di);

        panel.add(label,BorderLayout.NORTH);
        panel.add(label2,BorderLayout.SOUTH);

        add(menu,BorderLayout.NORTH);
        add(panel,BorderLayout.CENTER);


        pack();
        setVisible(true);
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bernard
  • 4,240
  • 18
  • 55
  • 88
  • 1
    You can't. The preferred/min/max size methods are "suggestions" that layout managers may choose to ignore, which what `BorderLayout` is doing. – MadProgrammer Sep 26 '12 at 04:50
  • 1
    in addition to @MadProgrammer 's comment: don't ever use setXXSize size in your code, [some reasons](http://stackoverflow.com/q/7229226/203657) – kleopatra Sep 26 '12 at 09:32

1 Answers1

4

You are assuming the layout of the panel is BorderLayout when you add components, ie: panel.add(label,BorderLayout.NORTH);. But you are not setting the layout and JPanel uses FlowLayout which is its default. You can fix it like this:

JPanel panel = new JPanel(new BorderLayout());
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • that perfectly makes sense... I'm little bit new to swing. Do you memorise all the default BorderLayout and etc for every component. That's heaps of information which freaks me out ;) – Bernard Sep 26 '12 at 04:53
  • Wait till the change it again :P – MadProgrammer Sep 26 '12 at 04:55
  • 2
    @Bernard `BorderLayout` is default for content panes, `FlowLayout` is default for `JPanel`. Be sure to check out [Using Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html) and [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for more details. – tenorsax Sep 26 '12 at 05:00