FlowLayout in Swing

16,185

Solution 1

Add northpanel and northpanel to a panel having GridLayout(0, 1) then

add(panel, BorderLayout.NORTH);

Solution 2

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

public class StackOverflow14837740
{
    public static void main (String[] args)
    {
        SwingUtilities.invokeLater (new Runnable ()
        {
            @Override
            public void run ()
            {
                createAndShowGUI ();
            }
        });
    }

    private static void createAndShowGUI ()
    {
        JFrame frame = new JFrame ();
        frame.setLayout (new BorderLayout ());
        frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);

        JPanel northPanel = new JPanel (new GridLayout (2, 1));

        JPanel welcomePanel = new JPanel (new FlowLayout (FlowLayout.CENTER));      
        welcomePanel.add (new JLabel ("Welcome"));

        northPanel.add (welcomePanel);

        JPanel radioPanel = new JPanel (new FlowLayout (FlowLayout.CENTER));

        JRadioButton button1 = new JRadioButton ("Button 1", true);
        JRadioButton button2 = new JRadioButton ("Button 2", false);

        ButtonGroup group = new ButtonGroup ();
        group.add (button1);
        group.add (button2);

        radioPanel.add (button1);
        radioPanel.add (button2);

        northPanel.add (radioPanel);

        JPanel middlePanel = new JPanel (new GridLayout (3, 3));

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                middlePanel.add (new JButton ("Button " + i + j));
            }
        }

        JPanel southPanel = new JPanel (new FlowLayout (FlowLayout.CENTER));

        southPanel.add (new JLabel ("Whose turn:"));
        southPanel.add (new JButton ("Reset"));

        frame.add (northPanel, BorderLayout.NORTH);
        frame.add (middlePanel, BorderLayout.CENTER);
        frame.add (southPanel, BorderLayout.SOUTH);

        frame.pack ();
        frame.setVisible (true);
    }
}

It looks like this (although you have to resize it a bit):

printscreen

Solution 3

You cannot add more than one component to a BorderLayout region and you are doing it in the end. You need to change your northpanel to be a BorderLayout, then put your welcome text and northtestpanel1 inside it, like this:

 northpanel -> BorderLayout, JFrame's NORTH position
 welcome -> northpanel NORTH position
 northpanel1 -> FlowLayout, northpanel CENTER position

You may have problems about put welcome in the center (I'm just guessing, maybe it will working fine). If you don't have any solution to it, just wrap it into a new JPanel and use the FlowLayout with FlowLayout.CENTER.

Solution 4

You have to use GridLayout OR GridBagLayout instead of Flow-layout.First set GridBagLayout of northpanel and then add your required components,let say your radio buttons and wellcome label. For more detail you can consult here.

Share:
16,185
KAKAK
Author by

KAKAK

Updated on June 04, 2022

Comments

  • KAKAK
    KAKAK almost 2 years

    This is my layout.

    enter image description here

    The two radio button should be below the welcome label.

    like this:

    __________________________
    |                        | 
    |        WELCOME         |
    |         *  *           |
    |                        |
    |                        |
    |                        |
    |________________________|
    

    the two asterisk are the radio buttons.

    My Code:

    northpanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    northpanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
    
    
    northpanel.add(welcome);  //this welcome text label
    
    northpanel1.add(r1);   //this radio 1
    northpanel1.add(r2);   //this radio 2
    
    
    add(northpanel,BorderLayout.NORTH);
    add(northpanel1,BorderLayout.NORTH);
    
  • KAKAK
    KAKAK about 11 years
    the welcome label however is pushed far left on the screen but the 2 radio buttons are centralised as needed
  • KAKAK
    KAKAK about 11 years
    wow..thank you so much..i am really amazed by the efforts put in the answers by this community!! keep up the good work! :)
  • Radu Murzea
    Radu Murzea about 11 years
    @user2016977 That's because the community is huge (hundreds of thousands of people)... ooh, and because we're awesome :D .
  • trashgod
    trashgod about 11 years
    @user2016977: Try FlowLayout.CENTER on the welcome panel, as SoboLAN shows.