文本编辑软件

来源:互联网 发布:网络推广威客 编辑:程序博客网 时间:2024/05/21 06:14

利用6个学时的时间完成编写一个文本编辑软件,要求:

1、具有菜单功能,包括如下菜单:

文件:新建、打开、保存、退出

编辑:复制、粘贴、查找、替换

帮助:关于

如图所示:


2、刚运行时默认是新建状态,标题栏可以显示为“新建文本文件”,可以直接在文本区(JTextArea)中输入文字内容,单击保存菜单时要做判断是否保存过,如果还没有保存过则提示输入文件名,然后保存为用户输入的文件名,同时窗口的标题栏显示为该文件名;如果不是第一次保存,即已经保存过一次,则直接保存到原来的文件名,标题栏仍然显示为原来的文件名。

3、单击打开菜单时,弹出打开文件对话框,要用户选择一个文本文件,然后把文件内容读取并显示到文本编辑区(用JTextArea实现),单击保存时,不用再提示输入文件名,直接保存到原来的打开时的文件名,文件名要显示到标题栏(同时文件名应该保存到属性变量中)

 

4、单击保存菜单时,要判断是否是第一次新建或默认运行时的状态,如果是,则提示输入文件名,然后保存到文件中去;否则,直接保存到原来的文件名里即可。

5、单击退出菜单,直接退出系统。

6、单击复制菜单时,将选中的文本复制到粘贴板。

7、单击粘贴菜单时,将粘贴板中的内容复制到光标所在的文本位置。

8、单击查找菜单时,提示输入要查找的内容,并把查找到的内容选中。

9、单击替换菜单时,提示输入要替换成的内容,并进行替换(只替换第一个找到的结果即可,其他的可以多次替换来完成)

10、单击关于菜单时,显示一个作者和版权信息即可(作者是你本人)。

import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JTextArea;public class TextEdit extends JFrame{JTextArea text;Container c;String filename;String filepath;JMenuBar jmb;JMenu file;JMenu edit;JMenu help;JMenuItem build;JMenuItem open;JMenuItem save;JMenuItem exit;JMenuItem copy;JMenuItem paste;JMenuItem find;JMenuItem replace;JMenuItem about;public void init(){c=this.getContentPane();c.setLayout(new BorderLayout());jmb=new JMenuBar();file=new JMenu("文件");edit=new JMenu("编辑");help=new JMenu("帮助");build=new JMenuItem("新建");build.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){text.setText("");}});open=new JMenuItem("打开");open.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){JFileChooser fc=new JFileChooser();int diablog=fc.showOpenDialog(open);if(diablog==JFileChooser.APPROVE_OPTION){filename=fc.getSelectedFile().getName();filepath=fc.getCurrentDirectory().toString();try { File file=new File(filepath,filename); BufferedReader bfReader=new BufferedReader(new FileReader(file));String line;try {line = bfReader.readLine();while(line!=null){text.append(line+"\n");line=bfReader.readLine();}bfReader.close();} catch (IOException e1) {e1.printStackTrace();}} catch (FileNotFoundException e1) {e1.printStackTrace();}}}});save=new JMenuItem("保存");save.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){JFileChooser jfc=new JFileChooser();int diablog=jfc.showOpenDialog(open);if(diablog==JFileChooser.APPROVE_OPTION){filename=jfc.getSelectedFile().getName();if(filename.equals(""))System.out.println("文件名不能为空!");else{filepath=jfc.getCurrentDirectory().toString();try {FileWriter fwriter=new FileWriter(filepath+"/"+filename);fwriter.write(text.getText());fwriter.close();} catch (IOException e1) {e1.printStackTrace();}  }}else{try {FileWriter fwriter=new FileWriter(filepath+"/"+filename);fwriter.write(text.getText());fwriter.close();} catch (IOException e1) {e1.printStackTrace();}}}});exit=new JMenuItem("退出");exit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.exit(0);}});copy=new JMenuItem("复制");copy.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){text.copy();}});paste=new JMenuItem("粘贴");paste.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){text.paste();}});find=new JMenuItem("查找");find.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String input=JOptionPane.showInputDialog("请输入要查找的内容");String tmp=text.getText();int start=tmp.indexOf(input);int end=start+input.length();text.setSelectionStart(start);text.setSelectionEnd(end);}});replace=new JMenuItem("替换");replace.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String input=JOptionPane.showInputDialog("请输入要替换的内容");text.replaceRange(input,text.getSelectionStart(),text.getSelectionEnd());}});about=new JMenuItem("关于");about.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){JOptionPane.showConfirmDialog(about,"李鸿超");}});c.add(jmb,BorderLayout.NORTH);file.add(build);file.add(open);file.add(save);file.add(exit);edit.add(copy);edit.add(paste);edit.add(find);edit.add(replace);help.add(about);jmb.add(file);jmb.add(edit);jmb.add(help);text=new JTextArea(200,200);c.add(text);this.setSize(500,500);this.setVisible(true);}public TextEdit(String s){super(s);init();}public static void main(String[] args) {TextEdit te=new TextEdit("My FirstText!");}}


0 0
原创粉丝点击