Is there any way to add 0 margin/padding to a JPanel so it fits the WHOLE screen it's suppose?
Here is what I am talking about: (see the little space above the panel? why doesn't it cover that as well?)
Here is the way it's setup:
labelStatus = new JLabel("\n\nSorry, the server crashed!");
labelStatus.setForeground(Color.WHITE.brighter());
statusPanel = new JPanel();
statusPanel.setBackground(Color.RED.darker());
statusPanel.add(labelStatus);
statusPanel.setPreferredSize(new Dimension(513,352));
and this is how it gets iniated:
} catch (Exception rwe) {
// System.exit(0);
game.add(statusPanel);
game.remove(yPanel);
game.remove(xPanel);
game.remove(roomPanel);
game.remove(userPanel);
game.remove(titlePanel);
game.remove(introPanel);
statusPanel.setOpaque(true);
labelStatus.setVisible(true);
System.out.println("Server went down -- crap!");
c.append("\nServer crashed!");
rwe.printStackTrace();
}
So.. how do I fix that small gap?
You can set a panel's layout manager using the JPanel constructor. For example: JPanel panel = new JPanel(new BorderLayout()); After a container has been created, you can set its layout manager using the setLayout method.
We can set a margin to a JButton by using the setMargin() method of JButton class and pass Insets(int top, int left, int bottom, int right) as an argument.
by giving the JLabel constructor the String argument that is the text to describe what's in there. A JPanel, on the other hand, is a Panel, a designated part of the GUI. Given that it is a distinct part, it is naturally a Container, and should thus be given the stuff.
By default, all containers use the layout manager FlowLayout
. FlowLayout specifies an hgap and a vgap to separate components. Try this:
((FlowLayout)game.getLayout()).setVgap(0);
It's strange, though, that there's no horizontal gap on the left.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With