Java写字板程序设计.doc_第1页
Java写字板程序设计.doc_第2页
Java写字板程序设计.doc_第3页
Java写字板程序设计.doc_第4页
Java写字板程序设计.doc_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

华北科技学院计算机系综合性实验报告华北科技学院计算机系综合性实验实 验 报 告 课程名称 Java程序设计 实验学期 2011 至 2012 学年 第 1 学期学生所在系部 基础部 年级 2009 专业班级 计算B092 学生姓名 李 宁 学号 200909014217 任课教师 李芙玲 实验成绩 计算机系制 Java程序设计课程综合性实验报告开课实验室: 基础实验室 2011 年 12 月 5 日实验题目简易写字板软件设计一、实验目的通过编写Java的应用系统综合实例简易写字板软件,总结、回顾和实践面向对象的编程思想以及编程方法,并通过编写程序来掌握Java语言编程技巧,将学习到的知识融会贯通,同时提高调试程序的能力,养成良好的编程习惯,并增强对程序设计整体思路的把握。在实践中感受、学习Java语言,提高Java应用能力。熟悉在集成开发环境下编写Java程序。二、设备与环境 硬件:多媒体计算机 软件:Windows 2000以上的操作系统、JDK开发包、集成开发环境、MyEclipse9.0三、实验内容总体要求:程序功能基本完成,用户界面友好,代码的可维护性和可扩展性较好,最好包括必要的注释和相应的文档。具体要求:基本的文本录入,字符修改,删除,换行,粘贴,复制,剪切等功能,主要包括:1 菜单栏设计及功能实现:菜单栏主要包括文件、编辑、格式、帮助等常见菜单。其中文件菜单有新建、打开、保存、另存为、退出等功能;编辑菜单有剪切、复制、粘贴、等功能;格式菜单含有对文字字体、字型、字号及颜色等的设置功能,根据情况可在编辑菜单添加查找和查找替换功能。2 编辑区设计:对文本的编辑处理区。3 工具栏设计:可根据需要增加该项功能。4 其他功能设计。四、实验结果及分析1主界面的设计与实现整个界面共包括菜单栏、工具栏、编辑区和状态栏4部分,如图1.1所示。图1.1 界面全局 菜单栏的设计本程序共设计5个菜单,分别是文件、编辑、格式、查看和帮助,其中文件菜单实现了新建、打开、保存、另存为和退出功能,编辑菜单实现了撤销、重做、剪切、复制、粘贴、查找、替换、全选和插入时间的功能,格式菜单实现了字体及字体颜色的设置,查看菜单实现了查看工具栏及状态栏的功能,帮助菜单实现了查看软件信息以及作者信息的功能。各个菜单的功能见图1.2。图1.2 各个菜单功能 工具栏设计工具栏实现了新建、保存、对齐方式设置、字形设置、字体设置以及字体大小设置。右击工具栏可设置工具栏是否可拖动。如图1.3所示。图1.3 工具栏 编辑区设计编辑区实现的文本录入、自动换行和右击菜单功能。如图1.4所示。图1.4 编辑区 状态栏设计状态栏实现了显示操作系统当前时间的功能。如图1.5所示。图1.5 状态栏2文件菜单中各项功能的实现 文件菜单包括新建、打开、保存、另存为和退出5部分,如图 所示。新建功能设计新建实现了新建文件功能,当前文档不为空时提示是否保存当前文档,选择“是”进行保存当前文本,选择“否”或“取消”进行取消。如图2.1和2.2所示。图2.1新建保存提示图2.2新建保存打开功能设计打开实现了在打开本地文件的功能,选择本地文件并打开在当前窗口。如图2.3所示。 图2.3 打开文档界面保存功能设计保存实现了在当前路径下保存当前文档,若无路径则添加路径。如图2.4所示。图2.4 保存界面另存为功能设计另存为实现了在新路径下保存当前文档的功能。如图2.5所示。图2.5 文档另存为退出功能设计退出实现了用户退出是提醒用户是否确定要退出。如图1.11所示。图2.5 退出提示3编辑菜单中各项功能的实现编辑菜单包括撤销、恢复、剪切、复制、粘贴、查找、替换、全选、日期时间9部分,如图 3.1所示。图3.1 编辑菜单 撤销功能设计撤销实现了返回上一步的操作,当撤销到最初状态时提示无法撤销。如图3.2所示。图3.2 撤销提示 恢复功能设计恢复实现了返回撤销之前的一步,当恢复到最终状态时提示无法恢复。如图3.3所示。图3.3 恢复提示 剪切功能设计剪切实现了对所选文本的剪切。 复制功能设计复制实现了对所选文本的复制。 粘贴功能设计粘贴实现了对当前文档剪贴板的粘贴。 查找功能设计查找实现了在当前文档中查找字符的操作。如图3.4所示。图3.4 查找界面 替换功能设计 替换实现了对当前文档中字符的替换操作。如图3.5所示。图3.5 替换界面 全选功能设计全选实现了对当前文本的全部选择。如图3.6所示。图3.6 全选界面 插入时间功能设计 插入时间实现了在文本当前位子插入系统时间的功能。如图3.7所示。图3.7 插入系统时间4格式菜单中各项功能的实现格式菜单包括设置字体、字体颜色2部分,如图4.1所示。图4.1 格式菜单 字体功能设计字体实现了对文本区字体以及字号的设置,并通过预览窗口显示效果,单击确定进行设置,单击取消进行退出字体设置。若文本为空,提示“您的文本中还没有内容,请输入内容后重新设置!”如图4.2和4.3所示。图4.2 字体设置预览图4.3 空文档字体设置提示 字体颜色功能设计字体颜色实现了最当前文本字体的颜色设置。如图4.4图4.4 字体颜色设置界面5查看菜单中各项功能的实现查看菜单包括查看工具栏和状态栏2部分,如图5.1所示图5.1 查看菜单 “工具栏”功能设计“工具栏”实现了设置工具栏的可见性。如图5.2和5.3所示。图5.2 设置工具栏可见图5.3 设置工具栏不可见 “状态栏”功能设计“状态栏”实现了设置状态栏的可见性。如图5.4和5.5所示。图5.4 设置状态栏可见图5.5 设置状态栏不可见6帮助菜单中各项功能的实现帮助菜单包括查看“关于记事本”和“关于作者”2部分,如图6.1所示。图6.1 帮助菜单 “关于记事本”功能设计 “关于记事本”实现了显示记事本信息。如图6.2所示。图6.2关于记事本“关于作者”功能设计 “关于作者”实现了显示记事本作者的信息。如图6.3所示。图6.3关于作者7.主要代码解释主窗口: NoteBook() setTitle(简易写字板);setSize(850, 500);setLocation(240, 160);setVisible(true);content = new JTextPane();content.setBackground(new Color(15, 227, 240);add(new JScrollPane(content), BorderLayout.CENTER);myUndo = new MyUndoManager();content.getDocument().addUndoableEditListener(myUndo);validate();setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) if (JOptionPane.showConfirmDialog(new Frame(),你确定退出吗?,退出提示, JOptionPane.WARNING_MESSAGE) = JOptionPane.OK_OPTION)System.exit(0););getJMenuBar();JPopupMenu();getToolBar();菜单栏及功能实现:创建菜单栏并添加文件、编辑、格式、查看、帮助菜单和它们的子菜单,再添加对应的快捷键,图片以及监听器。/菜单栏及其功能实现public JMenuBar getJMenuBar()if(jmenuBar = null)JMenuBar Jmenu = new JMenuBar();setJMenuBar(Jmenu);JMenu filemenu = new JMenu(文件(F);JMenu editmenu = new JMenu(编辑(E);JMenu formatmenu = new JMenu(格式(O);JMenu viewmenu = new JMenu(查看(V);JMenu helpmenu = new JMenu(帮助(H);Jmenu.add(filemenu);Jmenu.add(editmenu);Jmenu.add(formatmenu);Jmenu.add(viewmenu);Jmenu.add(helpmenu);Jmenu.setBackground(new Color(100,240,60);/创建文件菜单上的各个菜单项并添加到菜单上JMenuItem newitem = new JMenuItem(新建(N);JMenuItem openitem = new JMenuItem(打开(O);JMenuItem saveitem = new JMenuItem(保存(S);JMenuItem saveasitem = new JMenuItem(另存为(A);JMenuItem exititem = new JMenuItem(退出(X);newitem.setIcon(new ImageIcon(icons/new.jpg);openitem.setIcon(new ImageIcon(icons/open.jpg);saveitem.setIcon(new ImageIcon(icons/save.jpg);saveasitem.setIcon(new ImageIcon(icons/lingcun.jpg);exititem.setIcon(new ImageIcon(icons/close.jpg);newitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK);openitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK);saveitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK);exititem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK);filemenu.add(newitem);filemenu.add(openitem);filemenu.add(saveitem);filemenu.add(saveasitem);filemenu.addSeparator();filemenu.add(exititem);/新建菜单项事件newitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)Action.newFile(); );/打开菜单项事件openitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) Action.openFile(); );/保存菜单项事件saveitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) Action.saveFile(););/另存为菜单项事件saveasitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) Action.saveasFile(););/退出菜单项的功能实现exititem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) if(JOptionPane.showConfirmDialog(new Frame(),你确定退出吗?,退出提示,JOptionPane.WARNING_MESSAGE)=JOptionPane.OK_OPTION)System.exit(0););/创建编辑菜单上的各个菜单项并添加到菜单上JMenuItem undoitem = new JMenuItem(撤销(U);JMenuItem redoitem = new JMenuItem(重做(R);JMenuItem cutitem = new JMenuItem(剪切(T); JMenuItem copyitem = new JMenuItem(复制(C);JMenuItem pasteitem = new JMenuItem(粘贴(P);JMenuItem finditem = new JMenuItem(查找(F);JMenuItem replaceitem = new JMenuItem(替换(R);JMenuItem allitem = new JMenuItem(全选(A));JMenuItem dateitem = new JMenuItem(日期/时间(D);undoitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK);undoitem.setIcon(new ImageIcon(icons/undo.jpg);redoitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y,InputEvent.CTRL_MASK);redoitem.setIcon(new ImageIcon(icons/redo.jpg);cutitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK);cutitem.setIcon(new ImageIcon(icons/cut.jpg);copyitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK);copyitem.setIcon(new ImageIcon(icons/copy.jpg);pasteitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK);pasteitem.setIcon(new ImageIcon(icons/paste.jpg);finditem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3,InputEvent.CTRL_MASK);replaceitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK);allitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK);dateitem.setIcon(new ImageIcon(icons/time.jpg);editmenu.add(undoitem);editmenu.add(redoitem);editmenu.addSeparator();editmenu.add(cutitem);editmenu.add(copyitem); editmenu.add(pasteitem); editmenu.addSeparator();editmenu.add(finditem); ; editmenu.add(replaceitem); editmenu.addSeparator();editmenu.add(allitem);editmenu.addSeparator();editmenu.add(dateitem);/添加监听器来实现编辑菜单上的各个菜单项的功能/撤销菜单项的功能实现undoitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)try getUndoManager().undo(); / 执行撤销操作 catch (CannotUndoException ex) JOptionPane.showMessageDialog(new JFrame(),无法撤销!,撤销提示,JOptionPane.INFORMATION_MESSAGE););/恢复菜单项的功能实现redoitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)try getUndoManager().redo(); / 执行重做操作 catch (CannotRedoException ex) JOptionPane.showMessageDialog(new JFrame(),无法恢复!,恢复提示,JOptionPane.INFORMATION_MESSAGE););/剪切菜单项的功能实现cutitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)content.cut(););/复制菜单项的功能实现copyitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) content.copy(););/粘贴菜单项的功能实现pasteitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) content.paste(););/查找菜单项的功能实现finditem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)Action.find();); /替换菜单项的功能实现replaceitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)Action.replace(););/全选菜单项的功能实现allitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)content.selectAll(););/时间菜单项的功能实现dateitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) String str=content.getText();/获取当前文本区文本content.setText(str+s););/创建格式菜单上的各个菜单项并添加到菜单上JMenuItem worditem = new JMenuItem(字体(F);JMenuItem coloritem = new JMenuItem(字体颜色(C);worditem.setIcon(new ImageIcon(icons/word.jpg);coloritem.setIcon(new ImageIcon(icons/color.jpg);formatmenu.add(worditem);formatmenu.addSeparator();formatmenu.add(coloritem);formatmenu.addSeparator();worditem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) Action.setFont(););coloritem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)color=JColorChooser.showDialog(new JFrame(),字体颜色,color);content.setForeground(color););/创建查看菜单上的各个菜单项并添加到菜单上final JCheckBox Gongju=new JCheckBox(工具栏(T);final JCheckBox status=new JCheckBox(状态栏(S);viewmenu.add(Gongju);viewmenu.addSeparator();viewmenu.add(status);Gongju.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(Gongju.isSelected()toolBar.setVisible(true);elsetoolBar.setVisible(false););status.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(status.isSelected()toolBar2.setVisible(true);elsetoolBar2.setVisible(false););/创建帮助菜单上的各个菜单项并添加到菜单上JMenuItem findhelpitem = new JMenuItem(查看帮助(H);JMenuItem aboutboxitem = new JMenuItem(关于记事本(A);JMenuItem writeritem = new JMenuItem(关于作者(S);helpmenu.add(findhelpitem);findhelpitem.setEnabled(false);helpmenu.addSeparator();helpmenu.add(aboutboxitem);helpmenu.addSeparator();helpmenu.add(writeritem);aboutboxitem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)JOptionPane.showMessageDialog(new JFrame(), 写字板1.0版n +本软件用MyEclipse9.0开发制作!n +如有任何疑问及改善意见,随时欢迎指出,n +本人将尽最大的努力满足您的需求!n +谢谢您的使用!n版权所有,请勿侵权!n +如有雷同,纯属抄袭!,关于记事本,JOptionPane.INFORMATION_MESSAGE););writeritem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)JOptionPane.showMessageDialog(new JFrame(),姓名:李宁n性别:男n班级:计算B092n +邮箱:lining_95126.comnQQ号:450696882n +院校:华北科技学院n,关于作者,JOptionPane.INFORMATION_MESSAGE););return jmenuBar;右击菜单:/ 右击菜单void JPopupMenu() / 文本区右击菜单final JPopupMenu popupmenu;JMenuItem itemCopy, itemCut, itemPaste;popupmenu = new JPopupMenu();popupmenu.setBackground(new Color(203, 21, 242);itemCopy = new JMenuItem(复制, new ImageIcon(icons/copy.jpg);itemCut = new JMenuItem(剪切, new ImageIcon(icons/cut.jpg);itemPaste = new JMenuItem(粘贴, new ImageIcon(icons/paste.jpg);itemCopy.setBackground(new Color(203, 21, 242);itemCut.setBackground(new Color(203, 21, 242);itemPaste.setBackground(new Color(203, 21, 242);popupmenu.add(itemCopy);popupmenu.addSeparator();popupmenu.add(itemCut);popupmenu.addSeparator();popupmenu.add(itemPaste);content.addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent e) if (e.getModifiers() = InputEvent.BUTTON3_MASK)popupmenu.show(content, e.getX(), e.getY(););itemCopy.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) content.copy(););itemCut.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) content.cut(););itemPaste.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) content.paste(););void ToolMenu() / 工具栏右击菜单final JPopupMenu ToolMenu;ToolMenu = new JPopupMenu();final JCheckBox move = new JCheckBox(是否可拖动);move.setBackground(new Color(0, 255, 0);ToolMenu.add(move);toolBar.addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent e) if (e.getModifiers() = InputEvent.BUTTON3_MASK)ToolMenu.show(toolBar, e.getX(), e.getY(););move.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if (move.isSelected() toolBar.setFloatable(true); else toolBar.setFloatable(false););工具栏:工具栏和状态栏分别添加为toolBar和toolBar2。/ 创建工具栏及其功能实现public JToolBar getToolBar() if (toolBar = null) toolBar = new JToolBar();toolBar.setFloatable(false); / 设置工具条为不可以拖动toolBar.setVisible(false);toolBar.setBackground(new Color(60, 170, 230);add(toolBar, BorderLayout.NORTH);JButton newbutton = new JButton(,new ImageIcon(icons/new.jpg);JButton savebutton = new JButton(,new ImageIcon(icons/save.jpg);JButton leftbutton = new JButton(,new ImageIcon(icons/left.jpg);JButton centerbutton = new JButton(,new ImageIcon(icons/center.jpg);JButton rightbutton = new JButton(,new ImageIcon(icons/right.jpg);toolBar.add(newbutton);toolBar.add(savebutton);toolBar.addSeparator();newbutton.setBackground(new Color(15, 227, 240);savebutton.setBackground(new Color(15, 227, 240);JLabel label1 = new JLabel(对齐方式);toolBar.add(label1);toolBar.addSeparator();toolBar.add(leftbutton);toolBar.add(centerbutton);toolBar.add(rightbutton);toolBar.addSeparator();leftbutton.setBackground(new Color(15, 227, 240);centerbutton.setBackground(new Color(15, 227, 240);rightbutton.setBackground(new Color(15, 227, 240);/ 添加字体样式选框JToolBar align = new JToolBar();/ 字体样式选框放在同一个JToolBar上align.setFloatable(false);align.setBackground(new Color(15, 227, 240);final JCheckBox cheakbox1 = new JCheckBox();/ 加粗选框final JCheckBox cheakbox2 = new JCheckBox();/ 倾斜选框final JCheckBox cheakbox3 = new JCheckBox();/ 下划线选框cheakbox1.setBackground(new Color(15, 227, 240);/ 选框背景色cheakbox2.setBackground(new Color(15, 227, 240);cheakbox3.setBackground(new Color(15, 227, 240);JLabel Textstyle = new JLabel(字形);JLabel bold = new JLabel();JLabel italic = new JLabel();JLabel underline = new JLabel();bold.setIcon(new ImageIcon(icons/bold.jpg);italic.setIcon(new ImageIcon(icons/italic.jpg);underline.setIcon(new ImageIcon(icons/underline.jpg);bold.setToolTipText(加粗);italic.setToolTipText(倾斜);underline.setToolTipText(下划线);toolBar.add(Textstyle);toolBar.addSeparator();toolBar.add(align);align.add(cheakbox1);align.add(bold);toolBar.addSeparator();align.add(cheakbox2);align.add(italic);toolBar.addSeparator();align.add(cheakbox3);align.add(underline);JLabel label2 = new JLabel(字体);toolBar.add(label2);toolBar.addSeparator();GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();String Fontname = ge.getAvailableFontFamilyNames();final JComboBox FontType = new JComboBox(Fontname);toolBar.add(FontType);FontType.setMaximumRowCount(18);FontType.setBackground(new Color(100, 200, 240);toolBar.addSeparator();JLabel label3 = new JLabel(字号);toolBar.add(label3);toolBar.addSeparator();String ComboStr = 5, 8, 9, 10,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论