由于实际需要,在菜单中实现最近打开文件。从头到尾基本没有找到参考资料,基本是靠自己实现的。自从项目开发按照客户要求把Swing换成SWT之后,个人感觉麻烦了很多,主要是可参考资料变得很少,开发难度加大。但是另一方面,SWT做出来的东西的确比Swing漂亮,嘿嘿,闲话少说,下面介绍一下菜单的实现方法,中国自学编程网,www.zxbc.cn。 首先,在菜单的指定位置实现一个不可用的菜单项。 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,0delete 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,0delete 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). } }