I have a JPanel (cardLayoutPanel) which layout is CardLayout.
I also have another 3 different panels (firstPagePanel, secondPagePanel, thirdPagePanel)
firstPagePanelsize is approximately around 450x400,secondPagePanelsize is approximately around 800x600thirdPagePanelsize is approximately around 1024x768
I cardLayoutPanel.add all 3 panels and I show firstPagePanel first as my view.
I want to let my program displayed the size of firstPagePanel first which is 450x400
and then if secondPagePanel is displayed, it will change the size to 800x600 and if thirdPagePanel is displayed,it will change the size to 1024,768
Instead of guessing what my 3 panel size should be, I used frame.getPreferredSize() but my first view will always take in the size of my thirdPagePanel which is 1024x768 instead of 450x400;
What can I do to resolve this issue?
public class MainFrame {
private CardLayout cardLayout = new CardLayout();
private JPanel cardLayoutPanel = new JPanel();
private FirstPagePanel firstPagePanel = new FirstPagePanel();
private SecondPagePanel secondPagePanel = new SecondPagePanel();
private ThirdPagePanel thirdPagePanel = new ThirdPagePanel();
private JFrame frame = new JFrame("Panel size test");
public MainFrame() {
cardLayoutPanel.setLayout(cardLayout);
cardLayoutPanel.add(firstPagePanel,"1");
cardLayoutPanel.add(secondPagePanel,"2");
cardLayoutPanel.add(thirdPagePanel,"3");
cardLayout.show(cardLayoutPanel,"1");
frame.add(cardLayoutPanel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.getPreferredSize();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
}