java plaindocument_文本编辑器的设计与实现(java)

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

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

******************-------main函数-----------------------*****************************

public class ex

{

public static void

main(String[] args)

{

new Notepad();

}

}

****************---------主要实现框架函数Notepad-----------***************************

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

//import java.awt.datatransfer.Clipboard;

import java.awt.print.PageFormat;

import java.awt.print.Printable;

import java.awt.print.PrinterException;

import java.awt.print.PrinterJob;

import javax.swing.*;

import javax.swing.filechooser.FileNameExtensionFilter;

import javax.swing.undo.UndoManager;

//import javax.swing.text.PlainDocument;public class Notepad extends JFrame

implements ActionListener ,MouseListener

{private static final long

serialVersionUID = 1L;

private File file;

private JTextArea text;

private JMenu menu[];

private JFileChooser fchooser;

private PrinterJob prtMe;

private JCheckBoxMenuItem checkboxmenuitem;

private JPopupMenu popupmenu;

private UndoManager myundo = new

UndoManager();

//UndoManager 管理 UndoableEdit

列表,提供撤消或恢复适当编辑的方法。

//FindJFrame findnext;

//Clipboard

clipbd=getToolkit().getSystemClipboard();public Notepad()

{

super("Notepad");

Dimension dim =

getToolkit().getScreenSize(); //获得屏幕分辨率

this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2);//窗口大小一半、居中

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

text = new JTextArea("Welcom");

text.setLineWrap(true); //JTextArea默认下可以换行

text.getDocument().addUndoableEditListener(myundo);

this.getContentPane().add(new JScrollPane(text)); //JTextField加滚动条

JMenuBar menubar = new

JMenuBar(); //加菜单栏

this.setJMenuBar(menubar);

String[] menustr = {"文件","编辑","格式","查看","帮助"};

menu = new JMenu[menustr.length];

for(int i=0;i

{

menu[i] = new JMenu(menustr[i]);

menubar.add(menu[i]);

}

Filemenu();

Editmenu();

Formatmenu();

watchmenu();

helpmenu();

//加弹出菜单

text.addMouseListener(this); //JTextField中加鼠标监听器

mypopupmenu();

text.add(popupmenu); //把弹出菜单加在JTextField中

//加工具栏

JToolBar toolbar=new JToolBar();

this.getContentPane().add(toolbar,"North");

JButton bcreatfile =new JButton("新建");

bcreatfile.addActionListener(this);

toolbar.add(bcreatfile);

JButton bopen=new JButton("打开",new ImageIcon("open.gif"));

bopen.addActionListener(this);

toolbar.add(bopen);

JButton bsave=new JButton("保存",new ImageIcon("save.gif"));

bsave.addActionListener(this);

toolbar.add(bsave);

JButton bundo=new JButton("撤消");

bundo.addActionListener(this);

toolbar.add(bundo);

JButton bredo=new JButton("恢复");

bredo.addActionListener(this);

toolbar.add(bredo);

this.setVisible(true);

this.file=null;

this.fchooser=new JFileChooser(new

File(".","")); //文件对话框的初始路径是当前路径

this.fchooser.setFileFilter(new

FileNameExtensionFilter("文本文件(*.txt)","txt")); //设置文件过滤器

prtMe = PrinterJob.getPrinterJob();

//PrinterJob

类是控制打印的主要类。应用程序调用此类中的方法设置作业、(可选地)调用与用户的打印对话框,然后打印作业的页面。

}public Notepad(File

file) {

this();

if(file!=null)

{

this.file=file;

this.text.setText(this.file.getName());

this.setTitle(this.file.getName());

}

} public Notepad(String filename)

{

this(new File(filename));

} private

void mypopupmenu()

{

popupmenu=new

JPopupMenu();

String

menuitemstr[]={"撤消","恢复","剪切","复制","粘贴","全选","字体"};

JMenuItem popupmenuitem[]=new

JMenuItem[menuitemstr.length];

for(int

i=0;i

{

popupmenuitem[i]=new

JMenuItem(menuitemstr[i]);

if(i==1||i==4||i==5)

popupmenu.addSeparator();

popupmenu.add(popupmenuitem[i]);

popupmenuitem[i].addActionListener(this); }

//为弹出菜单中的恢复、剪切、复制、粘贴、全选 增加快捷方式

popupmenuitem[2].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));

popupmenuitem[3].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));

popupmenuitem[4].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));

popupmenuitem[5].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));

}private void Filemenu()

{

String menuitemstr[] = {"新建"

,"打开","保存","另存为","页面设置","打印","退出"};

JMenuItem menuitem[]=new

JMenuItem[menuitemstr.length];

for(int

i=0;i

{

menuitem[i]=

new JMenuItem(menuitemstr[i]);

menuitem[i].addActionListener(thi

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值