当我们显式设置容器的位置时,pack()函数的行为如何? [英] How the pack() function behave when we explicitly set the location of the container?

查看:123
本文介绍了当我们显式设置容器的位置时,pack()函数的行为如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的主要GUI设计中遇到的问题的最小示例

Here is the minimal example what problem I am facing in my main GUI design

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.BoxLayout;
class GuiTester
{

JFrame mainFrame = new JFrame();
JPanel panel = new JPanel();

GuiTester()
{

    JButton newButton = new JButton();
    JButton continueButton = new JButton();
    panel.setLayout(new BoxLayout( panel, BoxLayout.Y_AXIS));
    panel.add(newButton);
    panel.add(continueButton);
    panel.add(new JButton());
    panel.add(new JButton());
    panel.add(new JButton());
    mainFrame.getContentPane().add(panel);
    mainFrame.setLocationRelativeTo(null); // if I do it here then the display of window is little towards the right side down corner.
    mainFrame.pack();
    //mainFrame.setLocationRelativeTo(null) if I do it here instead of earlier than mainFrame.pack() it works great.
    mainFrame.setVisible(true);
}

public static void main(String[] args) {
    GuiTester gui = new GuiTester();
  }
}

所以我的问题是,当我们在setLocationRelativeTo(null)之前和之后执行setLocationRelativeTo(null)时,pack()的工作方式有何不同?

So my question is that how pack() working differently when we do setLocationRelativeTo(null) prior to it and later to it?

如果我们在pack()之后执行setLocationRelativeTo(null),则效果很好.

And if we do setLocationRelativeTo(null) after pack() it works good.

尽管在这个最小示例中的差异并不大,但是在我实际工作的GUI中,这却造成了一个巨大的问题.请解释.

Although the difference in this minimal example is not huge but in my actual working GUI this is creating a huge problem. Please explain.

编辑我的第二个问题是,我听说建议在pack()之前调用setVisible(true)setReiszable(false),为什么会这样?

EDIT My second question is that I have heard that it is recommended to call setVisible(true) or setReiszable(false) prior to pack(), why it is so?

推荐答案

setLocationRelativeTo使用窗口的当前大小来决定应该放置窗口的位置,因为尚未确定窗口的大小,因此正在使用0x0pack提供了初始大小,因此首先调用它会为setLocationRelativeTo提供所需的信息

setLocationRelativeTo uses the current size of the window to make decisions about where it should be placed, since the window hasn't been sized yet, it's using 0x0, pack provides the initial size, so calling this first provides setLocationRelativeTo with the information it needs

您必须认识到,在对组件进行布局之前(或者在直到窗口打包或调整大小之前),该组件都没有大小.

You have to recognize that until a component is laid out (or in the case, until the is window packed or sized) it has no size.

例如...

mainFrame.setLocationRelativeTo(null);
mainFrame.pack();

这是说,将尺寸为0x0的窗口定位到屏幕的中心点,然后使用pack提供实际大小

This says, position the window, which is sized 0x0 to the center point of the screen, then use pack to provide an actual size

何处...

mainFrame.pack();
mainFrame.setLocationRelativeTo(null);

说,pack为框架提供初始大小和相对于屏幕中心的位置,该框架考虑了pack

Says, pack the frame to provide it with an initial size and the position it relative to the center of the screen, which takes into consideration windows size as calculated by pack

这篇关于当我们显式设置容器的位置时,pack()函数的行为如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆