將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");
}
}
屏幕截圖在這裏會有所幫助。 –