I have a JFrame which sets the content pane to a JPanel, which has a BoxLayout and two more JPanels, one is a "top bar" and the other is the content I want to work with. The top bar should occupy precisely the size I'm giving it and the content panel should take up the rest, but instead they both take half of the space. What am I doing wrong?
My classes:
public class TopBar extends JPanel
{
public TopBar()
{
setLayout(new FlowLayout());
setPreferredSize(new Dimension((int) (MyFrame.WIDTH / MyFrame.COMPRESSION), MyFrame.TOPBAR));
add(new JButton("something"));
}
}
public class ContentPanel extends JPanel
{
public ContentPanel()
{
setLayout(null);
}
}
public class MyJpanel extends JPanel
{
private JPanel topPanel;
private JPanel contentPanel;
public MyJpanel()
{
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
topPanel = new TopBar();
contentPanel = new ContentPanel();
add(topPanel);
add(contentPanel);
}
}
You should use setSize instead of setPreferredSize.