容器composite、shell

本文介绍了SWT中的Composite容器类及其常用方法,如设置背景颜色、背景图片及光标等。同时深入探讨了Shell窗口的不同样式,包括最大化、最小化、关闭等按钮配置,并解析了Shell的模态属性及事件监听。
Composite:最基本的容器类,可以嵌套Composite。
可以将基本控件用Composite组合起来形成自定义的复合控件,方便重复使用。(扩展Composite,生产子类 )。
composite.setBackground():设置容器背景色
composite.setbackgroundImage():设置背景图片
setBackgroundMode()设置背景模式,SWT.Inherite_none(指定子控件不继承背景色设置),SWT.Inherite_default(只有也指定了default的子控件才会继承),SWT.Inherite_force(强制继承),
setCursor();设置光标
Cursor cursor=new Cursor(display,SWT>CURSOR_CROSS);
composite.setcursor(cursor);
...
cursor.dispose();
图片鼠标:Image得到ImageData,然后cursor=new Cursor(display,imageData,0,0),最后两个参数指定了图片上箭头端点位置。

Shell样式:SHELL_TRIM(默认)样式有最大最小关闭按钮,可调尺寸,由CLOSE\TITLE\MIN\MAX\RESIZE\Border组成。resize样式没有标题栏,可调尺寸,close样式只有关闭按钮,不能调尺寸。ON_Top保证最前端,No_trim去掉所有修饰,

Shell的模态:MODELESS,PRIMARY_MODEL,APPLICATION_MODEL,SYSTEM_MODEL。modeless不对任何消息进行阻截,primary_model子窗口阻断所有发送到其父窗口的消息,若不关掉子窗口则无法对父窗口操作,application_mode子窗口阻断与其共享一个display的窗口,若为单display则实际阻断了整个应用程序,system_model阻断所有桌面应用程序,最好不要用。

shell支持5种事件:activate(成为活动窗口时),close,deactivate(变成非活动),deiconify(从最小化恢复),iconify(最小化),采用ShellListener来监听所有上述事件。

为容器添加上下文菜单
Menu menu=new Menu(shell, SWT.POP_UP);
container.setMenu(menu);



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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值