java Swing 中的 FlowLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14837740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:34:48  来源:igfitidea点击:

FlowLayout in Swing

javaswinglayout

提问by KAKAK

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

采纳答案by trashgod

Add northpaneland northpanelto a panelhaving GridLayout(0, 1)then

添加northpanelnorthpanel到一个panel拥有GridLayout(0, 1)然后

add(panel, BorderLayout.NORTH);

回答by Radu Murzea

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

打印屏幕

回答by Daniel Pereira

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

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

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

You may have problems about put welcomein 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 JPaneland use the FlowLayoutwith FlowLayout.CENTER.

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

回答by Mohammad Imran

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.

您必须使用 GridLayout 或 GridBagLayout 而不是 Flow-layout。首先设置北面板的 GridBagLayout,然后添加您需要的组件,比如说您的单选按钮和欢迎标签。有关更多详细信息,您可以在此处咨询。