SWT控件的computeSize方法

本文介绍了Eclipse SWT中Control.computeSize方法的工作原理。该方法可通过指定宽高参数来计算控件所需尺寸,当参数设置为SWT.DEFAULT时,控件会自动计算适合的尺寸。

/**

 * 用于SWT控件自动计算自身所需的尺寸

 */

Point org.eclipse.swt.widgets.Control.computeSize(int wHint, int hHint)

 

两个参数很有意思,分别是指定一个宽和一个高的值。这两个参数有什么用呢?一般我们都是赋予默认值SWT.DEFAULT就可以得到控件所需的尺寸了。但如果将其变为一个固定的整数,那么,嘿嘿,他的输出便是你给定的那个值。

 

还不明白?

 

例如:

        computeSize(10, 300),那么得到控件的尺寸就是(10, 300)

        computeSize(SWT.DEFAULT, SWT.DEFAULT),那么控件将通过计算得到合适的尺寸

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.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); shell.setLayout(new GridLayout(1, false)); // 为shell设置布局 // 创建滚动容器 scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.BORDER); scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); scrolledComposite.setExpandHorizontal(true); scrolledComposite.setExpandVertical(true); // 创建滚动内容容器 Composite contentComposite = new Composite(scrolledComposite, SWT.NONE); // 为内容容器设置布局:垂直排列的GridLayout,一列 GridLayout contentLayout = new GridLayout(1, false); contentComposite.setLayout(contentLayout); // 将内容容器添加到滚动容器 scrolledComposite.setContent(contentComposite); // 推荐语句标签 Label recommendationLabel = new Label(contentComposite, SWT.NONE); recommendationLabel.setText("推荐语句"); recommendationLabel.setBounds(10, 10, 100, 30); // 全选按钮 final Button selectAllButton = new Button(contentComposite, SWT.CHECK); selectAllButton.setText("全选"); selectAllButton.setBounds(50, 10, 100, 30); // 复选框列表 final List<Button> checkBoxList = new ArrayList<>(); // 使用RecommendedSentences创建复选框 if (RecommendedSentences != null) { for (String sentence : RecommendedSentences) { Button checkBox = new Button(contentComposite, SWT.CHECK); checkBox.setText(sentence); checkBox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); checkBoxList.add(checkBox); } } for (int i = 0; i < 25; i++) { Button checkBox = new Button(contentComposite, SWT.CHECK); checkBox.setText("选项" + (i + 1)); checkBox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); 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); } }); } // 设置内容容器的大小(计算所有控件的大小) contentComposite.pack(); // 设置滚动容器的最小大小 scrolledComposite.setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); } public List<String> SelectSentences() { return this.SelectSentences; } } 将“推荐语句”跟全选按钮放在同一列
最新发布
09-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值