Java SWT ScrolledComposite在调整大小后显示内容不正确
我想要一个包含多个组合的Java SWT ScrolledComposite在调整大小后显示内容不正确,java,swt,Java,Swt,我想要一个包含多个组合的ScrolledComposite 如果调整了窗口的大小,则ScrolledComposite的大小调整不正确。如果我缩小宽度,它将削减内容的底部,不允许用户向下滚动足够多。如果我将宽度增加到更大,它将始终保持滚动条,并允许用户向下滚动太远 我尝试过更新滚动组合的最小高度,就像问题中的答案一样,但没有帮助。我试着遵循多个教程并查看示例,但我看不出是什么导致了这种情况 示例 import org.apache.commons.lang3.StringUtils; impor
ScrolledComposite
如果调整了窗口的大小,则ScrolledComposite
的大小调整不正确。如果我缩小宽度,它将削减内容的底部,不允许用户向下滚动足够多。如果我将宽度增加到更大,它将始终保持滚动条,并允许用户向下滚动太远
我尝试过更新滚动组合的最小高度,就像问题中的答案一样,但没有帮助。我试着遵循多个教程并查看示例,但我看不出是什么导致了这种情况
示例
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class Example {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Scrolled Composite Example");
// Create a scrolled composite with content
shell.setLayout(new GridLayout(1, true));
shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Composite scrollParent = new Composite(shell, SWT.NONE);
scrollParent.setLayout(new FillLayout());
scrollParent.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).span(1, 1).create());
final ScrolledComposite scrollComposite = new ScrolledComposite(scrollParent, SWT.V_SCROLL);
final Composite scrollContent = new Composite(scrollComposite, SWT.NONE);
scrollContent.setLayout(new GridLayout(1, true));
scrollComposite.setContent(scrollContent);
scrollComposite.setExpandHorizontal(true);
scrollComposite.setExpandVertical(true);
// Add a composite to the scroll content (A label with lots of text)
final Composite cell = new Composite(scrollContent, SWT.BORDER);
cell.setLayout(new GridLayout(4, false));
cell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
final Label l = new Label(cell, SWT.WRAP);
l.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
l.setText(StringUtils.repeat('1', 10000));
// Listen for width changes to update the minimum height
scrollContent.addListener(SWT.Resize, new Listener() {
int width = -1;
@Override
public void handleEvent(final Event e) {
int newWidth = scrollContent.getSize().x;
if (newWidth != width) {
scrollComposite.setMinHeight(scrollContent.computeSize(newWidth, SWT.DEFAULT).y);
width = newWidth;
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
是什么导致了这种情况?我通过添加一个侦听器来更改ScrolledComposite
的大小,从而解决了这个问题
如果调整了ScrolledComposite的大小,我将获得客户端区域的宽度,并计算内容所需的最小大小
scrolledComposite.addListener(SWT.Resize, event -> {
final int width = scrolledComposite.getClientArea().width;
scrolledComposite.setMinSize(content.computeSize(width, SWT.DEFAULT));
});
我通过添加一个监听器来更改ScrolledComposite
的大小,从而解决了这个问题
如果调整了ScrolledComposite的大小,我将获得客户端区域的宽度,并计算内容所需的最小大小
scrolledComposite.addListener(SWT.Resize, event -> {
final int width = scrolledComposite.getClientArea().width;
scrolledComposite.setMinSize(content.computeSize(width, SWT.DEFAULT));
});