1

I'd like to create a main window frame with a BorderLayout that contains other layouts as its components.

How can I add, say, a FlowLayout to my BorderLayout's NORTH position?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2mac
  • 1,609
  • 5
  • 20
  • 35

1 Answers1

2

Here's a little program that shows you:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
    public class LayoutExample {
        public static void main (String[] args) {
            JFrame frame = new JFrame("Frame with BorderLayout");
            frame.setLayout(new BorderLayout());
            JPanel flow = new JPanel();
            JLabel label = new JLabel("This is a flowlayout.");
            flow.setBorder(new LineBorder(Color.BLACK));
            flow.setLayout(new FlowLayout());
            flow.add(label);

            frame.add(flow, BorderLayout.NORTH);
            frame.setSize(300,300);
            frame.setVisible(true);
       } 
    }

That's how it looks like at the end:

enter image description here

Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35