JFace中Dialog类的使用方法
2019独角兽企业重金招聘Python工程师标准>>>
cyper的例子:
注意dialog.open()返回Dialog.OK而不是SWT.OK
窗体代码:
public class ChooseIntegerDialog extends Dialog {private String[] possibleIntegerFields;/*** checkedIntegerFields is fields separated with comma*/private String checkedIntegerFields;private List<Button> buttonList = new ArrayList<Button>();public ChooseIntegerDialog(Shell parentShell, String[] possibleIntegerFields) {super(parentShell);this.possibleIntegerFields = possibleIntegerFields;}@Overrideprotected void configureShell(Shell newShell) {super.configureShell(newShell);newShell.setSize(300, 200);newShell.setLocation(450, 200);newShell.setText("Choose Integer Dialog");}@Overrideprotected Control createDialogArea(Composite parent) {parent.setLayout(new GridLayout(1, false));new Label(parent, SWT.NONE).setText("哪些字段是数值类型(不使用单引号包裹)");for (int i = 0; i < possibleIntegerFields.length; i++) {Button btn = new Button(parent, SWT.CHECK);btn.setText(possibleIntegerFields[i]);buttonList.add(btn);}return super.createDialogArea(parent);}@Overrideprotected void okPressed() {StringBuffer sb = new StringBuffer();for (Button btn : buttonList) {if (btn.getSelection()) {sb.append(btn.getText());sb.append(",");}}if (sb.length() > 0) {sb.setLength(sb.length() - 1);checkedIntegerFields = sb.toString();}super.okPressed();}public String getCheckedIntegerFields() {return checkedIntegerFields;}
}
使用代码:
String[] possibleIntegerFields = REUtil.getREGroupSet(content, "([_a-zA-Z0-9]+)\\s*(?:=|<>|!=)\\s*'[0-9]+'");String checkedIntegerFields = null;ChooseIntegerDialog integerDialog = new ChooseIntegerDialog(sw.getShell(), possibleIntegerFields);if (integerDialog.open() == Dialog.OK) {checkedIntegerFields = integerDialog.getCheckedIntegerFields();}else{return;}
以下是转载部分
近期的工作需要一个模态化的对话框,发现JFace中的Dialog正好符合我的要求,并且可以定制的方面也不少,使用起来很方便。下面是一些常用方法,因此在这里记录下来。
①设置标题栏名称
protected void configureShell(Shell newShell) { // TODO Auto-generated method stub super.configureShell(newShell); newShell.setText("Hello!");
}
②设置窗体大小
protected Point getInitialSize() { // TODO Auto-generated method stub return new Point(300,400);
}
③取消自带的OK、Cancel按钮
@Override protected void createButtonsForButtonBar(Composite parent) { // TODO Auto-generated method stub }
④定义对话框上我们需要的控件
@Override protected Control createDialogArea(Composite parent) { // TODO Auto-generated method stub Composite container = new Composite(parent, SWT.NONE); container.setBounds(0, 0, 300, 400); Button btn = new Button(container, SWT.NONE); btn.setBounds(10, 10, 55, 20); btn.setText("Click!"); // 实现自带“取消”按钮的功能 btn.addSelectionListener(new SelectionAdapter(){ @Override public void widgetSelected(SelectionEvent e) { // TODO Auto-generated method stub setReturnCode(CANCEL); close(); } }); return container; }
⑤居中对齐
这个最简单了,在创建Dialog的时候指定父窗口shell就可以了!
⑥更改Shell样式
@Override protected void setShellStyle(int newShellStyle) { // TODO Auto-generated method stub // 取消关闭“X”按钮 super.setShellStyle(newShellStyle ^ SWT.CLOSE); }
⑦自定义关闭事件
@Override
public boolean close() { // TODO Auto-generated method stub super.close(); // do something return true;
}
转载于:https://my.oschina.net/uniquejava/blog/90850
JFace中Dialog类的使用方法相关推荐
- 运行配置文件中指定类的指定方法
案例需求 通过反射运行配置文件中指定类的指定方法 代码实现 public class ReflectTest02 {public static void main(String[] args) thr ...
- Java中String类的concat方法___java的String字符串的concat()方法连接字符串和“+“连接字符串解释
Java中String类的concat方法 在了解concat()之前,首先需要明确的是String的两点特殊性. 长度不可变 值不可变 这两点从源码中对String的声明可以体现: private ...
- colsure php_PHP_PHP中Closure类的使用方法及详解,Closure,匿名函数,又称为Anonym - phpStudy...
PHP中Closure类的使用方法及详解 Closure,匿名函数,又称为Anonymous functions,是php5.3的时候引入的.匿名函数就是没有定义名字的函数.这点牢牢记住就能理解匿名函 ...
- Java 中Date类getTime()的方法
Java 中Date类getTime()的方法 1.getTime() /**Returns the number of milliseconds since January 1, 1970, 00: ...
- c++主函数中调用类内函数的方法
c++主函数中调用类内函数的方法 以基数排序为例: #include <iostream> #include <vector> using namespace std;//基数 ...
- java的character用法_Java中Character类的使用方法
Character类的使用方法 Character:字符类型 1.属性. static int MIN_RADIX :返回最小基数. static int MAX_RADIX :返回最大基数. sta ...
- c#string倒数第二位插入字符_【转载】C#中string类使用Substring方法截取字符串
在C#的字符串操作过程中,截取字符串是一种常见的字符串操作,可使用string类的Substring方法来完成字符串的截取操作,该方法支持设定截取的开始位置以及截取的字符串长度等参数,Substrin ...
- Java中Scanner类的close()方法所引发的错误
Java通过系统类System实现标准输入/输出的功能 我们经常通过实例化Scanner类的方式实现从键盘的数据输入 但其中的如果对其中一些细节不了解,就会引发不能理解的错误 比如close方法: i ...
- JAVA中Math类的random()方法使用
使用了Math类的random()方法, 由于Math类是出于java.lang包(Package),故使用时不必import这个包. 此外本例还使用了移位运算符 /*** 使用了Math类的rand ...
最新文章
- WaveSwipeRefreshLayout
- JupyterLab 3.0发布!
- (LeetCode 153)Find Minimum in Rotated Sorted Array
- linux 下使用 curl 访问带多参数,GET掉参数解决方案
- MYSQL 命令【精华帖】
- 从零开始的全栈工程师——html篇1
- mysql crash 如何导出数据库_mysql 如何做到crash后无损恢复数据的
- mysql5.5提示Deprecated: mysql_query(): The mysql extension is deprecated
- iOS AFN监听网络状态
- 使用python来构造有向随机网络
- 用户不在sudoers文件中,需要使用命令 sudo npm install 的解决方法
- Oracle中 ORA-12704:字符集不匹配
- 【vpn异地组网专题】爱快ikuai软路由-VM虚拟机安装教程
- 《从零开始的RPG游戏制作教程》第一期:制作基础场景
- 大多数计算机专业研究生的三年是怎么过的?
- 特斯拉神器TeslaMate一键安装,终于来了
- 如何撰写品牌故事?品牌故事软文撰写技巧分享
- 【渝粤教育】国家开放大学2019年春季 0777-22T市场开发与营销推广 参考试题
- R语言使用lm函数拟合线性回归模型:使用predict函数和训练好的模型进行预测推理、使用ggplot2可视化预测值和实际值的曲线进行对比分析
- c语言作业 电影院选座系统,c语言编程 电影院票务系统.wps