Java课程设计报告-记事本源代码有流程图

Java课程设计报告

题 目:简单记事本程序的设计
年级专业:计算机科学与技术  软件工程
学 号:
学生姓名:
指导老师:


目    录

摘要… 1

前言… 2

1需求分析… 2

1.1需求分析… 2

1.2功能设计… 3

2.概要设计… 3

2.1程序设计思路… 3

2.2程序运行界面… 3

2.3模块说明图… 4

2.4程序流程图… 5

2.5程序相关说明… 6

3.程序详细设计与分析… 6

3.1.初始化组件… 6

3.2.构建菜单栏及其下拉菜单… 6

3.3.“文件”菜单的事件监听… 7

3.4.“编辑”菜单的事件监听… 8

3.5.异常处理… 9

4.测试分析… 10

5.源程序清单… 12

6.课程设计总结… 17

参考文献… 17

程序代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class mynotepad extends JFrame{
    File file=null;
   	Color color=Color.red;
   	mynotepad(){
        initTextContent();
   	initMenu();
   	initAboutDialog();
   	        }
   	void initTextContent(){
   		getContentPane().add(new JScrollPane(content));
   		}
       JTextPane content=new JTextPane(); 
      JFileChooser openfile=new JFileChooser();
      JColorChooser opencolor=new JColorChooser();
      JDialog about=new JDialog(this);
      JMenuBar menu=new JMenuBar();
   	
 JMenu[] menus=new JMenu[]{
  new JMenu("文件"),
  new JMenu("编辑"),
  new JMenu("关于")
 };
 
 JMenuItem optionofmenu[][]=new JMenuItem[][]{{
  new JMenuItem("新建"),
  new JMenuItem("打开"),
  new JMenuItem("保存"),
  new JMenuItem("退出")
           },		         
   {
          
  new JMenuItem("复制"),         
  new JMenuItem("剪切"),
  new JMenuItem("粘贴"),
  new JMenuItem("颜色")
               },
               {
               new JMenuItem("关于")
               }
        };
      void initMenu(){
      	
      	  for(int i=0;i<menus.length;i++){
      	  	menu.add(menus[i]);
      	  	for(int j=0;j<optionofmenu[i].length;j++){
      	  		menus[i].add(optionofmenu[i][j]);
      	  		optionofmenu[i][j].addActionListener( action );
      	  	}
      	  }
      	  this.setJMenuBar(menu);
      } 
     ActionListener action=new ActionListener(){                   
     public void actionPerformed(ActionEvent e){
     	String name = e.getActionCommand();
  JMenuItem MI=(JMenuItem)e.getSource();
  if("新建".equals(name)){
   content.setText("");
   file=null;
  }else if("打开".equals(name)){
                    if(file !=null)openfile.setSelectedFile(file);
                    int returnVal=openfile.showOpenDialog(mynotepad.this);
                    if(returnVal==JFileChooser.APPROVE_OPTION){

                    file=openfile.getSelectedFile();
                    unfold();
                              }

      }else if("保存".equals(name)){
       if(file!=null) openfile.setSelectedFile(file);
          int returnVal=openfile.showSaveDialog(mynotepad.this);
                if(returnVal==JFileChooser.APPROVE_OPTION){
                file=openfile.getSelectedFile();
                  saving();
                                  }
           
             }else if("退出".equals(name)){
               mynotepad f=new mynotepad();
               int s=JOptionPane.showConfirmDialog(f,"退出?","退出",JOptionPane.YES_NO_OPTION);
               if(s==JOptionPane.YES_OPTION)
                System.exit(0);
             }else if("剪切".equals(name)){
               content.cut();
             }else if("复制".equals(name)){
               content.copy();
             }else if("粘贴".equals(name)){
               content.paste();
             }else if("颜色".equals(name)){
               color=JColorChooser.showDialog(mynotepad.this,"",color);
                 content.setForeground(color); 
                 
    }else if("关于".equals(name)){
              about.setSize(300,150);
              about.show();
     }    
   }  
  }; 

  void saving(){
       try{
        FileWriter Writef=new FileWriter(file);
        Writef.write(content.getText());
        Writef.close();
         }
    catch(Exception e){e.printStackTrace();}
                  }                
    void unfold(){
         try{
              FileReader Readf=new FileReader(file);
              int len=(int)file.length();
              char []buffer=new char[len];
              Readf.read(buffer,0,len);
              Readf.close();
              content.setText(new String(buffer));
              }catch(Exception e){e.printStackTrace();}
       }
    void initAboutDialog(){
      about.setLayout(new GridLayout(3,1));
      about.getContentPane().setBackground(Color.white);
      about.getContentPane().add(new JLabel("我的记事本程序"));
      about.getContentPane().add(new JLabel("制作者:liuhui"));
      about.getContentPane().add(new JLabel("2010年6月"));
      about.setModal(true);
      about.setSize(100,100);
      about.setLocation(250,170);
       }
  ;
   }   
     public class Notepad{
 public static void main(String args[]){		 
                mynotepad noted=new mynotepad();                
              noted.addWindowListener(new WindowAdapter(){
                  });
                       noted.setTitle("我的记事本程序");
                 noted.setSize(640,320);
                 noted.show();
                 noted.setLocation(150,100);
 }
  }

Java课程设计报告-记事本源代码有流程图下载

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/3782.html

(0)
江山如画的头像江山如画管理团队
上一篇 2019年10月4日 上午10:46
下一篇 2019年10月4日 下午8:58

99%的人还看了以下文章

  • python 循环语句的应用:水仙花数判断及爱因斯坦阶梯编程

    一、水仙花数判断程序 1.任务内容: 水仙花数是一个三位整数,如153是一个水仙花数,是因为该数的百位的立方、十位的立方、个位的立方和等于该数本身,如下所示: 2.程序编写要求: 使用for语句完成; 统计水仙花数个数的值保存到变量中,要求自动进行统计 •输出结果如下所示: 153 是水仙数370 是水仙数371 是水仙数407 是水仙数三位数中有4个水仙数…

    2022年5月11日
    3.3K1
  • python 中time.gmtime()与localtime()的区别

    gmtime转换的时间是UTL时间,与北京时间相差了8个小时 import time print(time.time()) print(time.strftime(“%Y-%m-%d 星期%w %H:%M:%S”,time.localtime())) print(time.strftime(“%Y-%m-%d 星期%w %H:%M:%S”,time.gmti…

    2022年7月15日
    3.2K0
  • 第2课:编写第一个C语言程序

      用C语言语句编写的程序称为C程序或C源程序。 下面编写第一个C语言程序,这个程序是在Visual C++ 环境下编译通过的。 Microsoft Visual C++(简称Visual C++、MSVC、VS或VC)是微软公司的C++开发工具,具有集成开发环境,可提供编辑C语言,C++以及C++/CLI等编程语言。 【例1.1】用C语言编写一个…

    2020年4月5日
    4.7K0
  • 2个pycharm使用技巧:更改文件、文件夹名称,更换pip为清华源

    pycharm怎么重命名文件、文件夹名称 1.右键单击文件名称,在弹出的界面选择refacto下的rename选项 2.在弹出的界面输入新的名称即可 pycharm更换pip清华源 永久使用 直接在Pycharm打开终端Terminal,选择Command Prompt,将下列语句复制进去,回车执行 pip config set global.index-u…

    2023年10月3日
    1.6K0
  • 单元测试步骤、单元测试策略,单元测试快速入门教程三

    工作性质不同决定了工作侧重点也不同,因此程序开发人员在单元测试过程中关注更多的是程序代码本身和已经实现的功能。因此,站在他们的角度看,单元测试的过程就是在编写测试方法之前: 首先考虑如何对方法进行测试; 然后编写测试代码; 下一步就是运行某个测试,或者同时运行该单元的所有测试,确保所有测试都通过。 下图从宏观的角度概括了单元测试的工作过程图。 1.单元测试进…

    2018年4月18日
    5.0K0
  • 第一个Tkinter程序

    Tkinter(Python GUI编程)从入门到精通(一) 上一篇文章介绍什么是Tkinter?tkinter的优缺点,tkinter的开发工具。 今天我们在上一篇的基础上,开始给主窗口添加文本、按钮,并给按钮绑定一个事件,点击按钮弹出一个消息窗口。 创建label label=tk.Label(root,text=”hello GUI&#82…

    2022年8月25日
    1.4K0

发表回复

登录后才能评论