JAVA/GUI程序之记事本
发布时间:2016-05-22 13:00:06
自上半年JAVA课程结束后,再也没有看过JAVA了,最近不是很忙,又简单的看了看,本博客纯属记录学习过程,请大神们别笑,其中错误是难免的,毕竟是新手写的博客。下面就进入我们的正题吧,复习GUI时,就想到WINDOWS的记事本,如果用GUI来仿写应该不难。实现向记事本这样的文本编辑 ...
自上半年JAVA课程结束后,再也没有看过JAVA了,最近不是很忙,又简单的看了看,本博客纯属记录学习过程,请大神们别笑,其中错误是难免的,毕竟是新手写的博客。下面就进入我们的正题吧,复习GUI时,就想到WINDOWS的记事本,如果用GUI来仿写应该不难。实现向记事本这样的文本编辑器,第一步,当然是界面的问题,这对于GUI来说再简单不过了,所以我也不多说了,直接贴上代码即可,相信都能看懂。
创建菜单代码:
1 //创建主菜单 2 public void createMenu() 3 { 4 //创建JMenuBar菜单条 5 mainMenuBar=new JMenuBar(); 6 //创建四个JMenu下拉菜单 7 fileMenu=new JMenu("文件(F)"); 8 editMenu=new JMenu("编辑(E)"); 9 formatMenu=new JMenu("格式(O)");10 viewMenu=new JMenu("查看(V)");11 helpMenu=new JMenu("帮助(H)");12 //创建JMenuItem并添加到对应的JMenu中13 mainMenuBar.add(fileMenu);14 newItem=new JMenuItem("新建");15 openItem=new JMenuItem("打开..");16 saveItem=new JMenuItem("保存..");17 saveasItem=new JMenuItem("另存为..");18 pageItem=new JMenuItem("页面设置..");19 printItem=new JMenuItem("打印..");20 exitItem=new JMenuItem("退出");21 fileMenu.add(newItem); 22 fileMenu.add(openItem);23 fileMenu.add(saveItem);24 fileMenu.add(saveasItem);25 fileMenu.addSeparator();26 fileMenu.add(pageItem);27 fileMenu.add(printItem);28 fileMenu.addSeparator();29 fileMenu.add(exitItem);30 31 mainMenuBar.add(editMenu);32 undoItem=new JMenuItem("撤消");33 cutItem=new JMenuItem("剪切");34 copyItem=new JMenuItem("复制");35 pasteItem=new JMenuItem("粘贴");36 findItem=new JMenuItem("查找..");37 replaceItem=new JMenuItem("替换..");38 selectallItem=new JMenuItem("全选");39 dateItem=new JMenuItem("时间/日期");40 editMenu.add(undoItem);41 editMenu.addSeparator();42 editMenu.add(cutItem);43 editMenu.add(copyItem);44 editMenu.add(pasteItem);45 editMenu.addSeparator();46 editMenu.add(findItem);47 editMenu.add(replaceItem);48 editMenu.addSeparator();49 editMenu.add(selectallItem);50 editMenu.add(dateItem);51 mainMenuBar.add(formatMenu);52 wrapItem=new JCheckBoxMenuItem("自动换行");53 fontItem=new JMenuItem("设置字体..");54 formatMenu.add(wrapItem);55 formatMenu.add(fontItem);56 mainMenuBar.add(viewMenu);57 mainMenuBar.add(helpMenu);58 helpItem=new JMenuItem("查看帮助(H)");59 aboutItem=new JMenuItem("关于记事本..(A)");60 helpMenu.add(helpItem);61 helpMenu.add(aboutItem);62 //为每个菜单项添加**63 exitItem.addActionListener(this);64 saveItem.addActionListener(this);65 saveasItem.addActionListener(this);66 newItem.addActionListener(this);67 printItem.addActionListener(this);68 openItem.addActionListener(this);69 cutItem.addActionListener(this);70 copyItem.addActionListener(this);71 pasteItem.addActionListener(this);72 selectallItem.addActionListener(this);73 dateItem.addActionListener(this);74 wrapItem.addActionListener(this);75 findItem.addActionListener(this);76 fontItem.addActionListener(this);77 helpItem.addActionListener(this);78 aboutItem.addActionListener(this);79 }
原标题:JAVA/GUI程序之记事本
关键词:JAVA
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。