Java 设置面板的大小

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

Setting the size of panels

javauser-interfacesizepanel

提问by Karen

I have 3 panels. One is the main panel which holds 2 smaller panels.

我有3个面板。一个是主面板,其中包含 2 个较小的面板。

For the main panel, I used

对于主面板,我使用了

setPreferredSize(new Dimension(350, 190));

For the smaller left panel, I used

对于较小的左侧面板,我使用了

setPreferredSize(new Dimension(100, 190));

For the smaller right panel, I used

对于较小的右侧面板,我使用了

setPreferredSize(new Dimension(250, 190));

but the smaller panels remain the same size. How can I fix this?alt text

但较小的面板保持相同的尺寸。我怎样才能解决这个问题?替代文字

This is the code I have in my main Panel.

这是我在主面板中的代码。

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

public class Panel extends JPanel
{

public Panel(Prison prison)
{
    setup();
    build(prison);
}

private void setup()
{
    setBorder(BorderFactory.createLineBorder(Color.blue));
    setLayout(new BorderLayout(1, 1));
    setPreferredSize(new Dimension(350, 190));
}

private void build(Prison prison)
{
    JTabbedPane tab = new JTabbedPane();       
    tab.addTab("Input", null, new InputPanel(), "Input");
    tab.addTab("Display", null, new DisplayPanel(), "Display");
    add(tab);            
}
}

采纳答案by Michael Borgwardt

Don't do this.

不要这样做。

The whole point of layout managers is to allow dynamic resizing, which is necessary not just for user-resizable windows but also changing texts (internationalization) and different default font sizes and fonts.

布局管理器的重点是允许动态调整大小,这不仅对于用户可调整大小的窗口是必需的,而且对于更改文本(国际化)和不同的默认字体大小和字体也是必需的。

If you just use the layout managers correctly, they will take care of panel sizes. To avoid having your components stretched out all over the screen when the user increases the window size, have an outermost panel with a left-aligned FlowLayout and the rest of the UI as its single child - that will give the UI its preferred size and any surplus is filled with the background color.

如果您只是正确使用布局管理器,它们将处理面板尺寸。为避免在用户增加窗口大小时组件在整个屏幕上伸展开来,请使用左对齐 FlowLayout 的最外面板,并将 UI 的其余部分作为其单个子项 - 这将为 UI 提供其首选大小和任何盈余填充了背景色。

回答by Jonathan Feinberg

It looks like you're using a GridLayout, or perhaps a FlowLayout, neither being what you want. You probably want to use a BoxLayout, which respects its components preferred sizes.

看起来您正在使用 GridLayout 或 FlowLayout,这两者都不是您想要的。您可能想要使用BoxLayout,它尊重其组件的首选大小。

final JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(leftPanel);
p.add(mainPanel);
p.add(rightPanel);

回答by Jill Renee

also- that is the preferred size. if you don't want to allow resizing, you can also set the maximum sizes as well.

也是-这是首选尺寸。如果您不想允许调整大小,您也可以设置最大大小。

if you're able, you may want to check out the MIGlayout, but BoxLayout is also easy to use and in the java toolkit already.

如果可以,您可能想查看MIG布局,但 BoxLayout 也很容易使用,并且已经在 J​​ava 工具包中。

回答by smkelsey

It's this one.

是这个。

buttonPanel = new JPanel();
buttonPanel.setSize(new Dimension(30, 100));

Don't let the layout manager adjust your sizes.

不要让布局管理器调整您的尺寸。