FlowLayout in Swing

This is my layout.

enter image description here

Two switches should be below the welcome mark.

like this:

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

two asterisks are switches.

My code is:

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);
+5
source share
4 answers

Add northpaneland northpanelin panelwith GridLayout(0, 1), then

add(panel, BorderLayout.NORTH);
+3
source
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 need to resize it a bit):

printscreen

+5
source

BorderLayout, . northpanel BorderLayout, northtestpanel1, :

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

welcome ( , , ). , JPanel FlowLayout FlowLayout.CENTER.

+2

You should use GridLayout OR GridBagLayout instead of Flow-layout. First install the GridBagLayout from the north panel, and then add the necessary components, say, your radio buttons and the welcome shortcut. For more information, you can refer to here .

+1
source

All Articles