2mac
2mac

Reputation: 1689

Use a layout in place of a component

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?

Upvotes: 1

Views: 42

Answers (1)

Lukas Rotter
Lukas Rotter

Reputation: 4188

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

Upvotes: 2

Related Questions