java 如何在java中的边框布局中嵌入网格布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10155654/
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-30 23:50:10  来源:igfitidea点击:

How to embed a grid layout inside a border layout in java

javaswinggrid-layout

提问by R Doolabh

I have a border layout and I want to add a grid layout to the center section. However, I can't declare a grid and then add it my my center border. How can I do this?

我有一个边框布局,我想向中心部分添加一个网格布局。但是,我无法声明网格,然后将其添加到我的中心边框。我怎样才能做到这一点?

public Liability_Calculator(String s)
{
    super(s);
    setSize(325,200);

    c = getContentPane();
    c.setLayout(new BorderLayout());

    //the top label
    total = new JLabel("Total monthly liabilities ", JLabel.CENTER);
    c.add(total, BorderLayout.NORTH);


    //the grid
    GridLayout grid = new GridLayout(2,2);

    text_field1 = new JTextField(7);

    //I GET AN ERROR HERE!!!!!!!
    grid.add(text_field1);

    //AND ERROR HERE!!!!!!!!!!!!!
    c.add(grid, BorderLayout.CENTER);




    setVisible(true);
}

回答by Hovercraft Full Of Eels

You're trying to add a component to a layout, and that simply cannot be done. Instead use a JPanel, give it a GridLayout, and then add the component to the JPanel (acting as the "container" here).

您正在尝试将组件添加到layout,而这根本无法完成。而是使用 JPanel,给它一个 GridLayout,然后将组件添加到 JPanel(在这里充当“容器”)。

In general, you will want to nest JPanels with each using the best layout for the GUI, here the inner JPanel using GridLayout and the outer one using BorderLayout. Then you simply add the inner JPanel to the outer one (here your contentPane) in the BorderLayout.CENTER position.

通常,您会希望使用 GUI 的最佳布局将 JPanel 与每个 JPanel 嵌套在一起,这里使用 GridLayout 的内部 JPanel 和使用 BorderLayout 的外部 JPanel。然后,您只需将内部 JPanel 添加到 BorderLayout.CENTER 位置的外部 JPanel(这里是您的 contentPane)。

回答by Taken Display Name

Even thou this is a question answered a long time ago. Here is my solution to that. I just wanted to provide code visualization in my answer derived from what Hovercraft had to say.

即使你这是很久以前回答的问题。这是我的解决方案。我只是想在我的答案中提供代码可视化,这些答案来自 Hovercraft 所说的内容。

public class Display extends JFrame
{

JPanel gridHolder = new JPanel(); // panel to store the grid
private GridLayout buttonsGrid; // space containing a buttons
private JButton myButtons[]; // grid is to be filled with these buttons
private BorderLayout mainGUILayout; // main gui layout
private Container mainGuiContainer;

public Display()
{
    mainGUILayout = new BorderLayout(5,5); // the width of space in between the main gui elements.
    mainGuiContainer = getContentPane(); // getting content pane
    mainGuiContainer.setLayout(mainFrameLayout); // setting main layout
    buttonsGrid = new GridLayout(4, 1, 5, 5); // 4 buttons one over the other
    myButtons = new JButton[4]; // player's hand represented with buttons
    gridHolder.setLayout(buttonsGrid);

                for (int x = 0; x < 4; x++)
                {

                myButtons[x] = new JButton (" ");
                gridHolder.add(myButtons[x]);
                }

            add(gridHolder, BorderLayout.WEST);
            setVisible(true);
}
}



public class MainGUILaunch
{
    public static void main (String args[])
    {
        Display myApplication = new Display();
        myApplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myApplication.setSize(1024, 1024);
        myApplication.setVisible(true); // displaying application
    }
} // End of MainGUILaunch