java JPanel 如何固定大小

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

java JPanel How to fixed sizes

javaswingjpanellayout-manager

提问by ManInMoon

I want to have a resizable panel, that always has the top green panel of a fixed depth. i.e. all changes in height should effect the yellow panel only.

我想要一个可调整大小的面板,它始终具有固定深度的顶部绿色面板。即高度的所有变化应该只影响黄色面板。

My code below is almost OK, except the green panel varies in size a little.

我下面的代码几乎没问题,只是绿色面板的大小略有不同。

How do I do this?

我该怎么做呢?

            Panel.setLayout(new BoxLayout(Panel, BoxLayout.Y_AXIS));
        Panel.setAlignmentX(Component.LEFT_ALIGNMENT);

        JPanel TopPanel = new JPanel();
        TopPanel.setPreferredSize(new Dimension(80,150));
        TopPanel.setVisible(true);
        TopPanel.setBackground(Color.GREEN);
        JPanel MainPanel = new JPanel();
        MainPanel.setPreferredSize(new Dimension(80,750));
        MainPanel.setVisible(true);
        MainPanel.setOpaque(true);
        MainPanel.setBackground(Color.YELLOW);

        Panel.add(TopPanel);
        Panel.add(MainPanel);

enter image description here

在此处输入图片说明

采纳答案by jzd

Your question didn't restrict the solution to a BoxLayout, so I am going to suggest a different layout manager.

您的问题并未将解决方案限制为 a BoxLayout,因此我将建议使用不同的布局管理器。

I would attack this with a BorderLayoutand put the green panel in the PAGE_START location. Then put the yellow panel in the CENTER location without a preferredSizecall.

我会用 a 攻击它BorderLayout并将绿色面板放在 PAGE_START 位置。然后将黄色面板放在 CENTER 位置,无需preferredSize调用。

http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

Here is an SSCCE example of the solution:

这是解决方案的 SSCCE 示例:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestPad extends JFrame {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new BorderLayout());

        JPanel green = new JPanel();
        green.setPreferredSize(new Dimension(80, 150));
        green.setBackground(Color.GREEN);

        JPanel yellow = new JPanel();
        yellow.setBackground(Color.YELLOW);

        frame.getContentPane().add(green, BorderLayout.PAGE_START);
        frame.getContentPane().add(yellow, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

回答by gla3dr

If you make your Paneluse BorderLayoutinstead of BoxLayoutand put TopPanelin BorderLayout.NORTHand MainPanelin BorderLayout.CENTER, then they will both resize horizontally, but only the MainPanelwill resize vertically.

如果你让你Panel使用BorderLayout的不是BoxLayout,并把TopPanelBorderLayout.NORTHMainPanelBorderLayout.CENTER,那么他们都将调整大小的水平,但只有MainPanel将垂直调整。

See the BorderLayout documentation

请参阅BorderLayout 文档