首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >摆动中的FlowLayout

摆动中的FlowLayout
EN

Stack Overflow用户
提问于 2013-02-12 16:58:26
回答 4查看 16.4K关注 0票数 5

这是我的布局。

两个单选按钮应该在欢迎标签下.

就像这样:

代码语言:javascript
复制
__________________________
|                        | 
|        WELCOME         |
|         *  *           |
|                        |
|                        |
|                        |
|________________________|

这两个星号是单选按钮。

我的守则:

代码语言:javascript
复制
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);
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-02-12 17:11:29

然后将northpanelnorthpanel添加到具有GridLayout(0, 1)panel

代码语言:javascript
复制
add(panel, BorderLayout.NORTH);
票数 3
EN

Stack Overflow用户

发布于 2013-02-12 17:21:12

代码语言:javascript
复制
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);
    }
}

它看起来是这样的(尽管您必须调整它的大小):

票数 5
EN

Stack Overflow用户

发布于 2013-02-12 17:10:39

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

代码语言:javascript
复制
 northpanel -> BorderLayout, JFrame's NORTH position
 welcome -> northpanel NORTH position
 northpanel1 -> FlowLayout, northpanel CENTER position

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14837740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档