宽度受限的FlowLayout-Container的意外高度

时间:2016-12-14 14:41:35

标签: codenameone

将FlowLayout-Container放入宽度约束父容器中会产生意外结果。

预期结果应该是什么?

我希望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");
    }
}

1 个答案:

答案 0 :(得分:0)

FlowLayout打破行,这是我们建议避免使用行的原因之一,特别是对于重新计算大小的组件,例如SpanLabelTextArea等。

https://www.codenameone.com/blog/the-challenge-of-multiline-strings.html

在一定程度上讨论了与非确定性布局相关的挑战

当您使用流程布局时,我们不知道最终布局将如何显示,并产生不确定性,从而导致不良副作用。