文本编辑器课程设计报告
. I Java课程设计报告
题目:文本编辑器
班级:
学号:
姓名:
成绩:
日期:年月日
目录
一、综合设计的目的与要求 (3)
二、综合设计正文 (3)
1.系统分析 (3)
1.1系统开发背景、开发围、建设目标与必要性开发 (3)
1.2需求分析 (3)
2.系统设计 (3)
2.1功能设计 (3)
2.2系统运行环境与开发工具 (4)
3 .系统实施 (4)
3.1源程序 (4)
3.2.系统测试及部分截图 (12)
三、综合设计总结或结论 (13)
四、参考文献 (13)
《文本编辑器》
一、综合设计的目的与要求
要求在学生能够较熟练使用java程序设计语言进行软件代码的编写,同时还要具备较好的项目分析的能力,加深对相关课程基本容的理解。同时,在程序设计方法以及上机操作等基本技能和科学作风方面受到比较系统的练习。
二、综合设计正文
1 系统分析
1.1系统开发背景、开发围、建设目标与必要性
随着计算机科学日渐成熟,其强大的功能已为人们深刻认识,它已进入人类社会的各个领域并发挥着越来越重要的作用。当然对文本编辑操作仍然占据着重要地位,记事本是简单便捷的文本编辑器,可实现对文本编辑操作的基本功能。
1.2需求分析
程序设计主要分为两大部分:简单GUI设计和程序功能设计。通过GUI设计实现图形用户界面,提供图形菜单,方便用户操作。使用Java语言编写一个能够对输入文字进行操作,具有合理的界面,能够在界面中实现右键快捷方式,对文档容进行编辑操作,并且能够顺利退出的程序。通过设计和实现一个具有基本功能的文本编辑器,提高我们对Java语言设计的掌握能力,理论联系实际,进一步提高软件开发技术,从而培养我们分析、解决问题的能力。
2 系统设计
2.1功能设计
根据系统自带的文档编辑器的特征设计,包括如下的功能模块:
一、整体结构框架包括:菜单栏和文档编辑区
二、每个框架分别包含不同的子框架,各个模块的具体框架:
1.菜单栏:文件、编辑、工具、帮助
2.菜单栏中各个子菜单功能
文件:新建、打开、保存、另存为、退出
编辑:剪切、复制、粘贴
工具:查找、替换
3.子菜单功能的实现主要的几个功能介绍
1)打开与另存为:两个窗体调用系统函数直接利用,注意格式的选择
2)查找:查找容、查找、查找下一个
4.文本编辑区
监视文本容是否改变、监听光标所在位置
2.2系统运行环境与开发工具
开发环境与工具:jdk2.0 Eclipse SDK
3 系统实施
3.1 源程序
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class TxtEditor extends JFrame implements ActionListener{
String file_name;
String file_dir;
String tempString;
//上次保存后的文件名和地址
String fileName = "";
JPanel x=new JPanel();
JTextArea wen=new JTextArea(20,50);
JMenuItem ziti=new JMenuItem("字体");
JMenuItem a=new JMenuItem("普通");
//定义菜单项
JMenuItem xin=new JMenuItem("新建");
JMenuItem open=new JMenuItem("打开");
JMenuItem save=new JMenuItem("保存");
JMenuItem lsave=new JMenuItem("另存为");
JMenuItem tui=new JMenuItem("退出");
JMenuItem cut=new JMenuItem("剪切");
JMenuItem copy=new JMenuItem("复制");
JMenuItem cast=new JMenuItem("粘贴");
JMenuItem delete=new JMenuItem("删除");
JMenuItem b=new JMenuItem("粗体");
JMenuItem c=new JMenuItem("斜体");
TxtEditor(){
super ("文本编辑器");
//对话框
setBounds(250,100,700,450);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
int option= JOptionPane.showConfirmDialog(
TxtEditor.this, "确定要退出吗... ", "系统和你对话",JOptionPane.YES_NO_OPTION);
if(option==JOptionPane.YES_OPTION)
if(e.getWindow() == TxtEditor.this)
{
System.exit(0);
}
else
{
return;
}
}
});
//热键设置
xin.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MA SK));
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_ MASK));
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_M ASK));
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_M ASK));
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_ MASK));
cast.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_M ASK));
//定义面板
// x.add(
add(new JScrollPane (wen));//);//滚动条wen.setFont(new Font("楷体" , Font.PLAIN ,20)); // wen.setBackground(Color.blue);
// add(x);
//菜单栏的创建
JMenuBar cai=new JMenuBar();
this.setJMenuBar(cai);
cai.setOpaque(true);
JMenu jian=new JMenu("文件");
jian.add(xin);
jian.add(open);
jian.add(save);
jian.add(lsave);
jian.addSeparator( );
jian.add(tui);
cai.add(jian);
JMenu bian= new JMenu("编辑");
bian.add(cut);
bian.add(copy);
bian.add(cast);
bian.add(delete);
cai.add(bian);
JMenu geshi = new JMenu ("格式");
JMenu optionsMenu=new JMenu("字体"); geshi.add(optionsMenu);
optionsMenu.add(a);
optionsMenu.add(b);
optionsMenu.add(c);
cai.add(geshi);
//增加监听器
xin.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
lsave.addActionListener(this);
tui.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
cast.addActionListener(this);
delete.addActionListener(this);
ziti.addActionListener(this);
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
// 文本框锁定
//this.setResizable(false);
}
//重写方法
public void actionPerformed(ActionEvent e){
String actionCommand=e.getActionCommand();
if(e.getSource()instanceof JMenu);
{
if(e.getSource()==xin){
newfile();
}
else if(e.getSource()==open){
openfile();
}
else if(e.getSource()==save){
savefile();
}
else if(e.getSource()==lsave){
lsavefile();
}
else if(e.getSource()==cut){
cutfile();
}
else if(e.getSource()==copy){
copyfile();
}
else if(e.getSource()==cast){
castfile();
}
else if(e.getSource()==delete){
deletefile();
}
else if(e.getSource()==a){
afile();
}
else if(e.getSource()==b){
bfile();
}
else if(e.getSource()==c){
cfile();
}
else if("退出".equals(actionCommand))
System.exit(0);
}
}
// 方法定义
public void newfile(){
savefile();
wen.setText(null);
fileName = "";
}
//打开
public void openfile(){
String fileName = null;
FileDialog df = new FileDialog(this,"打开文件",FileDialog.LOAD);
df.setVisible(true);
//建立新文件
File f = new File( df.getDirectory()+df.getFile() );
//得到文件名
fileName = df.getDirectory()+df.getFile();
//用此文件的长度建立一个字符数组(特别标注)
char ch[] = new char [(int)f.length()];
//异常处理
try
{
//读出数据,并存入字符数组ch中
BufferedReader bw = new BufferedReader( new FileReader(f) );
bw.read(ch);
bw.close();
}
catch( FileNotFoundException fe ){
System.out.println("file not found");
System.exit(0);