Java JPanel 中的固定宽度、可变高度与流程

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

Fixed width, variable height in JPanel with flow

javaswingfixed-widthlayout-manager

提问by poke

I have an annoying problem with Java's layout managers. I have the following situation: In a panel Aare two other panels Bwith an absolute layout and Cwith a FlowLayout. Bis highly customized and has a fixed size set via setPreferredSize. Cshould have the same fixedwidth as Bbut otherwise be of a variable height, depending on how many components are added in the flow. The resulting Ashould then have the fixed width and A.height + B.heightas the height – at least that is what I want.

我对 Java 的布局管理器有一个恼人的问题。我有以下情况:在面板A中,另外两个面板B具有绝对布局,C具有 FlowLayout。B是高度定制的,并通过setPreferredSize. C应具有与B相同的固定宽度,但高度可变,具体取决于流中添加的组件数量。结果A应该具有固定的宽度和高度 - 至少这是我想要的。A.height + B.height

However what I get is that the width of the panel Ais not fixed at all (even if I set its preferred size), and the contents in panel Care not automatically wrapping but instead are displayed in a long line. Of course this also makes Bhaving a larger width than it should be.

但是我得到的是面板A的宽度根本不固定(即使我设置了它的首选大小),并且面板C中的内容不会自动换行,而是显示在一条长线上。当然,这也使B具有比应有的更大的宽度。

What can I do to fix that? Is there any better layout, or do I have to emulate that all using an absolute layout?

我能做些什么来解决这个问题?有没有更好的布局,还是我必须使用绝对布局来模拟所有布局?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Test extends JPanel
{
    public Test ()
    {
        this.setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );

        JPanel top = new JPanel( null );
        top.setBackground( Color.GREEN );
        top.setPreferredSize( new Dimension( 200, 20 ) );
        JPanel flowPanel = new JPanel( new FlowLayout( FlowLayout.LEFT, 2, 2 ) );

        this.add( top );
        this.add( flowPanel );

        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
        flowPanel.add( new JButton( "x" ) );
    }
}

采纳答案by camickr

Wrap Layoutshould help.

Wrap Layout应该会有所帮助。

回答by Riduidel

The best way to provide the kind of advanced view configuration you desire is by replacing you FlowLayoutby the powerful GridBagLayoutManager. It's worth to note that the constraints, expressed usually by GridBagConstraints, are by far easier to understand when using fluent subclasses, like GBC.

提供你想要的那种高级视图配置的最好方法是用FlowLayout强大的GridBagLayoutManager替换你。值得注意的是,通常由GridBagConstraints表示的约束在使用流畅的子类(如GBC )时更容易理解。

Finally, like always, you should consider taking a look at the Swing tutorial.

最后,像往常一样,您应该考虑查看Swing 教程

回答by Aaron Digulla

  1. You shouldn't add children to the frame directly. Always add to the contentPane-> this.getContentPage().add(...)

  2. To make your layout work, put a JPanel with a GridBagLayoutin the content page and make that panel fill the content page.

  3. Give the GridBagLayout 2 columns.

  1. 您不应该直接将子项添加到框架中。总是添加到contentPane->this.getContentPage().add(...)

  2. 要使您的布局正常工作,请在内容页面中放置一个带有GridBagLayout的 JPanel,并使该面板填充内容页面。

  3. 给 GridBagLayout 2 列。

Now you can't disable horizontal resizing of the window, so you must find a different way to handle the extra space. I suggest to give the first column a fixed width (set fill=NONE) and let the panel Cfill the remaining space.

现在您无法禁用窗口的水平调整大小,因此您必须找到一种不同的方法来处理额外的空间。我建议给第一列一个固定的宽度( set fill=NONE),让面板C填充剩余的空间。

If you set all the sizes (min,max,preferred) of the panel Bto the preferred size, it should not change it's size anymore.

如果您将面板的所有尺寸(最小、最大、首选)B设置为首选尺寸,则不应再更改其尺寸。

Tip: If a Swing layout doesn't work, nest it in another layout.

提示:如果 Swing 布局不起作用,请将其嵌套在另一个布局中。

回答by Doume

Everything you put in a LINE_END or LINE_START part of a BorderLayout will keep its width constant : Only height will be resized if container is resized

您在 BorderLayout 的 LINE_END 或 LINE_START 部分中放置的所有内容都将保持其宽度不变:如果调整容器大小,则只会调整高度

NORTH and SOUTH are width variable

NORTH 和 SOUTH 是宽度可变的

CENTER is width & height variable

CENTER 是宽度和高度可变的

LINE_START & LINE_END are only height variable

LINE_START & LINE_END 只是高度变量

So, I suggest you put your 'C' container in this kind of BorderLayout area

所以,我建议你把你的“C”容器放在这种 BorderLayout 区域