一个简单Swing程序的架构

Java Swing程序不像J2EE程序那样有框架有模式可遵,反倒显得很自由无所拘束,但如些一来反而显得程序代码杂乱无章,如果只是写个示例程序的话倒也无所谓,但随着代码的增多或过段时间在回首时就发现代码很不容易看懂,不以维护。我虽无大型Swing程序的经验,但平素喜爱用Swing写写工具程序以练练手,因此我以下是我的经验总结。下面以些图来表示
一个简单Swing程序的架构_第1张图片
一个桌面级程序必然是以一个主界面展开的,在Swing中这个主界面是派生自JFrame的,其中有菜单、工具栏、状态栏、内容面板(contentPanel)和一些相应的按钮,这些界面元素即可以派生自某个类或调用Swing的现成类,但最后总要添加到界面中,而为了与用户交互,控件要和一个实现了Action接口的类绑定,因此在界面层中我分别实例化了ActionsManager和Functions对象,我专门写了一个ActionsManager类的用以管理程序中所有的Action接口的类,通过键值对集合把某个控件的动作与相应的控件绑定,当然你可能会觉得这很无聊,还不如在构造控件时直接将动作与之绑定就可以了,但这样做时很没有重复性,如果某个动作即要被按钮实现又要被菜单实现,还要被工具栏实现的话就要重复写三次代码,如果还要考虑其禁用与否就更麻烦了,因此我干脆将动作类统统封装在一个集合中统一调用,比如如下的代码
public class LoginActions { private MainPanel mainPanel; private LoginManager loginManager; private Map<String,LoginAction> mapActions; public LoginActions() { mapActions = new HashMap<String,LoginAction>(); new LoginAction("新建(N)","image/newfile.gif","image/tbnew.png",KeyEvent.VK_N,"ctrl N","new"){ public void actionPerformed(ActionEvent e) { mainPanel.newFile(); } }; new LoginAction("打开(O)...","image/open.gif","image/tbopen.png",KeyEvent.VK_O,"ctrl O","open"){ public void actionPerformed(ActionEvent e) { mainPanel.openFromFile(); } }; new LoginAction("保存(S)","image/save.gif","image/tbsave.png",KeyEvent.VK_S,"ctrl S","save"){ public void actionPerformed(ActionEvent e) { if(loginManager.isModifyFlag()) mainPanel.saveToFile(false); } }; new LoginAction("另存为(A)...",null,null,KeyEvent.VK_A,null,"saveas"){ public void actionPerformed(ActionEvent e) { if(loginManager.isModifyFlag()) mainPanel.saveToFile(true); } }; new LoginAction("导出(E)...",null,"image/tbexport.png",KeyEvent.VK_E,null,"export"){ public void actionPerformed(ActionEvent e) { mainPanel.exprotFile(); } }; new LoginAction("退出(X)",null,"image/tbexit.png",KeyEvent.VK_X,"ctrl alt X","exit"){ public void actionPerformed(ActionEvent e) { mainPanel.ExitApplication(); } }; new LoginAction("复制(C)","image/copy.gif",null,KeyEvent.VK_C,"ctrl C","copy"){ public void actionPerformed(ActionEvent e) { mainPanel.bottomPanel.editMenuCopy(); } }; new LoginAction("粘贴(P)","image/paste.gif",null,KeyEvent.VK_P,"ctrl V","paste"){ public void actionPerformed(ActionEvent e) { mainPanel.bottomPanel.editMenuPaste(); } }; new LoginAction("剪切(X)","image/cut.gif",null,KeyEvent.VK_X,"ctrl X","cut"){ public void actionPerformed(ActionEvent e) { mainPanel.bottomPanel.editMenuCut(); } }; new LoginAction("清除输入(R)",null,null,KeyEvent.VK_R,null,"reinput"){ public void actionPerformed(ActionEvent e) { mainPanel.bottomPanel.resetInput(); } }; new LoginAction("新增分类(A)",null,null,KeyEvent.VK_A,null,"addType"){ public void actionPerformed(ActionEvent e) { mainPanel.showTypeDlg(true); } }; new LoginAction("删除分类(D)",null,null,KeyEvent.VK_D,null,"delType"){ public void actionPerformed(ActionEvent e) { mainPanel.deleteType(); } }; new LoginAction("修改分类(M)",null,null,KeyEvent.VK_M,null,"modifyType"){ public void actionPerformed(ActionEvent e) { mainPanel.showTypeDlg(false); } }; new LoginAction("新增记录(D)",null,null,KeyEvent.VK_D,null,"addLogin"){ public void actionPerformed(ActionEvent e) { mainPanel.bottomPanel.addLogin(); } }; new LoginAction("删除记录(E)",null,null,KeyEvent.VK_E,null,"delLogin"){ public void actionPerformed(ActionEvent e) { mainPanel.bottomPanel.deleteLogin(); } }; new LoginAction("修改记录(O)",null,null,KeyEvent.VK_O,null,"modifyLogin"){ public void actionPerformed(ActionEvent e) { mainPanel.bottomPanel.modifyLogin(); } }; new LoginAction("查找(F)","image/tbfind.png",null,KeyEvent.VK_F,null,"findLogin"){ public void actionPerformed(ActionEvent e) { mainPanel.rightPanel.find(); } }; new LoginAction("转至地址(L)",null,null,KeyEvent.VK_L,null,"goURL"){ public void actionPerformed(ActionEvent e) { mainPanel.bottomPanel.goUrl(); } }; new LoginAction("刷新(U)","image/refresh.gif","image/tbudpate.png",KeyEvent.VK_U,null,"refresh"){ public void actionPerformed(ActionEvent e) { mainPanel.updatePanelUI(); } }; new LoginAction("程序设置(S)...","image/setup.gif","image/tbOption.png",KeyEvent.VK_S,null,"setup"){ public void actionPerformed(ActionEvent e) { mainPanel.showSetupDlg(); } }; new LoginAction("关于(A)...","image/about.gif","image/tbHelp.png",KeyEvent.VK_A,null,"about"){ public void actionPerformed(ActionEvent e) { mainPanel.showAboutDlg(); } }; } public Action getAction(String command) { return mapActions.get(command); } public void setMainPanel(MainPanel mainPanel) { this.mainPanel = mainPanel; } public void setLoginManager(LoginManager loginManager) { this.loginManager = loginManager; } abstract class LoginAction extends AbstractAction{ public LoginAction(String name,String icon,String bigIcon,int memKey,String stroke,String command) { this.putValue(NAME, name); if(icon!=null) this.putValue(Action.SMALL_ICON, new ImageIcon(this.getClass().getClassLoader().getResource(icon))); else this.putValue(Action.SMALL_ICON, new ImageIcon(this.getClass().getClassLoader().getResource("image/mnuNullIcon.gif"))); if(bigIcon!=null) this.putValue(Action.LARGE_ICON_KEY, new ImageIcon(this.getClass().getClassLoader().getResource(bigIcon))); if(memKey!=0) this.putValue(Action.MNEMONIC_KEY, memKey); if(stroke!=null) this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(stroke)); this.putValue("command", command); mapActions.put(command, this); } } }
然后在所有的控件容器类中实现相关的功能,让ActionsManager类的相应Action接口类来调用即可,而将界表示层和业务逻辑层分离已是众所皆知的道理,因此在ActionsManager内的主界面对象实例可以访问界面的UI Manager方法集,由UI Manager再调用相应的业务逻辑层,即Functions内的方法,并把结果返回至UI Manager,以此来实现动作层、表示层和业务层的简单框架。

 

你可能感兴趣的:(swing,String,manager,command,null,action)