Swing中的FlowLayout

时间:2013-02-12 16:58:26

标签: java swing layout

这是我的布局。

enter image description here

两个单选按钮应位于欢迎标签下方。

像这样:

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

两个星号是单选按钮。

我的代码:

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);

4 个答案:

答案 0 :(得分:5)

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);
    }
}

它看起来像这样(虽然你必须稍微调整一下):

printscreen

答案 1 :(得分:3)

northpanelnorthpanel添加到panel的{​​{1}},然后

GridLayout(0, 1)

答案 2 :(得分:2)

您无法向BorderLayout区域添加多个组件,而您最终会这样做。您需要将northpanel更改为BorderLayout,然后将欢迎文本和northtestpanel1放入其中,如下所示:

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

你可能在将welcome置于中心时遇到问题(我只是猜测,也许它会正常工作)。如果您没有任何解决方案,只需将其包装到新的JPanel中,然后将FlowLayoutFlowLayout.CENTER一起使用。

答案 3 :(得分:1)

你必须使用GridLayout或GridBagLayout而不是Flow-layout.First设置northpanel的GridBagLayout,然后添加你需要的组件,比如你的单选按钮和wellcome标签。有关详细信息,请参阅here