SWT ScrolledComposite滚动面板设置

本文详细阐述了在SWT中实现滚动面板时遇到的问题及解决方案,包括创建容器、设置滚动面板属性以及嵌入内容的具体步骤。通过实例代码展示如何正确配置滚动面板以显示所需的Group组件。

最近使用到SWT中的滚动面板,按照相关书籍编写的代码并不能正常显示滚动条,经查询总结如下:

要显示的内容:shell下一个滚动面板,该面板中显示一个Group组。


1.首先在shell在建立一个容器Composite,在该容器中的布局为FillLayout

Composite com = new Composite(shell,SWT.NONE);
FillLayout gl_com = new FillLayout();
com.setLayout(gl_com);
com.setVisible(false);


2.;然后建立一个滚动面板 ScrolledComposite实例sc,设置

sc.setExpandHorizontal(true);

sc.setExpandVertical(true);

这两条语句

sc下用FillLayout布局(根据你的具体布局情况决定)

ScrolledComposite sc = new ScrolledComposite(com,SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);

sc.setLayout(new FillLayout());

sc.setExpandHorizontal(true);
sc.setExpandVertical(true);


3.然后是你要嵌入到sc中的内容。这里是一个Group。

Group grpTextD = new Group(sc, SWT.NONE);

grpTextD.setText("数据");


4.最后设置

sc.setContent(grpTextD);//设置面板的有效性

sc.setMinSize(new Point(300,680));//面板的最小大小

package com.huawei.kdmpm.timeview; import com.huawei.common.util.SWTUtil; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Dialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import java.util.ArrayList; import java.util.List; public class AiRecommendDialog extends Dialog { private Shell shell; private List<String> RecommendedSentences; private List<String> SelectSentences = new ArrayList<>(); private ScrolledComposite scrolledComposite; private Object result; public AiRecommendDialog(Shell parent, List<String> RecommendedSentences) { super(parent); this.RecommendedSentences = RecommendedSentences; } /** * Open the dialog. * * @return 返回结果 */ public Object open() { createContents(); shell.open(); shell.layout(); Display displayPED = getParent().getDisplay(); while (!shell.isDisposed()) { if (!displayPED.readAndDispatch()) { displayPED.sleep(); } } return result; } /** * Create contents of the dialog. */ private void createContents() { shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); shell.setText("动作单元语句AI推荐"); shell.setSize(806, 526); SWTUtil.inst().centerShell(shell); // 创建滚动容器 scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL); scrolledComposite.setLayout(new GridLayout(1, false)); scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); // 创建滚动内容容器 Composite contentComposite = new Composite(scrolledComposite, SWT.NONE); contentComposite.setLayout(new FillLayout(SWT.VERTICAL)); // 将内容容器添加到滚动容器 scrolledComposite.setContent(contentComposite); Label recommendationLabel = new Label(contentComposite, SWT.NONE); recommendationLabel.setText("推荐语句"); // 创建全选按钮 final Button selectAllButton = new Button(contentComposite, SWT.CHECK); selectAllButton.setText("全选"); // 创建需要被控制的复选框 final List<Button> checkBoxList = new ArrayList<>(); for (int i = 0; i < 25; i++) { Button checkBox = new Button(contentComposite, SWT.CHECK); checkBox.setText("选项" + (i + 1)); checkBoxList.add(checkBox); } // 为全选按钮添加事件处理 selectAllButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { boolean isSelected = selectAllButton.getSelection(); for (Button checkBox : checkBoxList) { checkBox.setSelection(isSelected); } } }); // 为每个复选框添加事件处理 for (Button checkBox : checkBoxList) { checkBox.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { // 检查所有复选框是否都被选中 boolean allSelected = true; for (Button cb : checkBoxList) { if (!cb.getSelection()) { allSelected = false; break; } } selectAllButton.setSelection(allSelected); } }); } // 设置滚动容器的minSize scrolledComposite.setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); } public List<String> SelectSentences() { return this.SelectSentences; } } 没有显示内容
最新发布
09-18
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值