2016-12-14 67 views
1

將FlowLayout容器放入寬度約束父容器中會產生意外的結果。寬度受限的FlowLayout容器的意外高度

應該是預期的結果?

我期望FlowLayout容器如此適合他們的內容。

運行該代碼,點擊按鈕:

public class FormFlowLayoutInTableLayout extends Form { 
    public FormFlowLayoutInTableLayout() { 
     setTitle("FormFlowLayoutInTableLayout"); 
     setScrollable(false); 
     getContentPane().setLayout(new BorderLayout()); 
     Button buttonBorderLayout = new Button("BorderLayout"); 
     Button buttonTableLayout = new Button("TableLayout"); 
     Component containerSouth = Container.encloseIn(
       new BoxLayout(BoxLayout.X_AXIS), 
       buttonBorderLayout, 
       buttonTableLayout); 
     getContentPane().add(BorderLayout.SOUTH, containerSouth); 
     Container containerCenter = new Container(new BoxLayout(BoxLayout.Y_AXIS)); 
     containerCenter.setScrollableY(true); 
     getContentPane().add(BorderLayout.CENTER, containerCenter); 
     ActionListener<?> actionListenerRefreshContainerUsingBorderLayout = (e) -> { 
      containerCenter.removeAll(); 
      for (int tally = 0; tally < 10; tally++) { 
       Component container = createRowContainerUsingBorderLayout(); 
       container.setUIID("ListRenderer"); 
       containerCenter.add(container); 
      } 
      containerCenter.revalidate(); 
     }; 
     ActionListener<?> actionListenerRefreshContainerUsingTableLayout = (e) -> { 
      containerCenter.removeAll(); 
      for (int tally = 0; tally < 10; tally++) { 
       Component container = createRowContainerUsingTableLayout(); 
       container.setUIID("ListRenderer"); 
       containerCenter.add(container); 
      } 
      containerCenter.revalidate(); 
     }; 
     buttonBorderLayout.addActionListener(actionListenerRefreshContainerUsingBorderLayout); 
     buttonTableLayout.addActionListener(actionListenerRefreshContainerUsingTableLayout); 
     Display.getInstance().callSerially(() -> actionListenerRefreshContainerUsingTableLayout.actionPerformed(null)); 
    } 

    private Component createRowContainerUsingBorderLayout() { 
     Container container = new Container(new BorderLayout()); 
     container.add(BorderLayout.CENTER, createSomeLargeFlowLayoutedContainer()); 
     Container containerRight = new Container(new BoxLayout(BoxLayout.Y_AXIS)); 
     containerRight.add(createSomeSmallUpperLabel()); 
     containerRight.add(createSomeSmallLowerLabel()); 
     container.add(BorderLayout.EAST, containerRight); 
     return container; 
    } 

    private Component createRowContainerUsingTableLayout() { 
     TableLayout tableLayout = new TableLayout(2, 2); 
     Container container = new Container(tableLayout); 
     container.add(tableLayout.createConstraint(0, 0).verticalSpan(2).widthPercentage(60), createSomeLargeFlowLayoutedContainer()); 
     container.add(tableLayout.createConstraint(0, 1).horizontalAlign(Component.RIGHT), createSomeSmallUpperLabel()); 
     container.add(tableLayout.createConstraint(1, 1).horizontalAlign(Component.RIGHT), createSomeSmallLowerLabel()); 
     return container; 
    } 

    private Container createSomeLargeFlowLayoutedContainer() { 
     return Container.encloseIn(
       new FlowLayout(), 
       new SpanLabel("The quick brown fox jumps over the lazy dog"), 
       new SpanLabel("The quick brown fox jumps over the lazy dog")); 
    } 

    private Label createSomeSmallUpperLabel() { 
     return new Label("SmallUpper"); 
    } 

    private Label createSomeSmallLowerLabel() { 
     return new Label("SmallLower"); 
    } 
} 
+0

屏幕截圖在這裏會有所幫助。 –

回答

0

FlowLayout斷裂線這是爲什麼我們建議避免它尤其是組件重新計算尺寸例如原因之一當您使用流佈局,我們不知道最終的佈局將如何看待SpanLabelTextArea

與非確定性佈局所面臨的挑戰進行了討論在一定程度上https://www.codenameone.com/blog/the-challenge-of-multiline-strings.html

,並且造成了不確定性,這導致產生不希望的副作用。

+0

使用標籤代替SpanLabel時,情況更糟。 那麼你說什麼 - Codename One的默認佈局是FlowLayout,並且這不起作用? –

+0

'新的BoxLayout(BoxLayout.Y_AXIS)'很好地處理了幾個FlowLayout容器。 BorderLayout和TableLayout是否無法處理包含FlowLayoutcontainers? –

+0

https://github.com/codenameone/CodenameOne/issues/1986 –

相關問題