Java课程设计报告
题 目:简单记事本程序的设计
年级专业:计算机科学与技术
学 号:
学生姓名:
指导老师:
完成时间:20XX年 6月 8 日
简单记事本程序的设计
摘要
该报告中的程序是使用Java程序编写,能够进行输入文字操作,并具有新建文件,打开文件,保存文件,退出,复制,粘贴,剪切等功能,不同之处是本程序能够对文字的颜色进行设置。该程序主要用到了Java swing和事件监听等。
关键字:记事本程序;Java语言;Jcreator
前言
Java的前身是Oak,它一开始只是被应用于消费性电子产品中。后来它的开发者们发现它还可以被用于更大范围的Internet上。1995年,Java语言的名字从Oak编程了Java。1997年J2SE1.1发布。1998年J2SE1.2发布,标志Java2的诞生。十多年来,Java编程语言及平台成功地运用在网络计算及移动等各个领域。Java的体系结构由Java语言、Java class、Java API、Java虚拟机组成。它具有简单、面向对象、健壮、安全、结构中立、可移植和高效能等众多优点。Java支持多线程编程,Java运行时系统在多线程同步方面具有成熟的解决方案。Java的平台标准有Java ME,Java SE和Java EE。Java发展到今天,它的卓越成就及在业界的地位毋庸置疑。目前在众多的支持Java的开发工具中主要的7有Java Development Kit,NetBeans,Jcreator,JBuilder,JDeveloper和Eclipse等。其中Java Development Kit 简称JDK是大多开发工具的基础。以上的每种开发工具都有优缺点,对于开发者来说,重要的是要根据自己的开发规模、开发内容和软硬件环境等因素来选择一种合适的开发工具。
在本程序设计中,因为需要开发的是记事本程序,规模较小,内容较少,所以选择了适合初学者使用的JCreator开发工具。
1需求分析
1.1需求分析
现在网络上各种文档编辑器数不胜数,有EmEditor ,UEStudio ,GridinSoft Notepad ,Notepad++,win32pad ,SkimEdit,UniRed, xint 。功能也是应有尽有,有能改变字体的,有能改变文字颜色的。但是,这些软件又存在各种各样的瑕疵或问题:有的文件体积相对于一般文字编辑来说太大;有的功能太繁杂,使初级使用者一时难以掌握。仅从日常应用方面来说,一个文本编辑器只需一些简单实用的功能就够了。本程序设计就是依照这样一种使用需要设计了一个简单的记事本程序。
1.2功能设计
本程序要构建的记事本程序参照了Windows操作系统的记事本工具,其功能有以下几个方面:
(1).菜单中有“文件”,“编辑”,“关于”三个主菜单。
(2)“文件”有“新建”“打开”“保存”“退出”四个子菜单:分别用于新
建文件,打开文件,保存文件,退出记事本。
(3)“编辑”中有“剪切”“复制”“粘贴”“颜色”三个字菜单:用于剪切文字,复制文字,粘贴文字。增加了“颜色”子菜单来对文字的颜色进行设置。
(4)“关于”菜单中有关于记事本程序的制作日期,作者等信息
2.概要设计
2.1程序设计思路
在本程序中,设计了一个菜单栏和文本内容区。设计过程中用到了JScrollPane,JMenu,JTextPane,JFileChooser等。JMenu用来制作菜单,菜单包括“文件”“编辑”“关于”。JTextPane主要用来输入文本。为了加入颜色的设置,所以加了一个JColorChooser组件。
2.2程序运行界面
2.3模块说明图
图1
在图1中,首先创建一个名为mynotepad的类并继承JFrame类作为最底层的容器。
图2
在图2中,构造一个mynotepad类时要做三件事情,分别是初始化容器(initTextContent)、初始化组件(initMenu、initAboutDialog)、设置事件监听器。
2.4程序流程图
False False
true true true true true true true true
图3
2.5程序相关说明
整个记事本分为2快:菜单栏和内容输入区。本程序中首先定义一个mynotepad类继承JFrame作为最底层容器。
(1).Textcontent为程序文本输入区。
(2).Menu为菜单栏。
(3).AboutDialog为关于对话框。
(4).要想记事本完成需求分析中相应的功能,还必须添加事件监听器。事件监听器不仅要添加在菜单栏和内容输入区,还需加在容器中。本程序中ActListener实现了ActionListener接口,用来监听并处理所有菜单项和内容输入区为事件源的事件。
另外,还用来WindowListener来监听处理容器关闭触发的事件,WindowListener继承了WindowsAdapter类并覆盖了WindowsClosing方法。
3.程序详细设计与分析
3.1.初始化组件
initTextContent();
initMenu();
initAboutDialog();
3.2.构建菜单栏及其下拉菜单
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("关于")
}
};
3.3.“文件”菜单的事件监听
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);
3.4.“编辑”菜单的事件监听
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);
3.5.异常处理
在运行程序代码是有可能会产生异常情况或异常事件,为了避免这种情况,就需要在程序中用到Java异常处理机制。本程序中选用的异常处理机制是try,catch。捕获处理异常的第一步是用try选定要监控的异常范围,try后跟随catch代码块。
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();}
}
4.测试分析
图4:点击“文件”运行情况
图5:“文件”菜单中的“打开”对话框
图6:“编辑”中的“颜色”设置功能
图7:“关于”对话框
5.源程序清单
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);…………………//设置“关于”对话框大小为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));……………//将“关于”对话框设置为3行1列
about.getContentPane().setBackground(Color.white);//设置对话框背景色
about.getContentPane().add(new JLabel("我的记事本程序"));//对话框内容
about.getContentPane().add(new JLabel("制作者:Fwx"));
about.getContentPane().add(new JLabel("20##年12月"));
about.setModal(true); //设置对话框前端显示
about.setSize(100,100);
about.setLocation(250,170); //设置对话框显示位置
}
;
}
public class Notepad{
public static void main(String args[]){ //入口main函数
mynotepad noted=new mynotepad();
noted.addWindowListener(new WindowAdapter(){
});
noted.setTitle("我的记事本程序"); //记事本标题
noted.setSize(640,320); //设置记事本大小
noted.show();
noted.setLocation(150,100); //设置记事本显示位置
}
}
6.课程设计总结
经过差不多两个星期的努力,基本上完成我的Java课程设计—简单的记事本程序,也基本上实现了我在需求分析时所预期的功能。通过这次的课程设计,使将我从书本上学习到的理论知识用到了实践上,从而进一步巩固和丰富了我所学过的知识,让我更深层次地认识到Java及其强大的功能。同时,做这门课程设计也进一步加强了我的动手能力。为了完成好这次课程设计,我提前做了较多的准备,一边上网查找相关资料,另一方面查阅相关书籍。在这过程中也无形中锻炼了我的思维分析、遇到问题及想方设法通过各种途径解决问题的能力。但是,设计过程中也存在一些问题,本来一开始想给这个记事本程序加上一张背景图片,以及添加字体设置功能,但是由于本人知识浅陋以及各种其他原因,使得这些想法没有实现,这是这次课程设计的一点遗憾。
参考文献
[1].孙全党 王吴迪 赵枫朝,Java程序设计应用教程,电子工业出版社,2006.2
[2].雷之宇,Java项目开发实践—网络篇,中国铁道出版社,2005.5
[3].赵玉阳,Java从入门到精通,清华大学出版社,2006.8
[4].李茏怡,Java编程实例讲解,高等教育出版社,2005.11
完整代码:
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("20##年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);
}
}