2010-08-20 168 views
6

有什麼方法可以爲JPanel添加0邊距/填充以適合它假設的整個屏幕?JPanel上的頂部邊距

下面是我講的:(?看到面板上的小空間爲什麼沒有覆蓋呢?) 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)); 

,這是它如何被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(); 
      } 

所以..我該如何解決這個問題小的差距?

回答

6

默認情況下,所有容器都使用佈局管理器FlowLayout。 FlowLayout指定一個hgap和一個vgap來分隔組件。試試這個:

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

雖然這很奇怪,左邊沒有水平間隙。