Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

0 top margin on JPanel

Tags:

java

swing

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?) see the space above?

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?

like image 964
test Avatar asked Aug 20 '10 18:08

test


People also ask

How do I change the layout of a JPanel?

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.

How do you create a margin in Java?

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.

What is the difference between JPanel and JLabel?

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.


1 Answers

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.

like image 111
Erick Robertson Avatar answered Oct 06 '22 00:10

Erick Robertson