0

I have a pretty straight forward question. Can some please please explain to me why the following JFrame is not showing Hello (100,100 pixels on the left side of the screen and World (100,100 pixels) on the right side of the screen since I am using border layout.

  • I created a JFrame
  • Assigned it a layout of borderlayout
  • Created 2 panels with 2 labels and assigned the panels to be aligned left and right.
  • added the panels to the JFrame
  • Displayed the JFrame

What am I missing?

JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500);
frame.setLayout(new BorderLayout());
frame.setVisible(true);

JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(100,100));
panel1.setBackground(Color.BLUE);
JLabel label1 = new JLabel("Hello");
label1.setBackground(Color.YELLOW); 
label1.setForeground(Color.WHITE);
panel1.add(label1,BorderLayout.LINE_START);

frame.add(panel1);

JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(100,100));
panel2.setBackground(Color.RED);
JLabel label2 = new JLabel("World");
label2.setBackground(Color.CYAN);
label2.setForeground(Color.WHITE);
panel2.add(label2,BorderLayout.LINE_END);

frame.add(panel2);
17slim
  • 1,233
  • 1
  • 16
  • 21
  • 1
    Since your `frame` is managed as a `BorderLayout` you'll want to specify where in the frame `panel1` and `panel2` go. The way you have it, the labels are placed in the panels as if the panels are a `BorderLayout` rather than the frame. – 17slim Aug 29 '18 at 18:33
  • Doesn't `add(component, START_LINE/END_LINE) ` specify where `panel1` and `panel2` go? i.e one to left and one to the right? – Jack Froxern Aug 29 '18 at 18:35
  • It adds to the component `add` is called from (in your case `panel1.add(label1, LINE_START)` would add `label1` to `panel1` at `LINE_START`, but only if `panel1` used `BorderLayout`. You want to add to `frame` with the `BorderLayout` constraints since `frame` has the `BorderLayout`. – 17slim Aug 29 '18 at 18:37

1 Answers1

2

You are setting the Layout-Constraints on the wrong panel.

Instead of panel2.add(label2,BorderLayout.LINE_END); it should be panel2.add(label2) and instead of frame.add(panel2); it should be frame.add(panel2, BorderLayout.LINE_END);.

Same for panel1.

Halko Karr-Sajtarevic
  • 2,248
  • 1
  • 16
  • 14
  • Thanks so much. That makes sense. Why when I set the preferred size of the panel does it not set? – Jack Froxern Aug 29 '18 at 18:38
  • you're welcome! that's part of another question already explained here: https://stackoverflow.com/questions/1783793/java-difference-between-the-setpreferredsize-and-setsize-methods-in-compone – Halko Karr-Sajtarevic Aug 29 '18 at 18:43
  • That just explains the difference between two methods, not why the width will set, but the height will not. – Jack Froxern Aug 29 '18 at 18:58
  • that's true, sorry. if you take a look at the javadoc for `BorderLayout` here: https://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html you can see that it stretches some contents: `The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.` – Halko Karr-Sajtarevic Aug 29 '18 at 19:04