0

I have a problem that my frame becomes quite small when calling the frame.pack-method. code below:

public class ManagementView extends JFrame{
private CardLayout cl;
private JPanel panel;
private MainlandingPanel mainLandingPanel;
private MasterViewPanel masterViewPanel;

ManagementView() {
    masterViewPanel = new MasterViewPanel();
    mainLandingPanel = new MainLandingPanel();

    cl = new CardLayout();
    panel = new JPanel(cl);
    panel.setSize(1100, 770);
    panel.add(mainLandingPanel, "Home");
    panel.add(masterViewPanel, "MasterView");

    //Create the main Frame
    this.setTitle("Hello World");
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.setContentPane(panel);
    this.pack();
    this.setVisible(true);
    cl.show(panel, "Home");
}

}

Both MasterViewPanel and MainLandingPanel have a null Layout, but the member called panel has a given size, in wich i have added a CardLayout, containing the other two panels. I call frame.pack() expecting it to be 1100 x 770, but instead i only get an empty window.

What am i doing wrong? (Yes i do understand that not using a LayoutManager is not recommended, but i just can't understand how that works when designing more complex panels.)

FromBelow
  • 1
  • 2
  • 1
    `"What am i doing wrong? (Yes i do understand that not using a LayoutManager is not recommended"` -- and this is **exactly** what you're doing wrong. The pack method tells the layout managers to layout their components and for the components and containers to size themselves according to their estimated **preferred** sizes (not their sizes). `".. but i just can't understand how that works when designing more complex panels.)"` -- meaning that you need to study the layout managers more and practice using them. Remember that you can nest JPanels each using its own layout manager. – Hovercraft Full Of Eels May 28 '17 at 18:46
  • And yes, you could probably solve this by changing `setSize(...)` to `setPreferredSize(new Dimension(....))`, but then you're left still with null layout using containers, a maintenance nightmare. – Hovercraft Full Of Eels May 28 '17 at 18:47
  • @HovercraftFullOfEels hmm then it seems that i'll have to study layoutmanagers. It's just the fact that null Layout woud've been easy to create panels like this [link](http://imgur.com/yp9r1KE) – FromBelow May 28 '17 at 19:03
  • 1
    Great, then what if the requirements change and you need to add a new button to the top box, now you've got to manually calculate how you're going to move all the other buttons, resize the GUI, make sure that the text works on all types of monitors and on different platforms.... If you used layout managers and needed to add one button, you'd just need one line of code. Now tell me which is easier to maintain? – Hovercraft Full Of Eels May 28 '17 at 19:05

0 Answers0