已阅读5页,还剩20页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
用 Java 程序编写一个记事本程序的 设 计 报 告 学习中心 点 泾阳学习中心 专 业 计算机科学与技术 层 次 专升本 姓 名 李 永 固 批 次 112 目 录 一 设计分析 1 二 程序结构 1 三 各模块的功能及程序说明 2 1 类设计 2 2 主要功能代码实现 4 四 源程序 9 五 操作方法 20 六 试验结果 20 七 设计体会 22 1 用 Java 程序编写一个记事本程序的设计报告 一 设计一 设计要求要求 1 用图形用户界面实现 2 能实现编辑 保存 另存为 查找替换等功能 二 程序结构二 程序结构 流程图流程图 调用Loading方法启 动dluThread 线程 登陆界面 开始 dluThread 线程结 束后调用 MiniText 方法 启动MainFunction 的主函数main 调用 initTextPane 方法 初始化面板 调用initMenu 方法 初始化菜单 调用 initAboutDialog 方 法初始化关于对话 框 调用initToolBar 方 法初始化工具栏 调用initRightKey 方法初始化右键设 置 启动runtime线 程创建动态时 间按钮 退出 图图 1 基本流程图基本流程图 2 本Java文本编辑器功能设计如下 基基本本功功能能 文件格式编辑查看帮助工具 新建 打开 保存 另存 为 打 印等 复制 粘贴 删除 撤销等 自动换 行 字体颜 色 系统记 事本 系统计 算器 关于查找时间 图图 2 基本功能图基本功能图 三 各模块的功能及程序说明三 各模块的功能及程序说明 1 类设计 1 类 MiniText 包括 initTextPane 方法初始化面板 initMenu 方法初始 化菜单 initAboutDialog 方法初始化关于对话框 initToolBar 方法初始化工 具栏 initRightKey 方法初始化右键设置 等方法 类成员如下 3 2 类 Loading 实现登陆 类成员如下 4 3 类 runtime 实现时间同步 类成员如下 4 类 MainFunction 包含主函数 类成员如下 2 主要功能代码实现 1 登陆界面 class Loading extends JWindow implements Runnable Thread dluThread null private JProgressBar progress public Loading setCursor Cursor getPredefinedCursor Cursor WAIT CURSOR 设置光标为等待状态 JPanel dlu new JPanel new BorderLayout URL url getClass getResource images loading jpg if url null dlu add new JButton new ImageIcon url BorderLayout CENTER progress new JProgressBar 1 100 progress setStringPainted true progress setBorderPainted true progress setBackground Color white progress setForeground Color green dlu add progress BorderLayout SOUTH setContentPane dlu 设置窗口属性为登陆面板属性 Dimension screen getToolkit getScreenSize pack 显示登陆界面 setLocation screen width 4 screen height 4 设置登陆界面始终在屏幕中间显示 public void start this toFront dluThread new Thread this dluThread start public void run show 5 try for int i 0 i 100 i Thread sleep 100 progress setValue progress getValue 1 progress setString 欢迎进入迷你编辑 请稍后 i catch Exception ex ex printStackTrace dispose 关闭登陆界面 2 时间标签 class runtime extends JButton implements Runnable Thread datetime null public runtime public void run for java util Date timeer new java util Date Calendar now Calendar getInstance int year1 now get Calendar YEAR int month1 now get Calendar MONTH int day1 now get Calendar DAY OF MONTH int hour1 now get Calendar HOUR int minute1 now get Calendar MINUTE int sec now get Calendar SECOND 6 setText year1 年年 month1 月月 day1 日日 hour1 minute1 sec System out println now get Calendar DAY OF MONTH 60 try Thread currentThread sleep 1000 catch InterruptedException e 3 右键的实现 void initRightKey filepopup new JPopupMenu uundo new JMenuItem 撤消撤消 U uundo setMnemonic U ccut new JMenuItem 剪切剪切 T ccut setMnemonic T ccopy new JMenuItem 复制复制 C ccopy setMnemonic C ppaste new JMenuItem 粘贴粘贴 V ppaste setMnemonic V ddelete new JMenuItem 删除删除 D ddelete setMnemonic D sselectall new JMenuItem 全选全选 A sselectall setMnemonic A filepopup add uundo filepopup addSeparator filepopup add ccut filepopup add ccopy filepopup add ppaste filepopup add ddelete filepopup addSeparator filepopup add sselectall uundo addActionListener action ccut addActionListener action 7 ccopy addActionListener action ppaste addActionListener action ddelete addActionListener action sselectall addActionListener action text addMouseListener new MouseAdapter public void mousePressed MouseEvent event checkForTriggerEvent event public void mouseReleased MouseEvent event checkForTriggerEvent event public void checkForTriggerEvent MouseEvent event if event isPopupTrigger filepopup show event getComponent event getX event getY 4 打开 保存 复制 粘贴 撤销等功能事件响应实现 事件处理事件处理 ActionListener action new ActionListener public void actionPerformed ActionEvent e JMenuItem mi JMenuItem e getSource String id mi getText if id equals 新建新建 Date date new Date text setText date toString file null else if id equals 打开打开 if file null filechooser setSelectedFile file 为用户选择文件提供了一种简单的机制为用户选择文件提供了一种简单的机制 8 int returnVal filechooser showOpenDialog MiniText this if returnVal JFileChooser APPROVE OPTION file filechooser getSelectedFile openFile else if id equals 保存保存 if file null filechooser setSelectedFile file int returnVal filechooser showSaveDialog MiniText this if returnVal JFileChooser APPROVE OPTION file filechooser getSelectedFile saveFile JOptionPane showMessageDialog null 已保存已保存 else if id equals 另存为另存为 if file null filechooser setSelectedFile file int returnVal filechooser showSaveDialog MiniText this if returnVal JFileChooser APPROVE OPTION file filechooser getSelectedFile saveFile JOptionPane showMessageDialog null 已保存已保存 else if id equals 打印打印 prt printDialog else if id equals 打印页面设置打印页面设置 try prt print catch Exception ew JOptionPane showMessageDialog null 打印出错打印出错 错误错误 JOptionPane ERROR MESSAGE else if id equals 退出退出 System exit 0 else if id equals 剪切剪切 id equals 剪切剪切 T text cut else if id equals 复制复制 id equals 复制复制 C text copy else if id equals 粘贴粘贴 id equals 粘贴粘贴 V text paste else if id equals 删除删除 id equals 删除删除 D text replaceSelection else if id equals 选择全部选择全部 id equals 全选全选 A text selectAll else if id equals 字体颜色字体颜色 color JColorChooser showDialog MiniText this 字体颜色设置字体颜色设置 color text setForeground color 9 else if id equals 自动换行自动换行 else if id equals 撤销撤销 id equals 撤消撤消 U text setText text selectAll else if id equals 查找查找 String find 查找查找 find JOptionPane showInputDialog 查找查找 else if id equals 时间时间 time setFont new Font Times New Roman Font PLAIN 12 text insertComponent time else if id equals 系统系统 记事本记事本 try String command notepad exe Process child Runtime getRuntime exec command 从系统进程中调用记事本从系统进程中调用记事本 catch IOException ex else if id equals 系统系统 计算器计算器 try String command calc exe Process child Runtime getRuntime exec command 调用系统计算器调用系统计算器 catch IOException ex else if id equals 关于关于 about setSize 250 150 about show 四 源程序四 源程序 import java awt import java awt event import javax swing import java io public class Notepad extends JFrame 系统组件声明 private JMenuBar menuBar new JMenuBar private JEditorPane content new JEditorPane private JScrollPane scroll new JScrollPane content private JFileChooser filechooser new JFileChooser private BorderLayout bord new BorderLayout private JLabel statusBar new JLabel 10 private JPanel pane new JPanel private File file null 定义文件菜单 private JMenu fileMenu new JMenu private JMenuItem newMenuItem new JMenuItem private JMenuItem openMenuItem new JMenuItem private JMenuItem saveMenuItem new JMenuItem private JMenuItem saveAsMenuItem new JMenuItem private JMenuItem pageSetupMenuItem new JMenuItem private JMenuItem printMenuItem new JMenuItem private JMenuItem exitMenuItem new JMenuItem 定义风格菜单 private JMenu styleMenu new JMenu private ButtonGroup styleMenuGroup new ButtonGroup private JRadioButtonMenuItem javaStyleMenuItem new JRadioButtonMenuItem private JRadioButtonMenuItem metalStyleMenuItem new JRadioButtonMenuItem private JRadioButtonMenuItem windowsStyleMenuItem new JRadioButtonMenuItem 定义帮助菜单 private JMenuItem aboutMenuItem new JMenuItem private JMenuItem helpTopicMenuItem new JMenuItem private JMenu helpMenu new JMenu 构造函数 public Notepad initComponents private void initComponents 添加子菜单项到文件菜单 fileMenu setText u6587 u4ef6 F newMenuItem setText 新建 N Ctrl N openMenuItem setText 打开 O Ctrl O saveMenuItem setText 保存 S Ctrl S saveAsMenuItem setText 另存为 A pageSetupMenuItem setText 页面设置 U printMenuItem setText 打印 P Ctrl P exitMenuItem setText 退出 fileMenu add newMenuItem fileMenu add openMenuItem fileMenu add saveMenuItem 11 fileMenu add saveAsMenuItem fileMenu addSeparator fileMenu add pageSetupMenuItem fileMenu add printMenuItem fileMenu addSeparator fileMenu add exitMenuItem 添加子菜单项到风格菜单 styleMenu setText 风格 S javaStyleMenuItem setText Java 默认 metalStyleMenuItem setText Metal 风格 windowsStyleMenuItem setText Windows 风格 styleMenuGroup add javaStyleMenuItem styleMenuGroup add metalStyleMenuItem styleMenuGroup add windowsStyleMenuItem styleMenu add javaStyleMenuItem styleMenu add metalStyleMenuItem styleMenu add windowsStyleMenuItem 添加子菜单项到帮助菜单 helpMenu setText 帮助 H helpTopicMenuItem setText 帮助主题 H aboutMenuItem setText 关于记事本 A helpMenu add helpTopicMenuItem helpMenu addSeparator helpMenu add aboutMenuItem 定义文件菜单下的事件监听 newMenuItem addActionListener new newMenuItem actionAdapter this openMenuItem addActionListener new openMenuItem actionAdapter this saveMenuItem addActionListener new saveMenuItem actionAdapter this saveAsMenuItem addActionListener new saveAsMenuItem actionAdapter this pageSetupMenuItem addActionListener new pageSetupMenuItem actionAdapter this printMenuItem addActionListener new printMenuItem actionAdapter this exitMenuItem addActionListener new exitMenuItem actionAdapter this 定义风格菜单下的事件监听 javaStyleMenuItem addActionListener new javaStyleMenuItem actionAdapter this metalStyleMenuItem addActionListener new metalStyleMenuItem actionAdapter this windowsStyleMenuItem addActionListener new windowsStyleMenuItem actionAdapter this 定义帮助菜单下的事件监听 helpTopicMenuItem addActionListener new helpTopicMenuItem actionAdapter this 12 aboutMenuItem addActionListener new aboutMenuItem actionAdapter this 填加菜单到菜单栏 menuBar add fileMenu menuBar add styleMenu menuBar add helpMenu 对主窗口的一些设置 this setDefaultCloseOperation EXIT ON CLOSE this setTitle 无标题 u8bb0 u4e8b u672c this setSize 640 480 setJMenuBar menuBar pane setLayout bord pane add Center scroll setContentPane pane 定义新建菜单项方法 public void newMenuItemActionPerformed ActionEvent evt file null if equals content getText Object options 是 Y 否 N 取消 int s JOptionPane showOptionDialog null u6587 u4ef6 getTitle u7684 u6587 u5b57 u5df2 u7ecf u6539 u53d8 u3002 n u60f3 u4fdd u5b58 u6587 u4ef6 u5417 uff 1f u8bb0 u4e8b u672c JOptionPane DEFAULT OPTION JOptionPane WARNING MESSAGE null options options 0 switch s case 0 int returnVal filechooser showSaveDialog this if returnVal JFileChooser APPROVE OPTION file filechooser getSelectedFile try FileWriter fw new FileWriter file fw write content getText setTitle filechooser getSelectedFile getName u8bb0 u4e8b u672c fw close catch Exception e e printStackTrace break 13 case 1 content setText setTitle 无标题 u8bb0 u4e8b u672c 定义打开菜单项方法 public void openMenuItemActionPerformed ActionEvent evt try file null int returnVal filechooser showOpenDialog this if returnVal JFileChooser APPROVE OPTION file filechooser getSelectedFile FileReader fr new FileReader file int len int file length char buffer new char len fr read buffer 0 len fr close content setText new String buffer catch Exception e e printStackTrace 定义退出菜单项方法 public void exitMenuItem actionPerformed ActionEvent e if equals content getText Object options 是 Y 否 N 取消 int s JOptionPane showOptionDialog null 文件的文字已经改变 n 想保存文件吗 u8bb0 u4e8b u672c JOptionPane DEFAULT OPTION JOptionPane WARNING MESSAGE null options options 0 switch s case 0 int returnVal filechooser showSaveDialog this if returnVal JFileChooser APPROVE OPTION file filechooser getSelectedFile try FileWriter fw new FileWriter file fw write content getText 14 setTitle filechooser getSelectedFile getName u8bb0 u4e8b u672c fw close catch Exception ex ex printStackTrace break case 1 System exit 0 else System exit 0 保存事件 public void saveMenuItemActionPerformed ActionEvent evt int returnVal filechooser showSaveDialog this if returnVal JFileChooser APPROVE OPTION file filechooser getSelectedFile try FileWriter fw new FileWriter file fw write content getText setTitle filechooser getSelectedFile getName u8bb0 u4e8b u672c fw close catch Exception e e printStackTrace 另存为事件 public void saveAsMenuItemActionPerformed ActionEvent evt filechooser setDialogTitle 另存为 int returnVal filechooser showSaveDialog this if returnVal JFileChooser APPROVE OPTION file filechooser getSelectedFile try FileWriter fw new FileWriter file 15 fw write content getText setTitle filechooser getSelectedFile getName u8bb0 u4e8b u672c fw close catch Exception e e printStackTrace 页面设置事件 public void pageSetupMenuItemActionPerformed ActionEvent evt JOptionPane showMessageDialog null 此功能正在开发中 打印事件 public void printMenuItemActionPerformed ActionEvent evt JOptionPane showMessageDialog null 打印中 更新风格外观方法 void changeLookFeel String className try UIManager setLookAndFeel className catch Exception e System out println e SwingUtilities updateComponentTreeUI this Java 风格事件 public void javaStyleMenuItemActionPerformed ActionEvent evt changeLookFeel javax swing plaf metal MetalLookAndFeel Motif 风格事件 public void metalStyleMenuItemActionPerformed ActionEvent evt changeLookFeel com sun java swing plaf motif MotifLookAndFeel 16 MAC 风格事件 public void windowsStyleMenuItemActionPerformed ActionEvent evt changeLookFeel com sun java swing plaf windows WindowsLookAndFeel 帮助事件 public void helpTopicMenuItemActionPerformed ActionEvent evt JOptionPane showMessageDialog null u9700 u8981 u5e2e u52a9 u5417 uff1f 关于事件 public void aboutMenuItemActionPerformed ActionEvent evt JOptionPane showMessageDialog null n u7a0b u5e8f u540d u79f0 uff1aJava u8bb0 u4e8b u672c n u4ee3 u7801 u7f16 u5199 uff1aCavien n u4f5c u8005 u7f51 u7ad9 uff1ahttp nE mail u3000 uff1aCavien 主函数 public static void main String args Notepad notepad new Notepad notepad setVisible true 定义新建事件类 class newMenuItem actionAdapter implements ActionListener Notepad adaptee newMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee newMenuItemActionPerformed evt 定义打开事件类 class openMenuItem actionAdapter implements ActionListener Notepad adaptee openMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt 17 adaptee openMenuItemActionPerformed evt 定义保存事件类 class saveMenuItem actionAdapter implements ActionListener Notepad adaptee saveMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee saveMenuItemActionPerformed evt 定义另存为事件类 class saveAsMenuItem actionAdapter implements ActionListener Notepad adaptee saveAsMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee saveAsMenuItemActionPerformed evt 定义页面设置事件类 class pageSetupMenuItem actionAdapter implements ActionListener Notepad adaptee pageSetupMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee pageSetupMenuItemActionPerformed evt 定义打印事件类 class printMenuItem actionAdapter implements ActionListener Notepad adaptee 18 printMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee printMenuItemActionPerformed evt 定义 Java 风格事件类 class javaStyleMenuItem actionAdapter implements ActionListener Notepad adaptee javaStyleMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee javaStyleMenuItemActionPerformed evt 定义 Java 风格事件类 class metalStyleMenuItem actionAdapter implements ActionListener Notepad adaptee metalStyleMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee metalStyleMenuItemActionPerformed evt 定义 Java 风格事件类 class windowsStyleMenuItem actionAdapter implements ActionListener Notepad adaptee windowsStyleMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee windowsStyleMenuItemActionPerformed evt 19 定义帮助主题事件类 class helpTopicMenuItem actionAdapter implements ActionListener Notepad adaptee helpTopicMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee helpTopicMenuItemActionPerformed evt 定义关于软件事件类 class aboutMenuItem actionAdapter implements ActionListener Notepad adaptee aboutMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee aboutMenuItemActionPerformed evt 定义退出事件类 class exitMenuItem actionAdapter implements ActionListener Notepad adaptee exitMenuItem actionAdapter Notepad adaptee this adaptee adaptee public void actionPerformed ActionEvent evt adaptee exitMenuItem actionPerformed evt 20 五 操作方法五 操作方法 由于功能比较多 不能全部截图演示 所以取其有代表的几项主要功能演示 其他在实际程序运行中可逐步实现 1 开始界面 包括基本菜单栏 工具栏 动态时间按钮 而且本编辑框 被设计为在屏幕中间显示 2 菜单显示 文件菜单包括新建 打开 保存 另存为 打印 打印页 面设置 退出等菜单子项 编辑菜单包括复制 剪切 粘贴 全选 撤销等菜单 子项 还有格式 查看 工具 帮助等六项主菜单和工具栏七个常用工具 3 打开功能的实现 出现选择文件对话框 包括打开和取消两个选择按 钮 用户选择合适的文件后可以点击打开按钮 在文本中显示文件内容 4 进入编辑状态后 点击右键选择全选菜单 或使用主菜中的选择全部 子菜单项 则文本将会处于被全选状态 5 若点击字体颜色设置工具则会出现相应字体颜色选择框 6 若选择部分文字 通过右键 工具栏或下拉菜单中的剪切或删除操作 会进行相应动作 剪切后粘贴 7 单击查看菜单中的查找子菜单 出现相应查找对话框 选择操作 8 进行撤销操作 9 若选择保存操作 则有相应保存选择对话框出现 若想保存为 word 文档 则需把后缀名改为 doc 形式 随后出现保存信息提示框 若没有成功保存 则提示保存出错 若保存成功 则 10 选择退出 则会关闭文本编辑器并退出界面 六 六 试验结果试验结果 经过多次调试运行 发现很多不足 1 刚开始启动程序时 等待时间比较长 于是考虑增加一个线程 设计登 陆界面 更加友好界面 代码改进 增加如下 登陆界面 class Loading extends JWindow implements Runnable Thread dluThread null private JProgressBar progress public Loading 21 setCursor Cursor getPredefinedCursor Cursor WAIT CURSOR 设置光标为等待状态 JPanel dlu new JPanel new BorderLayout URL url getClass getResource images loading jpg if url null dlu add new JButton new ImageIcon url BorderLayout CENTER progress new JProgressBar 1 100 progress setS
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 16财经商贸大类2027广西专升本财经商贸大类全真模拟-第二套(能力提升·中)(含详解与质量评价)
- 20 狼(教学设计)-2026-2027学年统编版语文七年级上册
- 20XX教师节短信祝福语教师节发给老师的祝福短信大全
- 江苏省扬州市2027届高三下学期联考数学试题含解析
- 安徽省重点高中联考2026-2027学年高一上学期期中考试政治试卷
- 钢桩施工检验批质量验收记录
- 2026年野生动物栖息地修复员(技师)职业技能等级认定考前冲刺试题
- 2026年土壤采样分析员(初级)职业技能等级认定模拟试题
- 2026年道床清筛机操作员(高级)职业技能等级认定历年真题
- 八年级地理上学期 主题学习 增强国家海洋权益意识 教学设计
- 2026河北石家庄市中晟华章企业管理有限公司公开招聘35人笔试备考题库及答案解析
- 河南省洛阳市2026-2027学年高一上学期11月期中考试化学试卷
- 湖北武汉华师一附中2027届高三上学期11月期中考试数学试卷
- 安徽省建设工程工程量清单计价规程(2026版)
- 住宿场所旅客突发疾病应急方案
- (2026年)个人述职述廉述德报告
- 管理人员竞聘考试题库及答案
- 老旧小区改造外墙保温施工组织设计
- 供水公司泵站运行管理制度
- 2026年高考英语口语北京卷考试真题
- 2025~2026学年江苏省如皋市高二上学期期末考试生物试卷
评论
0/150
提交评论