2012-11-06 161 views
0

我遇到了GridBagLayouts的問題。Java - 嵌套GridBagLayout佈局管理器

我有佈置幾個部件顯示在下面的圖:

enter image description here

我想「中心」組分,以使其能夠移動的「右上」如果底部邊緣上方「頂部」組件縮小得足夠小。但是,這顯然會導致問題,因爲它必須位於網格的不同行才能夠這樣做。

爲了解決這個問題 - 我將整個右列定義爲自己的容器,並帶有自己的佈局管理器。我希望這兩列能夠獨立行事,但我遇到同樣的問題!我看不出兩位佈局經理可能如何互動。任何人都可以解釋我的問題是什麼?

下面是相關的代碼(在我的課擴展小程序):

public void init(){ 
    addComponents(); 
} 

private void addComponents(){ 
    setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 

    Left left = new Left(); 
    Top top = new Top(); 
    Center center = new Center(); 
    TopRight topRight = new TopRight(); 
    BottomRight bottomRight = new BottomRight(); 
    Bottom bottom = new Bottom(); 

    topRight.setPreferredSize(new Dimension(200,200)); 
    top.setPreferredSize(new Dimension(0,200)); 
    center.setPreferredSize(new Dimension(0,100)); 
    container.setPreferredSize(new Dimension(200,200)); 

    c.fill = GridBagConstraints.BOTH; 

    c.gridx = 0; c.gridy = 0; 
    c.gridwidth = 1; c.gridheight = 3; 
    c.weightx = 0.2; c.weighty = 1.0; 
    this.add(left,c); 

    c.gridx = 1; c.gridy = 0; 
    c.gridwidth = 1; c.gridheight = 1; 
    c.weightx = 0.8; c.weighty = 0.8; 
    this.add(top,c); 

    c.gridx = 1; c.gridy = 1; 
    c.gridwidth = 1; c.gridheight = 1; 
    c.weightx = 0.8; c.weighty = 0.0; 
    this.add(center,c); 

    c.gridx = 1; c.gridy = 2; 
    c.gridwidth = 1; c.gridheight = 1; 
    c.weightx = 0.8; c.weighty = 0.2; 
    this.add(bottom,c); 

    JPanel container = new JPanel(); 
    container.setLayout(new GridBagLayout()); 

    c.gridx = 2; c.gridy = 0; 
    c.gridwidth = 1; c.gridheight = 3; 
    c.weightx = 0.0; c.weighty = 1.0; 
    this.add(container,c); 

    GridBagConstraints c2 = new GridBagConstraints(); 
    c2.fill = GridBagConstraints.BOTH; 

    c2.gridx = 0; c2.gridy = 0; 
    c2.gridwidth = 1; c2.gridheight = 1; 
    c2.weightx = 0.0; c2.weighty = 0.0; 
    container.add(topRight,c2); 

    c2.gridx = 0; c2.gridy = 1; 
    c2.gridwidth = 1; c2.gridheight = 1; 
    c2.weightx = 0.0; c2.weighty = 1.0; 
    container.add(bottomRight,c2); 
} 

注:每個所示(左,上,等等)的組件是JPanel的一個擴展 - 每個都有油漆(圖形g)方法重寫以填充某種顏色。

在此先感謝,併爲長期讀對不起,

喬納森

+0

要澄清邊界交叉時會發生什麼 - 右上角的組件會突然切換大小(遠低於其首選大小),以便中心組件可以繼續向上移動。直到中心組件移回超過右上角的首選高度後,它纔是新的尺寸。 – Porthos3

回答

0

我解決了這一問題。它和我有些事情需要不必要地設置Top組件的首選高度 - 當我刪除那條線時,它就像我想要的那樣工作。