Java 在 JFrame 中自动调整 JPanel 的大小

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

Automatically size JPanel inside JFrame

javaswingsizejframejpanel

提问by Igor

I have a JPanelsubclass on which I add buttons, labels, tables, etc. To show on screen it I use JFrame:

我有一个JPanel子类,我在上面添加按钮、标签、表格等。为了在屏幕上显示它,我使用JFrame

MainPanel mainPanel = new MainPanel(); //JPanel subclass

JFrame mainFrame = new JFrame();
mainFrame.setTitle("main window title");
mainFrame.getContentPane().add(mainPanel);
mainFrame.setLocation(100, 100);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

But when I size the window, size of panel don't change. How to make size of panel to be the same as the size of window even if it was resized?

但是当我调整窗口大小时,面板的大小不会改变。即使调整了大小,如何使面板的大小与窗口的大小相同?

采纳答案by Ole

You can set a layout manager like BorderLayout and then define more specifically, where your panel should go:

你可以设置一个像 BorderLayout 这样的布局管理器,然后更具体地定义你的面板应该去哪里:

MainPanel mainPanel = new MainPanel();
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.add(mainPanel, BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setVisible(true);

This puts the panel into the center area of the frame and lets it grow automatically when resizing the frame.

这会将面板置于框架的中心区域,并在调整框架大小时让它自动增长。

回答by Richard Walton

You need to set a layout manager for the JFrame to use - This deals with how components are positioned. A useful one is the BorderLayout manager.

您需要为要使用的 JFrame 设置一个布局管理器 - 这涉及如何定位组件。一个有用的是 BorderLayout 管理器。

Simply adding the following line of code should fix your problems:

只需添加以下代码行即可解决您的问题:

mainFrame.setLayout(new BorderLayout());

(Do this before adding components to the JFrame)

(在将组件添加到 JFrame 之前执行此操作)

回答by sv_in

If the BorderLayout option provided by our friends doesnot work, try adding ComponentListerner to the JFrame and implement the componentResized(event) method. When the JFrame object will be resized, this method will be called. So if you write the the code to set the size of the JPanel in this method, you will achieve the intended result.

如果我们朋友提供的 BorderLayout 选项不起作用,请尝试将 ComponentListerner 添加到 JFrame 并实现 componentResized(event) 方法。当 JFrame 对象将被调整大小时,将调用此方法。因此,如果您在此方法中编写代码来设置 JPanel 的大小,您将达到预期的结果。

Ya, I know this 'solution' is not good but use it as a safety net. ;)

是的,我知道这个“解决方案”不好,但可以将其用作安全网。;)

回答by tddmonkey

As other posters have said, you need to change the LayoutManager being used. I always preferred using a GridLayout so your code would become:

正如其他海报所说,您需要更改正在使用的 LayoutManager。我总是喜欢使用 GridLayout 这样你的代码就会变成:

MainPanel mainPanel = new MainPanel();
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new GridLayout());
mainFrame.pack();
mainFrame.setVisible(true);

GridLayout seems more conceptually correct to me when you want your panel to take up the entire screen.

当您希望面板占据整个屏幕时,GridLayout 对我来说在概念上似乎更正确。

回答by Luki B. Subekti

From my experience, I used GridLayout.

根据我的经验,我使用了 GridLayout。

    thePanel.setLayout(new GridLayout(a,b,c,d));

a = row number, b = column number, c = horizontal gap, d = vertical gap.

a = 行号,b = 列号,c = 水平间隙,d = 垂直间隙。


For example, if I want to create panel with:


例如,如果我想创建面板:

  • unlimited row (set a = 0)
  • 1 column (set b = 1)
  • vertical gap= 3 (set d = 3)
  • 无限行(设置 a = 0)
  • 1 列(设置 b = 1)
  • 垂直间隙= 3(设置d = 3)

The code is below:

代码如下:

    thePanel.setLayout(new GridLayout(0,1,0,3));

This method is useful when you want to add JScrollPane to your JPanel. Size of the JPanel inside JScrollPane will automatically changes when you add some components on it, so the JScrollPane will automatically reset the scroll bar.

当您想将 JScrollPane 添加到 JPanel 时,此方法很有用。JScrollPane 里面的 JPanel 的大小会在你添加一些组件时自动改变,所以 JScrollPane 会自动重置滚动条。