JPanel上的0顶部边距

6

有没有办法将JPanel的边距/填充设置为0,以使其完全适合屏幕?

这就是我所说的:(看到面板上方的小空间了吗?它为什么不能覆盖那个空间?) see the space above?

这是它的设置方式:

        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));

这是它的初始化过程:

} 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();
            }

那么,我该如何修复那个小间隙呢?
1个回答

7

默认情况下,所有容器使用布局管理器FlowLayout。FlowLayout指定了一个水平间距和垂直间距来分隔组件。尝试这样做:

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

然而,奇怪的是左侧没有水平间距。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接