布局管理器首选大小 Java

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

Layout Manager preferredSize Java

javaswinglayout

提问by David

I'm still trying to learn how layout managers work. I made a Frame with two JPanels. The first one contains a textArea with a boxLayout. The second one contains a flow layout with a button.

我仍在努力学习布局管理器的工作方式。我用两个 JPanel 制作了一个框架。第一个包含一个带有 boxLayout 的 textArea。第二个包含带有按钮的流布局。

I set the preferredSize of each panel accordingly, packed them, but got unexpected results.

我相应地设置了每个面板的 preferredSize,将它们打包,但得到了意想不到的结果。

import java.awt.*;
import javax.swing.*;

public class LayoutMgrTest
{
    public static void main(String[] args)
    {
        TableBasic frame = new TableBasic();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.setVisible(true);


        frame.getContentPane().setLayout(new GridLayout(2,1));

        JPanel controlPane = new JPanel();
        JPanel buttonPane = new JPanel();

        controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS));
        controlPane.setPreferredSize(new Dimension(200, 200));
        controlPane.add(new JScrollPane(new JTextArea()));

        buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
        buttonPane.setPreferredSize(new Dimension(100,20));
        buttonPane.add(new JButton("Button1"));
        buttonPane.add(new JButton("Button2"));

        frame.getContentPane().add(controlPane, BorderLayout.NORTH);
        frame.getContentPane().add(buttonPane, BorderLayout.SOUTH);
        frame.setSize(new Dimension(500,500));
        frame.pack();
    }
}

Whatever I do, if I use a grid Layout, it seems to always allocate half of the available space to each control. I have been told that:

无论我做什么,如果我使用网格布局,它似乎总是为每个控件分配一半的可用空间。有人告诉我:

The height of each row is dependent on the height of each component added in each row.

每行的高度取决于每行中添加的每个组件的高度。

The buttonpane's height is 20. It's allocating much more than that to it:

buttonpane 的高度是 20。它分配的比它多得多:

Wasted space

浪费空间

What's wrong with this code?
I would like to leave the two JPanels intact please. It's easy to simply add the textbox and the buttons directly to the frame, but I need to do it with JPanels (because I will be adding borders and other things).

这段代码有什么问题?
我想请保持两个 JPanel 完好无损。将文本框和按钮直接添加到框架很容易,但我需要使用 JPanel 来完成(因为我将添加边框和其他东西)。

回答by MByD

That's the result of using GridLayout as layout manager. Change it to BorderLayout:

这是使用 GridLayout 作为布局管理器的结果。将其更改为 BorderLayout:

frame.getContentPane().setLayout(new BorderLayout());

For example, this code (I changed a little as possible from the original):

例如,这段代码(我从原来的基础上做了尽可能少的改动):

import java.awt.*;
import javax.swing.*;

public class LayoutMgrTest
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        //frame.setVisible(true);   
        //frame.getContentPane().setLayout(new BorderLayout());

        JPanel controlPane = new JPanel();
        JPanel buttonPane = new JPanel();

        controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS));
        controlPane.setPreferredSize(new Dimension(200, 200));
        controlPane.add(new JScrollPane(new JTextArea()));

        buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
        buttonPane.setPreferredSize(new Dimension(100,40));
        buttonPane.add(new JButton("Button1"));
        buttonPane.add(new JButton("Button2"));

        frame.add(controlPane, BorderLayout.NORTH);
        frame.add(buttonPane, BorderLayout.SOUTH);
        //frame.setSize(new Dimension(500,500));
        frame.pack();
        frame.setVisible(true);
    }
}

Generates this frame:

生成此帧:

enter image description here

在此处输入图片说明

回答by camickr

I set the preferredSize of each panel accordingly,

我相应地设置了每个面板的 preferredSize,

That is another problem. You should NOT set the preferred size. That is the job of the layout manager. Just add your components to the panels and let the layout manager do its job.

那是另一个问题。您不应该设置首选大小。这就是布局管理器的工作。只需将您的组件添加到面板,让布局管理器完成它的工作。

Most compnents have a default preferred size. For some you need to give it a little tip. For example when using a text area you would give a "suggested" preferred size by using:

大多数组件都有默认的首选大小。对于某些人,您需要给它一点小费。例如,在使用文本区域时,您可以使用以下命令给出“建议的”首选大小:

JTextArea textArea = new JTextArea(rows, columns);

回答by Dragon8

If you use LayoutManager, you should not set a size on a component except the frame.

如果使用 LayoutManager,则不应在除框架之外的组件上设置大小。

the size for the components is calculated from the different layout managers.

组件的大小是根据不同的布局管理器计算的。

you find more infos at http://download.oracle.com/javase/tutorial/uiswing/layout/howLayoutWorks.html

您可以在http://download.oracle.com/javase/tutorial/uiswing/layout/howLayoutWorks.html 上找到更多信息

in your code, you can add the panel with the textarea to BorderLayout.CENTER. this should solve your problem. the component in BorderLayout.CENTER takes the whole space, except the space needed for the components in NORTH, EAST, SOUTH and WEST.

在您的代码中,您可以将带有 textarea 的面板添加到 BorderLayout.CENTER。这应该可以解决您的问题。BorderLayout.CENTER 中的组件占据了整个空间,除了 NORTH、EAST、SOUTH 和 WEST 中的组件所需的空间。