2013-03-17 97 views
1

我想向我的面板添加狀態欄。帶有邊框佈局的左下角的內容

事實上,它看起來像這樣:

enter image description here

但我想在左下角要顯示的文字。

這裏是我的代碼:

private JPanel pnl1, pnl2; 
    //pnl1 contains everything exept the status bar, pnl2 only status bar 
private JLabel lab3; 
    //lab3 is the status bar 
    (…) 

public static void main(String[] args) { 
    new Panel1(); 
} 

public Panel1() { 

    pnl1 = new JPanel(); 
    pnl2 = new JPanel(); 

    lab3 = new JLabel("Started.");   //Statusbar 
    (…) 
    pnl1.setLayout(new FormLayout(
      "40*($lcgap, 10dlu)", 
      "30*($lgap, 10dlu)" 
      )); 
    CellConstraints cc = new CellConstraints(); 

    pnl1.add(…) 
    pnl2.add(lab3); 

    this.add(pnl1); 
this.add(pnl2, BorderLayout.PAGE_END); 
    this.(…) 

謝謝您的幫助!

回答

0

您正在將標籤添加到pnl2中,似乎沒有改變佈局。 JPanel的默認佈局是FlowLayout,它使用居中對齊。

如果你想在標籤要左對齊,可以使用FlowLayout,確保對齊方式設置爲LEADING

pnl2.setLayout(new FlowLayout(FlowLayout.LEADING)); 
    pnl2.add(lab3); 

或者你可以使用不同的佈局,比如BorderLayout

pnl2.setLayout(new BorderLayout()); 
    pnl2.add(lab3); 
+0

謝謝。對不起,但我不能投你的答案,因爲我還沒有足夠的聲譽:-( – IndexOutOfBoundsException 2013-03-18 17:50:55