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();
}
}
So my question is that how pack()
working differently when we do setLocationRelativeTo(null)
prior to it and later to it?
And if we do setLocationRelativeTo(null)
after pack()
it works good.
Although the difference in this minimal example is not huge but in my actual working GUI this is creating a huge problem. Please explain.
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?