gpt4 book ai didi

java - GXT (Ext-GWT) : Layout problems with ContentPanel

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:03:55 33 4
gpt4 key购买 nike

我有一个适合整个窗口的 ContentPanel。它有一个 topComponent、一个位于中心的小部件和一个 bottomComponent。

当我尝试在 ContentPanel 渲染一次后将小部件添加到 topComponent 时,我遇到了布局问题:

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 和 GXT 2.2.1。)

最佳答案

发生这种情况是因为当您将另一个组件添加到 FlowLayout 时,它会调整大小,从而增加其自身的高度,从而将底部组件推到可见区域下方。代码中没有任何内容会缩小中心组件,从而使底部组件保留在其原始位置。

还有一件事是,您正在为包含 3 个组件的 contentPanel 使用 FitLayout,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);
}
});
}

关于java - GXT (Ext-GWT) : Layout problems with ContentPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5555862/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com