2011-04-05 78 views
5

我有一個適合整個窗口的ContentPanel。它有一個topComponent,一個位於中心的小部件和一個bottomComponent。GXT(Ext-GWT):ContentPanel的佈局問題

我越來越佈局的問題,當我嘗試小部件添加到的TopComponent後的ContentPanel已經呈現一次:

public void onModuleLoad() { 

    final Viewport viewport = new Viewport(); 
    viewport.setLayout(new FitLayout()); 

    final ContentPanel contentPanel = new ContentPanel(new FitLayout()); 
    contentPanel.setHeaderVisible(false); 

    final LayoutContainer topContainer = new LayoutContainer(
      new FlowLayout()); 

    final Button buttonOne = new Button("Top:One"); 
    topContainer.add(buttonOne); 

    contentPanel.setTopComponent(topContainer); 
    contentPanel.add(new Button("Center")); 
    contentPanel.setBottomComponent(new Button("Bottom")); 

    viewport.add(contentPanel); 
    RootPanel.get().add(viewport); 

    // Later, add a second button to the topComponent ... 
    Scheduler.get().scheduleDeferred(new ScheduledCommand() { 
     @Override 
     public void execute() { 
      final Button buttonTwo = new Button("Top:Two"); 
      topContainer.add(buttonTwo); // Doesn't show up at first. 

      topContainer.layout(); // Now, buttonTwo shows up. But we have 
          // a new problem: the "Bottom" button disappears... 

      contentPanel.layout(true); // This doesn't do anything, BTW. 
     } 
    }); 
} 

關於這個有趣的是,該佈局將自行解決,儘快當我調整瀏覽器窗口。我可以做些什麼來使它重新正確地立即佈局(我試過在幾個地方和組合中添加幾個layout()等等,但是到目前爲止還沒有任何運氣。)

(我正在使用GWT 2.1.1與2.2.1 GXT)

+0

什麼是「越來越佈局的問題」是什麼意思? – 2011-04-08 15:09:03

+0

@ Travis:像我的代碼中的評論一樣:首先buttonTwo不顯示。然後,當我調用layout()時,它顯示出來。但是現在「底部」按鈕消失。只要手動調整窗口大小(即使只是一個像素),佈局也會自行修正,即所有按鈕都顯示出來。 – 2011-04-08 17:50:01

回答

7

發生這種情況,因爲當您將另一個組件添加到FlowLayout時,它會調整大小,從而增加其自身的高度,從而將底部組件推到可見區域下方。代碼中沒有縮小中間組件,使底部組件停留在原來的位置。

還有一件事是,您正在使用FitLayout for contentPanel,其中包含3個組件,FitLayout僅用於容器中只有一個組件,它應該填充其父項。

你需要考慮以下幾點:

1)使用的RowLayout,它允許用戶對如何組件一個更好的控制應當設置

2)決定對其中的成分,你願意把垂直滾動條,因爲您正在動態添加組件。

您當前需求下面的代碼就足夠了:

public void onModuleLoad() { 
     final Viewport viewport = new Viewport(); 
     viewport.setLayout(new FitLayout()); 

//  final ContentPanel contentPanel = new ContentPanel(new FlowLayout()); 
//  contentPanel.setHeaderVisible(false); 

     final LayoutContainer topContainer = new LayoutContainer(
       new FlowLayout()); 

     final Button buttonOne = new Button("Top:One"); 
     topContainer.add(buttonOne); 
// contentPanel.setTopComponent(topContainer); 
     final LayoutContainer centerPanel = new LayoutContainer(new FitLayout()); 

     centerPanel.add(new Button("Center")); 
// contentPanel.add(centerPanel); 
     final LayoutContainer botPanel = new LayoutContainer(new FlowLayout()); 
     botPanel.add(new Button("Bottom")); 
//  contentPanel.setBottomComponent(botPanel); 

     final ContentPanel panel = new ContentPanel(); 
     panel.setHeaderVisible(false); 
     panel.setLayout(new RowLayout(Orientation.VERTICAL));  


     panel.add(topContainer, new RowData(1, -1, new Margins(4))); 
     panel.add(centerPanel, new RowData(1, 1, new Margins(0, 4, 0, 4))); 
     panel.add(botPanel, new RowData(1, -1, new Margins(4))); 

     viewport.add(panel, new FlowData(10)); 


     RootPanel.get().add(viewport); 

     // Later, add a second button to the topComponent ... 
     Scheduler.get().scheduleDeferred(new ScheduledCommand() { 
      @Override 
      public void execute() { 
       final Button buttonTwo = new Button("Top:Two"); 
       topContainer.add(buttonTwo); // Doesn't show up at first. 
       panel.layout(true); 
      } 
     }); 
} 
+0

我的contentPanel中的FitLayout只有一個孩子:中心按鈕。 「topComponent」和「bottomComponent」是分開的。那麼,你改變了代碼,根本不使用「setTopComponent」和「setBottomComponent」 - 並且這樣做,因爲它不使用ContentPanel的topComponent/bottomComponent特性。 (我爲解決這個問題而實施的臨時解決方案與您的解決方案類似)。但是,我確實想使用topComponent/bottomComponent特性,因爲它在我正在處理的項目的現有代碼中的其他地方使用 - 而且佈局也應該與它一起工作。 – 2011-04-11 11:40:40

+0

在現有的代碼中,您是否動態地將小部件添加到頂部或底部組件?我問這是因爲在contentPanel被渲染後,頂部和底部容器不參與佈局。當您手動調整瀏覽器窗口的大小時,會調用onResize並調整高度。有一件事你可以嘗試,如果你堅持使用頂部和底部組件,就是通過動態獲取高度和寬度來調用面板上的設置大小方法。 – Swapnil 2011-04-11 16:51:45

+0

@Swapnil:在現有的代碼中,到目前爲止,向頂部/底部組件添加小部件並不是必需的。但GXT文檔並沒有說,你不能在那裏動態地添加小部件,所以假設這是可行的。現在,通過我們的解決方法(即沒有頂部/底部組件),樣式有點不同(加上一些其他小問題)。原始樣式可以手動模仿,但這不是一個很乾淨的解決方案。 /我只是嘗試'contentPanel.setSize(contentPanel.getWidth(),contentPanel.getHeight());' - 不幸的工作。任何其他想法? – 2011-04-11 17:23:06

0

一旦瀏覽器完成渲染窗口小部件就不會呈現該組件再這樣做了一遍,我們可以使用 contentPanel.layout() 或 contentPanel.layout(真) 它按照新佈局重新呈現小部件。

+0

不幸的是,在我的情況下,'contentPanel.layout()'和'contentPanel.layout(true)'什麼也不做。 – 2011-04-06 07:41:21

1

發生這種情況,因爲在這條語句被調用的topComponentonRender事件:

contentPanel.setTopComponent(topContainer); 

然後,以後,要添加一個組件:topContainer.add(buttonTwo);。當您調整窗口大小時,將觸發onRender事件,並顯示您添加的按鈕。解決您的問題涉及觸發onRender事件topContainer

不幸的是,onRender是受保護的,所以你不能直接調用它: http://dev.sencha.com/deploy/gxtdocs/com/extjs/gxt/ui/client/widget/LayoutContainer.html#onRender%28com.google.gwt.user.client.Element,%20int%29

你將要使用fireEvent來觸發這個程序,而不是手動調整窗口大小: http://dev.sencha.com/deploy/gxtdocs/com/extjs/gxt/ui/client/widget/Component.html#fireEvent%28com.extjs.gxt.ui.client.event.EventType%29

+0

我該如何開展活動?我在contentPanel和topContainer上都嘗試過'fireEvent(Events.Render)'和'fireEvent(Events.Resize)'。我也在contentPanel.getBottomComponent()上試過它。到目前爲止沒有工作。 – 2011-04-08 19:45:54