文本编辑器的设计与实现

文本编辑器的设计与实现
文本编辑器的设计与实现

软件学院

课程设计报告书

课程名称

设计题目文本编辑器的设计与实现

专业班级 XXXXXXXXXXX 学号 xxxxxxxxxx 姓名 xxx 指导教师

2011 年11月

1 设计时间

2011年11月

2 设计目的

《面向对象程序设计》是一门实践性很强的计算机专业基础课程,课程设计是学习完该课程后进行的一次较全面的综合练习。其目的在于通过实践加深学生对面向对象程序设计的理论、方法和基础知识的理解,掌握使用Java语言进行面向对象设计的基本方法,提高运用面向对象知识分析实际问题、解决实际问题的能力,提高学生的应用能力。目前文本编辑器种类很多,所提供的功能也很多,但是能满足用户实现多种功能和进行Java的编译与运行很少,不能更好的适应当前用户的要求。本设计所完成的文本编辑器功能是针对学习Java程序语言,因此我们利用Java程序设计虚拟机和软件对用户及使用者的应用过程形成一整套完整的编写代码,编译,运行。

3设计任务

文本编辑器的设计与实现:设计一个类似于Windows记事本(Notepad)的Java程序。可以打开、新建、保存一个文本文件;对选中的文本进行各种编辑操作(设置字体、字号、字型、对齐方式、背景、前景色、复制、粘贴、剪切、查找、替换等);在文本中能够插入对象。

4 设计内容

4.1需求分析

需求分析的任务是确定功能必须完成的工作,也就是对目标系统提出完整、准确、清晰、具体的要求。简单文本编辑器提供给用户基本的纯文本编辑功能,能够将用户录入的文本存储到本地磁盘中。能够读取磁盘中现有的纯文本文件,以及方便用户进行需要的编辑功能。文件操作能够实现新建、保存、打开文档等,编辑操作能过实现文本的剪贴、复制、粘贴等,格式操作能过实现字体设置、背景等,帮助操作能够实现关于主题的查看等功能。

4.2概要设计

4.2.1程序基本功能概括

图4.2.1 功能架构图 4.2.2程序主要组件概括

1.基本的Frame 框架;

2.菜单;

3.打开文件对话框;

4.保存文件对话框;

5.颜色对话框;

6.Choice 下拉列表,运来实现字体设置;

7.简单的帮助框架。

4.3详细设计

4.3.1文件打开与保存

文本编辑器

格式

编辑

黏贴 打开

菜单

保存

新建

退出

另存为

文件

剪切

黏贴

查找

复制

字体

字号

插入对象

替换

文本编辑器的保存和打开功能的实现用文件对话框及输入输出流来完成。先建立打开和保存对话框,在public void actionPerformed(ActionEvent e)里分别用FileWriter()和FileReader()方法实现保存和打开。

filedialog_save=new FileDialog(this,"保存文件对话框",FileDialog.SA VE);

filedialog_save.setVisible(false);

filedialog_load=new FileDialog(this,"保存文件对话框",FileDialog.LOAD);

f iledialog_load.setVisible(false);

filedialog_save.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

filedialog_save.setVisible(false);

}

});

filedialog_load.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

filedialog_load.setVisible(false);

}

});

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==itemSave)

{

filedialog_save.setVisible(true);

if(filedialog_save.getFile()!=null)

{

try {File file=new

File(filedialog_save.getDirectory(),

filedialog_save.getFile());

tofile=new FileWriter(file);

out=new BufferedWriter(tofile);

out.write(area.getText(),0,(area.getText()).length());

out.close();

tofile.close();

}

catch(IOException e1) {}

}

}

else if(e.getSource()==itemLoad)

{

filedialog_load.setVisible(true);

area.setText(null);

String s;

if(filedialog_load.getFile()!=null)

{

try{File file=new

File(filedialog_load.getDirectory(),

filedialog_load.getFile());

file_reader=new FileReader(file);

in=new BufferedReader(file_reader);

while((s=in.readLine())!=null)

area.append(s+'\n');

in.close();

file_reader.close();

}

catch(IOException e1) {}

}

}

4.3.2字体,字形,字体大小的设置

文本编辑器要实现对字体的设置,选用了GraphicsEnvironment对象调用String [] getAvailableFontFamilyNames()方法,该方法可以获取计算机上所有可用的字体名称,并存放到字符串数组中。

Choice list;

GraphicsEnvironment

ge=GraphicsEnvironment.getLocalGraphicsEnvironment();

String fontName[]=ge.getAvailableFontFamilyNames();

public void itemStateChanged(ItemEvent e)

{

String name=list.getSelectedItem();

Font f=new Font(name,Font.PLAIN,15);

area.setFont(f);

}

else if(e.getSource()==item8)//设置字形(常规,倾斜,加粗)

{

Font font=area.getFont();

int style=font.getStyle();

style=style^0;

area.setFont(new Font("",style,font.getSize()));

}

else if(e.getSource()==item9)

{

Font font=area.getFont();

int style=font.getStyle();

style=style^2;

area.setFont(new Font("",style,font.getSize()));

}

else if(e.getSource()==item10)

{

Font font=area.getFont();

int style=font.getStyle();

style=style^1;

area.setFont(new Font("",style,font.getSize()));

}

else if(e.getSource()==item11) //设置字体大小

{

Font font=area.getFont();

int style=font.getStyle();

area.setFont(new Font(font.getName(),style,12));

}

else if(e.getSource()==item12)

{

Font font=area.getFont();

int style=font.getStyle();

area.setFont(new Font(font.getName(),style,24));

}

else if(e.getSource()==item13)

{

Font font=area.getFont();

int style=font.getStyle();

area.setFont(new Font(font.getName(),style,36));

}

4.3.3剪切,复制,粘贴设置

文本编辑器中关于剪切,复制,粘贴功能的实现选用处理JTextArea的DocumentEvent事件,通过area.cut(),area.copy(),area.paste()方法,点击“编辑”中相应菜单项可以选择将文本区中选中的内容剪切,复制,粘贴。

public void changedUpdate(DocumentEvent e)

{

String s=area.getText();

}

public void removeUpdate(DocumentEvent e)

{

changedUpdate(e);

}

public void insertUpdate(DocumentEvent e)

{

changedUpdate(e);

}

public void actionPerformed(ActionEvent e)

{

else if(e.getSource()==item2)

{

area.cut();

}

else if(e.getSource()==item3)

{

area.copy();

}

else if(e.getSource()==item4)

{

area.paste();

}

}

4.3.4插入子菜单,删除子菜单,创建格式菜单及其菜单项

JMenuItem insertItem=new JMenuItem("插入文本(K)");

insertItem.setMnemonic('K');

editMenu.add(insertItem);

insertItem.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent event){

JPanel insertPanel=new JPanel();

JLabel insertLabel=new JLabel("要插入的内容");

JTextField inputText=new JTextField(10);

insertPanel.add(insertLabel);

insertPanel.add(inputText);

JOptionPane.showMessageDialog(null,insertPanel);

int fromIndex=displayText.getCaretPosition();//取得当前的光标位置

displayText.insert(inputText.getText(),fromIndex);

}

}

);

JMenuItem RemoveItem=new JMenuItem("删除(G)");

RemoveItem.setMnemonic('G');

editMenu.add(RemoveItem);

RemoveItem.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{ int start=displayText.getSelectionStart();

int end=displayText.getSelectionEnd();

displayText.replaceRange(null,start,end);

}

});

editMenu.addSeparator();

bar.add( editMenu );//add editMenu

JMenu formatMenu = new JMenu( "格式(R)" );

formatMenu.setMnemonic( 'R' );

4.3.5创建,添加帮助菜项

JMenu helpMenu = new JMenu( "帮助(H)" );

helpMenu.setMnemonic( 'H' );

JMenuItem helpItem = new JMenuItem( "帮助主题(H)..." );

helpItem.setMnemonic( 'H' );

helpMenu.add( helpItem );

helpItem.addActionListener(

new ActionListener(){

public void actionPerformed( ActionEvent event ){

JTextArea helpText = new JTextArea(

JScrollPane scroller = new JScrollPane(helpText);

JOptionPane.showMessageDialog(null,scroller);

}

}

);

bar.add( helpMenu ); //添加

4.4设计成果

4.4.1运行界面

图2 文本编辑器主界面

图3文本编辑器编辑界面

图4文本编辑器文件界面

图5文本编辑器格式

图6文本编辑器查找界面

图7 文本编辑器帮助界面

图8文本编辑器字体名称界面

图9文本编辑器字体风格界面

图10文本编辑器中帮助中关于对话框

图11查找消息对话框

图12文本编辑器中另存为对话框

4.4.2主要代码

import java.awt.*;

import java.awt.event.*;

import java.awt.datatransfer.*;

import javax.swing.*;

import java.io.*;

import https://www.360docs.net/doc/3a12579438.html,ng.*;

public class Notepad extends JFrame{

private final Color colorvalues[] =

{ Color.black, Color.blue, Color.red, Color.green }; //定义颜色数组String styleNames[] = { "Bold", "Italic" };//定义风格数组

String fontNames[] = { "宋体", "华文行楷", "隶书" };//字体数组

String[] sizeString = new String[30];//字号数组

int[] size = new int[30];//与字号数组对应的字号整数,用于设置文字大小

private JRadioButtonMenuItem colorItems[], fonts[];

private JCheckBoxMenuItem styleItems[];

private JTextArea displayText;//定义文本编辑区

private ButtonGroup fontGroup, colorGroup;//字体组,跟字色组private int style;//字体风格

private JScrollPane scroll;//为文本编辑区提供滚动条

private String selectText = "";//存放文本编辑区中选中的文本内容private JComboBox styleBox,fontBox,sizeBox;//工具栏

private JPanel toolPanel;//存放工具栏

private int rowNumber = 0;

private FileDialog fd = new FileDialog(this);

// set up GUI

public Notepad()

{

super( "记事本" );

//创建菜单条

JMenuBar bar = new JMenuBar();

setJMenuBar( bar )

// 设置文件菜单及其菜单项

JMenu fileMenu = new JMenu( "文件(F)" );

fileMenu.setMnemonic( 'F' );

// 设置新建菜单项

JMenuItem newItem = new JMenuItem( "新建(N)" );

newItem.setMnemonic( 'N' );

fileMenu.add( newItem );

newItem.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )

{displayText.setText("");

}});

// 设置打开菜单项

JMenuItem openItem = new JMenuItem( "打开(O)" );

openItem.setMnemonic( 'O' );

fileMenu.add( openItem );

openItem.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )

{

fd.setTitle("打开");

//设置标题

fd.show();

if (fd.getFile() != null) {

File file = new File(fd.getFile()); //用从fd中取得的文件建立新文件,即打开的文件

displayText.setText( "");

try {

FileReader f = new FileReader(file);

BufferedReader b = new BufferedReader(f);//按行读打开的文件,然后传入文本域

String s;

try {

while ((s = b.readLine()) != null) {

displayText.append(s + "\n");//将给定文本追加到文本域的当前文本(即把读的内容加入文本域)

}

f.close();

b.close();

} catch (IOException ex) {}

} catch (FileNotFoundException ex) {

}

else {return;}

}

});

fileMenu.addSeparator(); //加分隔线

// 设置退出菜单项

JMenuItem exitItem = new JMenuItem( "退出(X)" );

exitItem.setMnemonic( 'x' );

fileMenu.add( exitItem );

exitItem.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )

{

System.exit( 0 );

}

}

);

bar.add( fileMenu );

//剪切菜单选项

JMenuItem cutItem = new JMenuItem( "剪切(T)" );

cutItem.setMnemonic( 'T' );

editMenu.add( cutItem );

cutItem.addActionListener(

new ActionListener(){

public void actionPerformed( ActionEvent event ){

selectText = displayText.getSelectedText();//取得选定的文本

int start = displayText.getSelectionStart();//选定的文本的开始位置

int end = displayText.getSelectionEnd();//选定的文本的结

displayText.replaceRange("", start, end);/*用指定替换文本替换指定

开始位置与结束位置之间的文本。

这里指定替换文本为空,即为剪切了文本*/ }

}

);

//复制菜单选项

JMenuItem copyItem = new JMenuItem( "复制(C)" );

copyItem.setMnemonic( 'C' );

editMenu.add( copyItem );

copyItem.addActionListener(

new ActionListener(){

public void actionPerformed( ActionEvent event ){

selectText = displayText.getSelectedText();//获得选中的内容,并保存在selectText里

}

}

);

//粘贴的实现

JMenuItem pasteItem = new JMenuItem( "粘贴(P)" );

pasteItem.setMnemonic( 'P' );

editMenu.add( pasteItem );

pasteItem.addActionListener(

new ActionListener(){

public void actionPerformed( ActionEvent event ){

int position = displayText.getCaretPosition();//获得鼠标当前位置

displayText.insert( selectText,position );//插入到鼠标当前位置

}

文本编辑器c++实验报告附源代码

四川大学软件学院 实验报告 课程名称数据结构实验课时8 实验项目文本编辑器实验时间12到14周实验目的了解c++类的封装和KMP算法。 实验环境 Windows平台 VC6.0++ 实验内容(算法、程序、步骤和方法) 部分函数创建思想: 创建过程如下: a、定义LinkList指针变量*temp: LinkList *temp; b、定义文本输入变量ch,记录文本行数变量j,记录每行字符数变量i; c、申请动态存储空间:head->next=(LinkList *)malloc(sizeof(LinkList)); d、首行头指针的前驱指针为空:head->pre=NULL; 首行指针:temp=head->next; 首行指针的前驱指针也为空:temp->pre=NULL; 定义没输入字符时文章长度为0:temp->length=0; 初始化为字符串结束标志,防止出现乱码:for(i=0;i<80;i++) temp->data[i]='\0'; e、利用循环进行文本输入 for(j=0;jdata[i]=ch; //给temp指向的行赋值 ···· temp->length++;//行中字符长度加1 if(ch=='#') {NUM=j; break; //文章结束时,Num来记录整个文章的行数 }}} 在字符输入的过程中,如果在单行输入的字符超过了80个字符, 则需要以下操作: 输入字符数大于80,重新分配空间建立下一行 temp->next=(LinkList *)malloc(sizeof(LinkList)) ;

几种常用网页文本编辑器总结

文本编辑器应用总结 一.lhgeditor文本编辑器 lhgeditor组件文件结构: 1. lhgeditor.js:组件的核心JS文件 2. lhgeditor.css:组件的样式表文件 3. images:组件所需的图片都在此文件夹中 以上三个文件为组件所必须的三个文件,组件包中其它以“_”开头的文件为示例的演示文件,实际使用中不需要这些文件。当然框架核心文件lhgcore.js是每个组件都必须用到的文件,记得加载组件前先要加载此文件。 lhgeditor组件使用说明: 1. 在调用组件的页面加载lhgcore.j s和lhgeditor.js两个文件。 2. 在window.onload函数里加入J.editor.add(编辑器的id).init(); 例:

二.nicEdit文本编辑器