|
人才网-提供最新的人才网信息,人才信息查询,人才网招聘服务
|
|
|
SWT在菜单中实现最近打开文件列表 |
|
|
这两天一直在做一个功能,在菜单中实现最近打开文件。从头到尾基本没有找到参考资料,基本是靠自己实现的。自从项目开发按照客户要求把Swing换成SWT之后,个人感觉麻烦了很多,主要是可参考资料变得很少,开发难度加大。但是另一方面,SWT做出来的东西的确比Swing漂亮,嘿嘿,闲话少说,下面介绍一下菜单的实现方法。 首先,在菜单的指定位置实现一个不可用的菜单项。 public void creatFileMenuItemUsedFile(){ pushFileUsedFile[0]= new MenuItem(subFile, SWT.PUSH); pushFileUsedFile[0].setEnabled(false); pushFileUsedFile[0].setText(WindowString.strMenuFileUsedFiles); //这里是在别的类中定义的菜单名 } 接着,实现需要保存路径的函数 /** * save the file path in every element which need to save * @param filePath * @throws N/A * @author tianrenliang * @since v0.1(2007/11/07) */ private void getFilePath(String filePath){ //if recent file not exist if(!(pathSave.contains(filePath))) { //If path number less than rule needed if(pathSave.size() { pathSave.add(filePath); } /*If path number bigger than rule needed,delete the first element in array list*/ else { // Delete the first position,the second change to the first pathSave.remove(0); pathSave.add(filePath); } } // if recent file exist,delete the file and add this file to the last else { pathSave.remove(pathSave.indexOf(filePath)); pathSave.add(filePath); } // convert the vector to string array for(int i =0; i { path[i] = (String)pathSave.get(i); } } 接着,实现设置历史文件的菜单项的函数 private void setUsedFileItem() { if(pushFileUsedFile[0].getText()==WindowString.strMenuFileSaveAll) { pushFileUsedFile[0].dispose(); } else { // do nothing } for(int i =0; i { if(pushFileUsedFile[i]==null) { pushFileUsedFile[i] = new MenuItem(subFile, SWT.PUSH,12); pushFileUsedFile[i].setText(path[i]); pushFileUsedFile[i].setEnabled(true); } else { pushFileUsedFile[i].setEnabled(true); pushFileUsedFile[i].setText(path[i]); } } } 最后,将这两个函数添加到需要保存文件路径的事件监听中即可。 因为程序关闭后,历史文件的纪录将随着内存释放而消失,所以如果想实现菜单打开后保存上次程序使用中的路径,只需将字符串数组中的内容导入XML文件中,将菜单设置中的数组读取部分从XML中读取,即可保存历史文件。 |
| 人才网http://www.job128.com 信息部 |