为Composite添加滚动条效果(ScrolledComposite)

本文讨论了在复合控件中添加滚动条时遇到的问题,并提供了正确的实现方法。重点在于设置复合控件的布局、使用ScrolledComposite类、避免直接在滚动条上添加控件、设置滚动条的扩展属性以及最小尺寸。
工作中一个任务是为一个已经有的Composite添加滚动条,原以为可以这样实现:
Composite scrollabledComposite = new Composite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
再设置一下其它的参数就可以了,谁知这样是可以添加滚动条,但是滚动条里的Composite根本不会跟着动;于是,查API,发现有ScrolledComposite这个类,好家伙,这个类里的注释连main () 方法都提供了,正点!

于是,我的代码如下:

parentComposite.setLayout(new FillLayout());
ScrolledComposite scrolledComposite = new ScrolledComposite(parentComposite, SWT.H_SCROLL|SWT.V_SCROLL);

Composite mainComposite = new Composite(scrolledComposite,SWT.NONE);
scrolledComposite.setContent(mainComposite);
mainComposite.setBackground(Display.getCurrent().getSystemColor (SWT.COLOR_WHITE));// White color
mainComposite.setLayout(new GridLayout(1,true));
GridData data = new GridData(GridData.FILL_BOTH);
mainComposite.setLayoutData(data);

Composite topComposite = new Composite(mainComposite, SWT.BORDER);
topComposite.setLayout(new GridLayout(2, false));
topComposite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));// White color

reloadBtn = new Button(topComposite, SWT.PUSH);
reloadBtn.setText("&Reload from preferences");
reloadBtn.setToolTipText("Reload values from preference page(Shift+R)");

saveBtn = new Button(topComposite, SWT.PUSH);
saveBtn.setText("&Save to preferences");
saveBtn.setToolTipText("save values to preference page(Shift+S)");

scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
scrolledComposite.setMinWidth(800);
scrolledComposite.setMinHeight(400);


总结:
1)在为Composite添加滚动条时,最上面的Composite的布局需设为FillLayout();
2) 不要直接往scrolledComposite上面添加控件;
3) 在创建完ScrolledComposite后不要忘记使用setContent()方法去设置滚动条所控制的Composite;
4) 最重要的是,Scrolledcomposite的以下四个参数必须设置才能出现滚动条:
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
scrolledComposite.setMinWidth(800);
scrolledComposite.setMinHeight(400);
只有前两项设为true之后,后面的两项才起作用。
5) 对于setMinWidth()和setMinHeight()方法,API的注释中是说用来设置滚动条出现的最小宽度和高度,但是我试了一下,有时出现滚动条了,
但是拖动滚动条还是不能显示Composite里面的全部内容,于是把setMinWidth()和setMinHeight()设大一些就可以了,个人感觉滚动条出现的
宽度和高度检测Scrolledcomposite自己已经实现了,这里的宽度和高度是指拖动滚动条里可以看到的Composite的最大宽度和最大高度。

在 SWT 中,可以使用 ScrolledComposite 控件来实现垂直滚动。但是,如果需要动态添加子组件,需要注意以下几点: 1. 在创建 ScrolledComposite 时,需要指定垂直滚动条的显示方式: ``` ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL); ``` 2. 在添加子组件时,需要设置子组件的布局数据,并将子组件添加ScrolledComposite 中: ``` Composite contentComposite = new Composite(scrolledComposite, SWT.NONE); contentComposite.setLayout(new GridLayout(1, false)); for (int i = 0; i < 10; i++) { Label label = new Label(contentComposite, SWT.NONE); label.setText("Label " + i); label.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); } scrolledComposite.setContent(contentComposite); ``` 3. 在添加完子组件后,需要调用 ScrolledComposite 的 setMinSize() 方法来设置内容区域的最小尺寸: ``` Point contentSize = contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT); scrolledComposite.setMinSize(contentSize); ``` 完整代码示例: ``` import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.graphics.Point; 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.Label; import org.eclipse.swt.widgets.Shell; public class ScrolledCompositeDemo { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(1, false)); ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL); scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); Composite contentComposite = new Composite(scrolledComposite, SWT.NONE); contentComposite.setLayout(new GridLayout(1, false)); for (int i = 0; i < 10; i++) { Label label = new Label(contentComposite, SWT.NONE); label.setText("Label " + i); label.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); } scrolledComposite.setContent(contentComposite); Point contentSize = contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT); scrolledComposite.setMinSize(contentSize); shell.setSize(200, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值