gpt4 book ai didi

java - 如何防止ScrolledComposite在垂直方向无限扩展

转载 作者:行者123 更新时间:2023-11-30 11:13:15 25 4
gpt4 key购买 nike

我有一个包含以下代码的对话框。出现滚动条,但不会滚动到最后。太长了最后的内容上不来。即使将对话框重新调整为较小的大小,也看不到最后的内容。请建议如何让滚动条在以下情况下正常工作。

public class SampleDialog extends TrayDialog {

public SampleDialog(final Shell shell) {
super(shell);
this.shell = shell;

}


@Override
public void create() {
super.create();

}

@Override
protected Control createDialogArea(final Composite parent) {
final GridLayout layout = new GridLayout();
layout.numColumns = 1;
parent.setLayout(layout);

final ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER);
sc1.setExpandHorizontal(true);
sc1.setExpandVertical(true);
//sc1.setMinWidth(0);

final Composite composite = new Composite(sc1, SWT.NONE);
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
composite.setLayout(gridLayout);

sc1.setContent(composite);

//this.sc1.setMinSize(parent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
//this.sc1.setMinHeight(50);
//this.sc1.setAlwaysShowScrollBars(true);

sc1.addControlListener(new ControlAdapter() {
@Override
public void controlResized(final ControlEvent e) {
final Rectangle r = sc1.getClientArea();
sc1.setMinSize(parent.computeSize(r.width, SWT.DEFAULT));
}
});
final GridData gridDataLabel = new GridData();
final Label label = new Label(composite, SWT.NONE);
label.setText("Testing");
label.setLayoutData(gridDataLabel);
for (i = 0 ; i <= n ; i++) { // n can be 1 to any number -- decided at run time
createSampleCombo(composite);
}
}

public void createSampleCombo(Composite parent) {
final Composite composite = new Composite(parent, SWT.NONE);
final GridLayout layout = new GridLayout();
layout.numColumns = 3;
composite.setLayout(layout);

final GridData gridDataCombo = new GridData();
gridDataCombo.horizontalAlignment = GridData.FILL;
gridDataCombo.grabExcessHorizontalSpace = true;

final Combo myCombo = new Combo(composite, SWT.NONE);
myCombo.setLayoutData(gridDataCombo);
gridDataCombo.minimumWidth = 500;

final GridData gridDataButton = new GridData();
final Button browse = new Button(composite, SWT.PUSH);
browse.setText("Browse");
browse.setLayoutData(gridDataButton);
}
}

where:
org.eclipse.jface.dialogs.TrayDialog;
org.eclipse.swt.layout.GridLayout;

when n = 10 the dialog looks like this

when n = 20 the dialog, scroll bar is on top

when n = 20 the dialog, and scroll bar is pulled down

在第三张图片中,最后一个控件不可见,确定和取消按钮也不可见。我需要向下滚动条以便我可以看到所有内容。请建议如何做到这一点

最佳答案

我最近遇到了类似的问题,我是这样解决的:首先你应该防止 ScrolledComposite 垂直扩展:

sc1.setExpandVertical( false );

如果您随后将 resizeListener 更改为:

Rectangle clientArea = scrolledComposite.getClientArea();
Point minSize = scrolledComposite.getContent().computeSize( clientArea.width, SWT.DEFAULT );
scrolledComposite.getContent().setSize( minSize );

ScrolledComposite 应该只占用其容器布局允许的空间。

有关如何使用 ScrolledComposites 的更多信息,请参见此处: http://www.codeaffine.com/2016/03/01/swt-scrolledcomposite/

关于java - 如何防止ScrolledComposite在垂直方向无限扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26499120/

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