当我们显式设置容器的位置时,pack() 函数的行为如何?
How the pack() function behave when we explicitly set the location of the container?
这是我在主要 GUI 设计中遇到的问题的最小示例
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();
}
}
所以我的问题是,pack()
之前和之后 setLocationRelativeTo(null)
的工作方式有何不同?
如果我们在 pack()
之后执行 setLocationRelativeTo(null)
,效果很好。
虽然这个最小示例中的差异并不大,但在我实际工作的 GUI 中,这会产生一个大问题。请解释。
EDIT 我的第二个问题是,我听说建议在 pack() 之前调用 setVisible(true)
或 setReiszable(false)
,为什么那么?
setLocationRelativeTo
使用 window 的当前大小来决定应该放置的位置,因为 window 尚未调整大小,它使用 0x0
, pack
提供初始大小,因此首先调用它会提供 setLocationRelativeTo
它需要的信息
您必须认识到,在布局组件之前(或者在这种情况下,直到 window 打包或确定尺寸)它没有尺寸。
例如...
mainFrame.setLocationRelativeTo(null);
mainFrame.pack();
这表示,将大小为 0x0
的 window 定位到屏幕的中心点,然后使用 pack
提供实际大小
哪里...
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
说,pack
框架为其提供初始大小及其相对于屏幕中心的位置,其中考虑了 windows 由 [=14= 计算的大小]
这是我在主要 GUI 设计中遇到的问题的最小示例
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();
}
}
所以我的问题是,pack()
之前和之后 setLocationRelativeTo(null)
的工作方式有何不同?
如果我们在 pack()
之后执行 setLocationRelativeTo(null)
,效果很好。
虽然这个最小示例中的差异并不大,但在我实际工作的 GUI 中,这会产生一个大问题。请解释。
EDIT 我的第二个问题是,我听说建议在 pack() 之前调用 setVisible(true)
或 setReiszable(false)
,为什么那么?
setLocationRelativeTo
使用 window 的当前大小来决定应该放置的位置,因为 window 尚未调整大小,它使用 0x0
, pack
提供初始大小,因此首先调用它会提供 setLocationRelativeTo
它需要的信息
您必须认识到,在布局组件之前(或者在这种情况下,直到 window 打包或确定尺寸)它没有尺寸。
例如...
mainFrame.setLocationRelativeTo(null);
mainFrame.pack();
这表示,将大小为 0x0
的 window 定位到屏幕的中心点,然后使用 pack
提供实际大小
哪里...
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
说,pack
框架为其提供初始大小及其相对于屏幕中心的位置,其中考虑了 windows 由 [=14= 计算的大小]