So I've made a class WindowDisp
which extends JFrame
:
public class WindowDisp extends JFrame{
private static final long serialVersionUID = 3245091489595286109L;
private int height, width;
private JPanel panel;
private JLabel mainPane;
public WindowDisp(int a, int b, int pw, int ph){
height = a;
width = b;
Container c = getContentPane();
c.setPreferredSize(new Dimension(width, height));
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
mainPane = new JLabel();
mainPane.setPreferredSize(new Dimension(pw, ph));
mainPane.setFont(new Font("Monospaced", Font.PLAIN, 10));
panel = new JPanel();
panel.setPreferredSize(getSize());
panel.add(mainPane);
add(panel);
setVisible(true);
pack();
}
public static void main(String[] args){
WindowDisp win = new WindowDisp(400, 400, 400, 400);
}
}
From my Main
class, I declare a WindowDisp
where its height
and width
are equal to its ph
and pw
. The problem, however, is that, upon running my program, white 'bars' appear around the default background colored JPanel
in the frame. They appear to be padding the panel from the right and the bottom, as though there is space in the frame that the panel is not occupying, although, if my coding is correct, the panel should be the same size as the frame's ContentPane
, should it not?
I've found that removing either of the two pack();
commands does not remove these bars, although removing the first one changes them to black, and removing the second widens the one on the right. Of course, removing both of them causes the frame not to be the same size as its ContentPane
. Furthermore, removing the add(panel);
altogether has no effect.
I can't figure out what in this code is causing that seemingly empty space to appear in my frame, and, again, in my program, all four values being passed to the Window
constructor are equal. What seems really strange is that, even if I just remove the add(panel);
, nothing at all changes visa vi the white padding. In fact, I can comment out everything from mainPane =...
to add(panel);
and that doesn't affect it at all.