JPanel上的0上边距

tes*_*est 6 java swing

有没有办法为JPanel添加0边距/填充,以便它适合它所假设的整个屏幕?

这就是我所说的:(看到面板上方的小空间?为什么它也不包括它?) 看到上面的空间?

这是它的设置方式:

        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));
Run Code Online (Sandbox Code Playgroud)

这就是它如何被推断:

} 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();
            }
Run Code Online (Sandbox Code Playgroud)

那么..我该如何解决这个小差距?

Eri*_*son 6

默认情况下,所有容器都使用布局管理器FlowLayout.FlowLayout指定用于分隔组件的hgap和vgap.试试这个:

((FlowLayout)game.getLayout()).setVgap(0);

但奇怪的是,左边没有水平间隙.