简单的文本编辑器Java代码 简单的文本编辑器java代码

求JAVA编写的文本编辑器。

import java.awt.*;

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站制作、网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的高邑网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

import java.awt.event.*;

import java.io.*;

public class Notepad /*implements ActionListener , MouseListener , MouseMotionListener , WindowListener , ItemListener , KeyListener, TextListener */

{

//成员变量

private Frame mainFrame;//主框架

private MenuBar mb ; //菜单条

private Menu mFile , mEdit , mFormat , mHelp ; //菜单:文件,编辑,格式,帮助

private MenuItem miNew , miOpen , miSave , miSaveAs , miExit ;//文件菜单项:新建,打开,保存,另存为,退出

private MenuItem miCut , miCopy , miPaste , miDelete ;//编辑菜单项:剪切,复制,粘贴,删除

private MenuItem miFont , miLowtoCapital, miCapitaltoLow ,miEncrypt , miDisencrypt;//格式菜单项:字体

private MenuItem miAboutNotepad;//帮助菜单项:关于记事本

private TextArea ta;//文本区

private String tempString;//临时字符串,用于存储需要复制粘贴的字符串

private boolean textValueChanged = false;

private int id_font ;//字体

String fileName = "";//上次保存后的文件名和地址

//构造函数

public Notepad(){

//框架

mainFrame = new Frame ("Notepad v0.99 by Launching");

mb = new MenuBar ();

ta = new TextArea (30 ,60);

ta.setFont( new Font ( "Times New Rome" , Font.PLAIN , 15));

ta.setBackground(new Color(0 , 250 , 200));

//菜单条

mFile = new Menu ( "File");

mEdit = new Menu ( "Edit");

mFormat = new Menu ("Format");

mHelp = new Menu ("Help");

//"文件"

miNew = new MenuItem ("New");

miOpen = new MenuItem ("Open");

miSave = new MenuItem ("Save");

miSaveAs = new MenuItem ("Save as");

miExit = new MenuItem ("Exit");

//"编辑"

miCut = new MenuItem ("Cut");

miCopy = new MenuItem ("Copy");

miPaste = new MenuItem ("Paste");

miDelete = new MenuItem ("Delete");

//"格式"

miFont = new MenuItem ("Font");

miLowtoCapital = new MenuItem("Low to Capital");

miCapitaltoLow = new MenuItem("Capital to Low");

miEncrypt = new MenuItem("Encrypt");

miDisencrypt = new MenuItem("Disencrypt");

//"帮助"

miAboutNotepad = new MenuItem ("About Notepad");

//添加文件菜单项

mFile.add(miNew);

mFile.add(miOpen);

mFile.add(miSave);

mFile.add(miSaveAs);

mFile.add(miExit);

//添加编辑菜单项

mEdit.add(miCut);

mEdit.add(miCopy);

mEdit.add(miPaste);

mEdit.add(miDelete);

//添加格式菜单项

mFormat.add(miFont);

mFormat.add(miLowtoCapital);

mFormat.add(miCapitaltoLow);

mFormat.add(miEncrypt);

mFormat.add(miDisencrypt);

//添加帮助菜单项

mHelp.add(miAboutNotepad);

//菜单条添加菜单

mb.add(mFile);

mb.add(mEdit);

mb.add(mFormat);

mb.add(mHelp);

//框架添加菜单条

mainFrame.setMenuBar( mb );

//初始字符串赋为空

tempString = "";

//添加文本区

mainFrame.add(ta, BorderLayout.CENTER);

mainFrame.setSize(800 , 500);

mainFrame.setLocation( 100 ,100);// 起始位置

mainFrame.setResizable(true);//不可更改大小

mainFrame.setVisible(true);

//mainFrame.pack();

//////////////////////////////////////////////////////////////////////////////////

////////////////////////////////增加监视器////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////

//主框架

mainFrame.addWindowListener(new WindowAdapter (){ //关闭窗口

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

//文本区

ta.addKeyListener( new KeyAdapter(){

public void KeyTyped(KeyEvent e){

textValueChanged = true ; //键盘按键按下即导致文本修改

}

});

////////////////"文件"菜单://////////////////////

//新建

miNew.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

ta.replaceRange("", 0 , ta.getText().length()) ;//清空文本区的内容

fileName = "";//文件名清空

}

});

//打开

miOpen.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e) {

FileDialog d=new FileDialog(mainFrame , "open file" , FileDialog.LOAD );//打开文件对话框

d.addWindowListener( new WindowAdapter(){ //关闭文件对话框窗口

public void windowClosing(WindowEvent ee){

System.exit(0);

}

});

d.setVisible(true);

File f = new File( d.getDirectory()+d.getFile() ); //建立新文件

fileName = d.getDirectory()+d.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);

}

catch( IOException ie){

System.out.println("IO error");

System.exit(0);

}

String s =new String (ch);

ta.setText(s);//设置文本区为所打开文件的内容

}

});

//保存

miSave.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e) {

if( fileName.equals("") ){ //如果文件没有被保存过,即文件名为空

FileDialog d=new FileDialog(mainFrame , "save file" , FileDialog.SAVE );//保存文件对话框

d.addWindowListener( new WindowAdapter(){ //关闭文件对话框窗口

public void windowClosing(WindowEvent ee){

System.exit(0);

}

});

d.setVisible(true);

String s = ta.getText();//得到所输入的文本内容

try//异常处理

{

File f = new File( d.getDirectory()+d.getFile());//新建文件

fileName = d.getDirectory()+d.getFile();//得到文件名

BufferedWriter bw = new BufferedWriter( new FileWriter (f));//输入到文件中

bw.write(s , 0 , s.length());

bw.close();

}

catch(FileNotFoundException fe_){

System.out.println("file not found");

System.exit(0);

}

catch( IOException ie_)

{

System.out.println(" IO error");

System.exit(0);

}

}

else //如果文件已经保存过

{

String s = ta.getText();//得到所输入的文本内容

try//异常处理

{

File f = new File( fileName );//新建文件

BufferedWriter bw = new BufferedWriter( new FileWriter (f));//输入到文件中

bw.write(s , 0 , s.length());

bw.close();

}

catch(FileNotFoundException fe_){

System.out.println("file not found");

System.exit(0);

}

catch( IOException ie_)

{

System.out.println(" IO error");

System.exit(0);

}

}

}

});

//另存为

miSaveAs.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e) {

FileDialog d=new FileDialog(mainFrame , "save file" , FileDialog.SAVE );//保存文件对话框

d.addWindowListener( new WindowAdapter(){ //关闭文件对话框窗口

public void windowClosing(WindowEvent ee){

System.exit(0);

}

});

d.setVisible(true);

String s = ta.getText();//得到所输入的文本内容

try//异常处理

{

File f = new File( d.getDirectory()+d.getFile());//新建文件

BufferedWriter bw = new BufferedWriter( new FileWriter (f));//输入到文件中

bw.write(s , 0 , s.length());

bw.close();

}

catch(FileNotFoundException fe_){

System.out.println("file not found");

System.exit(0);

}

catch( IOException ie_)

{

System.out.println(" IO error");

System.exit(0);

}

}

});

//退出

miExit.addActionListener( new ActionListener(){ ///退出程序

public void actionPerformed(ActionEvent e){

System.exit(0);

}

});

////////////////"编辑"菜单:////////////////////

//剪切

miCut.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

tempString = ta.getSelectedText(); ///得到要复制的内容,暂存在tempString中

StringBuffer tmp = new StringBuffer ( ta.getText());//临时存储文本

int start = ta.getSelectionStart(); //得到要删除的字符串的起始位置

int len = ta.getSelectedText().length(); //得到要删除的字符串的长度

tmp.delete( start , start+len); ///删除所选中的字符串

ta.setText(tmp.toString());//用新文本设置原文本

}

});

//复制

miCopy.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

tempString = ta.getSelectedText(); ///得到要复制的内容,暂存在tempString中

}

});

//粘贴

miPaste.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

StringBuffer tmp = new StringBuffer ( ta.getText());//临时存储文本

int start = ta.getSelectionStart(); //得到要粘贴的位置

tmp.insert(start , tempString);//查入要粘贴的内容

ta.setText(tmp.toString());//用新文本设置原文本

}

});

//删除

miDelete.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

StringBuffer tmp = new StringBuffer ( ta.getText());//临时存储文本

int start = ta.getSelectionStart(); //得到要删除的字符串的起始位置

int len = ta.getSelectedText().length(); //得到要删除的字符串的长度

tmp.delete( start , start+len); ///删除所选中的字符串

ta.setText(tmp.toString());//用新文本设置原文本

}

});

////////////////"格式"菜单:////////////////////

//字体

miFont.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

final Dialog d = new Dialog ( mainFrame , "Font");//新建对话框

d.setLocation( 250 ,250);// 起始位置

d.setLayout( new BorderLayout());//表格布局

//////////////////////////上部分面板

Label l_font = new Label ("font");//font标签

Panel p_1 = new Panel();

p_1.add(l_font);

p_1.setVisible(true);

//////////////////////////中部分面板

List font_list = new List (6 , false);//字体列表

//添加字体项目

font_list.add("Plain");///普通字体

font_list.add("Bold"); ///粗体

font_list.add("Italic");//斜体

font_list.addItemListener( new MyItemListener_font() ); //字体增加监视器

Panel p_2 = new Panel();

p_2.add(font_list);

p_2.setVisible(true);

//////////////////////////下部分面板

Button ok = new Button ("OK");

ok.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

d.dispose();

}

});

ok.setSize( new Dimension (20 , 5) );

Panel p_3 = new Panel();//下部分面板

p_3.add(ok);

p_3.setVisible(true);

//添加三个面板

d.add(p_1 , BorderLayout.NORTH);

d.add(p_2 , BorderLayout.CENTER);

d.add(p_3 , BorderLayout.SOUTH);

d.pack();

d.addWindowListener( new WindowAdapter(){ //关闭对话框窗口

public void windowClosing(WindowEvent ee){

d.dispose();

}

});

d.setVisible(true);

}

});

//小写字母转大写

miLowtoCapital.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

String s = ta.getText();//得到所输入的文本内容

StringBuffer temp = new StringBuffer("");

for(int i = 0 ; is.length() ; i++){

if((int)s.charAt(i)=97 (int)s.charAt(i)=122 ){

temp.append((char)((int)s.charAt(i)-32));

}

else

temp.append(s.charAt(i));

}

s = new String(temp);

ta.setText(s);

}

});

//大写字母转小写

miCapitaltoLow.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

String s = ta.getText();//得到所输入的文本内容

StringBuffer temp = new StringBuffer("");

for(int i = 0 ; is.length() ; i++){

if((int)s.charAt(i)=65 (int)s.charAt(i)=90 ){

temp.append((char)((int)s.charAt(i)+32));

}

else

temp.append(s.charAt(i));

}

s = new String(temp);

ta.setText(s);

}

});

//加密

miEncrypt.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

String s = ta.getText();//得到所输入的文本内容

StringBuffer temp = new StringBuffer("");

for(int i = 0 ; is.length() ; i++){

if(s.charAt(i)=40 s.charAt(i)=125){

if(i%2==0){

temp.append((char)(s.charAt(i) + 1 ));

}

else

temp.append((char)(s.charAt(i) - 1 ));

}

else

temp.append(s.charAt(i));

}

s = new String(temp);

ta.setText(s);

}

});

//解密

miDisencrypt.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

String s = ta.getText();//得到所输入的文本内容

StringBuffer temp = new StringBuffer("");

for(int i = 0 ; is.length() ; i++){

if(s.charAt(i)=40 s.charAt(i)=125){

if(i%2==0){

temp.append((char)(s.charAt(i) - 1 ));

}

else

temp.append((char)(s.charAt(i) + 1 ));

}

else

temp.append(s.charAt(i));

}

s = new String(temp);

ta.setText(s);

}

});

////////////////"帮助"菜单:////////////////////

//关于记事本

miAboutNotepad.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e){

final Dialog d = new Dialog ( mainFrame , "AboutNotepad");//新建对话框

TextArea t = new TextArea("\nwelcome to use Notepad " + "\n\n" + "Copyright@Launching " + "\n\n" + "free software" + "\n\n" + "v0.99");//添加标签

t.setSize( new Dimension ( 5 , 5));

t.setEditable(false);

d.setResizable(false);//不可调整大小

d.add(t);

d.pack();

d.addWindowListener( new WindowAdapter(){ //关闭对话框窗口

public void windowClosing(WindowEvent ee){

d.dispose();

}

});

d.setLocation( 100 ,250);// 起始位置

d.setVisible(true);

}

});

}

class MyItemListener_font implements ItemListener { //字体监听器

public void itemStateChanged(ItemEvent e) {

id_font = ((java.awt.List)e.getSource()).getSelectedIndex();

switch( id_font){

case 0:{

ta.setFont(new Font("Times New Roman", Font.PLAIN ,ta.getFont().getSize()) );//普通文字

break;

}

case 1:{

ta.setFont(new Font("Times New Roman" , Font.BOLD ,ta.getFont().getSize()) );//粗体文字

break;

}

case 2:{

ta.setFont(new Font("Times New Roman" , Font.ITALIC ,ta.getFont().getSize()) );//斜体文字

break;

}

}

}

}

/////////////////////////////////////////主函数///////////////////////////////////////////////////

public static void main(String arg[]){

Notepad test = new Notepad(); ///创建记事本

}

//////////////////////////////////////////////////////////////////////////////////////////////////

}

求一个用Java编写的文本编辑器代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

public class MyTextEditor extends JFrame implements ActionListener,ItemListener,MouseListener

{

private File file;

private JTextArea textarea;

private JRadioButtonMenuItem rbmi_red,rbmi_blue,rbmi_green;

private JMenuItem menuitem_copy,menuitem_cut,menuitem_paste,menuitem_seek;

private JMenuItem menuitem_song,menuitem_fang,menuitem_kai;//字体变量

private JMenuItem menuitem_normal,menuitem_bold,menuitem_italic;//字形变量

private JMenuItem menuitem_20,menuitem_30,menuitem_40;//字号变量

private JMenuItem menuitem_exit,menuitem_infor;

private JPopupMenu popupmenu;

private JMenuItem menuitem_red,menuitem_green,menuitem_blue;

private JDialog dialog,dialog1;

private JButton button_seek;

private JTextField textfield_seek;

private JLabel label_seek,label_infor;

String seek;

public MyTextEditor()

{

super("文本编辑器");

this.setSize(400,300);

this.setLocation(360,300);

this.setDefaultCloseOperation(HIDE_ON_CLOSE);

Container ss=this.getContentPane();

this.textarea = new JTextArea();

JScrollPane dd=new JScrollPane(textarea);

ss.add(dd);

textarea.addMouseListener(this);

this.addMenu();

this.setVisible(true);

this.Dialog();

this.Dialog1();

this.file = null;

}

public MyTextEditor(String filename)

{

this();

if (filename!=null)

{

this.file = new File(filename);

this.setTitle(filename);

this.textarea.setText(this.readFromFile());

}

}

public MyTextEditor(File file)

{

this();

if (file!=null)

{

this.file = file;

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

this.textarea.setText(this.readFromFile());

}

}

public void Dialog() //建立对话框的方法

{

dialog=new JDialog(this,"查找",true);

dialog.setLayout(new FlowLayout());

dialog.setSize(200,90);

label_seek=new JLabel("查找内容");

dialog.add(label_seek);

textfield_seek=new JTextField(10);

dialog.add(textfield_seek);

button_seek=new JButton("查找");

dialog.add(button_seek);

button_seek.addActionListener(this);

}

public void Dialog1()

{

dialog1=new JDialog(this,"专利",true);

dialog1.setLayout(new FlowLayout());

dialog1.setSize(200,100);

label_infor=new JLabel("刘同虎制作");

dialog1.add(label_infor);

}

public void addMenu()

{

JMenuBar menubar = new JMenuBar();

this.setJMenuBar(menubar);

JMenu menu_file = new JMenu("文件"); //文件菜单

menubar.add(menu_file);

JMenuItem menuitem_open = new JMenuItem("打开");

menu_file.add(menuitem_open);

menuitem_open.addActionListener(this);

JMenuItem menuitem_save = new JMenuItem("保存");

menu_file.add(menuitem_save);

menuitem_save.addActionListener(this);

JMenuItem menuitem_saveas = new JMenuItem("另存为");

menu_file.add(menuitem_saveas);

menuitem_saveas.addActionListener(this);

menuitem_exit=new JMenuItem("退出" );

menu_file.add(menuitem_exit);

menuitem_exit.addActionListener(this);

menuitem_infor=new JMenuItem("信息");

menu_file.add(menuitem_infor);

menuitem_infor.addActionListener(this);

JMenu menu_editor=new JMenu("编辑");//编辑菜单

menubar.add(menu_editor);

menuitem_seek=new JMenuItem("查找");

menu_editor.add(menuitem_seek);

menuitem_seek.addActionListener(this);

menuitem_copy=new JMenuItem("复制");

menuitem_copy.addActionListener(this);

menu_editor.add(menuitem_copy);

menuitem_cut=new JMenuItem("剪切");

menu_editor.add(menuitem_cut);

menuitem_cut.addActionListener(this);

menuitem_paste=new JMenuItem("粘贴");

menu_editor.add(menuitem_paste);

menuitem_paste.addActionListener(this);

JMenuItem menu_color=new JMenu("颜色");//颜色菜单

menu_editor.add(menu_color);

ButtonGroup buttongroup=new ButtonGroup();

rbmi_red=new JRadioButtonMenuItem("红",true);

buttongroup.add(rbmi_red);

menu_color.add(rbmi_red);

rbmi_red.addItemListener(this);

rbmi_blue=new JRadioButtonMenuItem("蓝",true);

buttongroup.add(rbmi_blue);

menu_color.add(rbmi_blue);

rbmi_blue.addItemListener(this);

rbmi_green=new JRadioButtonMenuItem("绿",true);

buttongroup.add(rbmi_green);

menu_color.add(rbmi_green);

rbmi_green.addItemListener(this);

JMenu menu_font=new JMenu("设置字体");//设置字体菜单

menubar.add(menu_font);

menuitem_song=new JMenuItem("宋体");

menu_font.add(menuitem_song);

menuitem_song.addActionListener(this);

menuitem_fang=new JMenuItem("仿宋");

menu_font.add(menuitem_fang);

menuitem_fang.addActionListener(this);

menuitem_kai=new JMenuItem("楷体");

menu_font.add(menuitem_kai);

menuitem_kai.addActionListener(this);

JMenu menu_style=new JMenu("设置字形");//设置字形菜单

menubar.add(menu_style);

menuitem_bold=new JMenuItem("粗体");

menu_style.add(menuitem_bold);

menuitem_bold.addActionListener(this);

menuitem_italic=new JMenuItem("斜体");

menu_style.add(menuitem_italic);

menuitem_italic.addActionListener(this);

JMenu menu_size=new JMenu("设置字号"); //设置字号菜单

menubar.add(menu_size);

menuitem_20=new JMenuItem("20");

menu_size.add(menuitem_20);

menuitem_20.addActionListener(this);

menuitem_30=new JMenuItem("30");

menu_size.add(menuitem_30);

menuitem_30.addActionListener(this);

menuitem_40=new JMenuItem("40");

menu_size.add(menuitem_40);

menuitem_40.addActionListener(this);

popupmenu=new JPopupMenu(); //快捷菜单

JMenuItem menuitem_red=new JMenuItem("红色");

popupmenu.add(menuitem_red);

menuitem_red.addActionListener(this);

JMenuItem menuitem_green=new JMenuItem("绿色");

popupmenu.add(menuitem_green);

menuitem_green.addActionListener(this);

menuitem_blue=new JMenuItem("蓝色");

popupmenu.add(menuitem_blue);

menuitem_blue.addActionListener(this);

textarea.add(popupmenu); //向文本区内添加快捷菜单

}

public void writeToFile(String lines) //写文件

{

try

{

FileWriter fout = new FileWriter(this.file);

fout.write(lines+"\r\n");

fout.close();

}

catch (IOException ioex)

{

return;

}

}

public String readFromFile() //读文件

{

try

{

FileReader fin = new FileReader(this.file);

BufferedReader bin = new BufferedReader(fin);

String aline="", lines="";

do

{

aline = bin.readLine();

if (aline!=null)

lines += aline + "\r\n";

} while (aline!=null);

bin.close();

fin.close();

return lines;

}

catch (IOException ioex)

{

return null;

}

}

如何用java编写一个简单的文本编辑器?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

public class f1 extends Frame implements ActionListener

{

private MenuBar menubar=new MenuBar();

private Menu filemenu=new Menu("文件");

private Menu editmenu=new Menu("编辑");

private Menu formmenu=new Menu("格式");

private MenuItem[] itemf=new MenuItem[4];

private MenuItem[] iteme=new MenuItem[6];

private MenuItem[] items=new MenuItem[2];

private TextArea tf=new TextArea();

public int a=0,b=0,c=0,style=Font.PLAIN,size=15;

public String s1="red:"+a+" "+"green:"+b+" "+"blue"+c,

s2="宋体";

public String[] sz1={"10","16","24","30","32","36"},

sz2={"宋体","黑体","幼圆","隶书","行楷","Arial","Georgia"},

sz3={"粗体","倾斜","常规","粗斜"};

JDialog dialog=new JDialog(this,"字体",true);

Container cp=dialog.getContentPane();

JLabel[] lb=new JLabel[8];

JLabel lb1=new JLabel(s1,JLabel.LEFT);

JButton b1=new JButton("确定"),

b2=new JButton("取消");

JComboBox jc1=new JComboBox(),

jc2=new JComboBox(),

jc3=new JComboBox();

JScrollBar jb1=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);

JScrollBar jb2=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);

JScrollBar jb3=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);


当前文章:简单的文本编辑器Java代码 简单的文本编辑器java代码
浏览路径:http://lszwz.com/article/ddgcjjc.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款