2010-11-28 67 views
0

先上我會告訴你它是如何的Java Swing的GridBagLayout - 問題與放置兩個元素的面板

picture_how_it_is

這是應該的(見下面的鏈接在我評論):

標籤必須打開並且TabPane必須在所有方向上用餘量填充屏幕的其餘部分。

這是使用GridBagLayout的鋪設出來的代碼:

// Layout --begin-- 
    this.mainPanel.setLayout(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 

    // Layout:headLineLabel --begin-- 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    gbc.ipadx = 0; 
    gbc.ipady = 0; 
    gbc.insets = new Insets(0, 10, 0, 0); 
    gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
    this.mainPanel.add(this.headLineLabel, gbc); 
    // Layout:headLineLabel --end-- 

    // Layout:FestplattenreinigerGraphicalUserInterfaceTabbedPane --begin-- 
    gbc.gridx = 0; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.ipadx = 0; 
    gbc.ipady = 0; 
    gbc.insets = new Insets(10, 10, 10, 10); 
    gbc.anchor = GridBagConstraints.CENTER; 
    this.mainPanel.add(new FestplattenreinigerGraphicalUserInterfaceTabbedPane(), gbc); 
    // Layout:FestplattenreinigerGraphicalUserInterfaceTabbedPane --end-- 
    // Layout --end-- 

事情你需要知道:

  1. FestplattenreinigerGraphicalUserInterfaceTabbedPane延伸JTabbedPane的。
  2. 我試圖指定所有約束(即使使用它們的默認值)來練習。
  3. 如果我推薦了錨,佈局仍然如此。

這件事有什麼不對?

THX非常! (對不起,我無法直接發佈圖片+只有1個鏈接,因爲我是新的.-)

+0

它應該如何:http://img832.imageshack.us/img832/6905/howitshouldbe.jpg – JavaHater 2010-11-28 16:18:42

回答

0

你已經沒有必要的weightx和weighty,或者一切都會在中間凝聚在一起。所以加入tabpane加前:

gbc.weightx = 1.0; 
gbc.weighty = 1.0; 
+0

thx男人。在設置權重之後,當我重新調整窗口的tabpane並且標籤相互之間有很大的差距時。現在我把它們放在同一個x和y網格中,現在它工作得很好,再次感謝你讓我的夜晚! – JavaHater 2010-11-28 16:43:09

+0

@JavaHater - 希望你恨Java少一點:) – willcodejavaforfood 2010-11-28 17:58:05

0

爲了做這樣的事情,確切的來樣,我想你可以使用NetBeans中,而不是GridBagLayout中的「免費設計」,然後限制來調整應用程序窗口的能力。

0

請勿使用GridBagLayout。嘗試一個BorderLayout。 NORTH中的標籤和CENTER中的選項卡式窗格。

或者可能是一個垂直的BoxLayout。