「java实现文本编辑器」编程用的文本编辑器

博主:adminadmin 2023-01-23 17:09:07 253

本篇文章给大家谈谈java实现文本编辑器,以及编程用的文本编辑器对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java文本编辑器 查找与替换功能如何实现?

最简单的就是将文本内容作使用String处理

只替换一次 : String.replace("需要替换的字符串","替换的字符串")

替换所有匹配字符 : String.replaceAll("需要替换的字符串","替换的字符串")

replcaeAll支持使用正则表达式。

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

import java.awt.*;\x0d\x0aimport java.awt.event.*;\x0d\x0aimport javax.swing.*;\x0d\x0aimport java.io.*;\x0d\x0a\x0d\x0apublic class f1 extends Frame implements ActionListener\x0d\x0a{\x0d\x0aprivate MenuBar menubar=new MenuBar();\x0d\x0aprivate Menu filemenu=new Menu("文件");\x0d\x0aprivate Menu editmenu=new Menu("编辑");\x0d\x0aprivate Menu formmenu=new Menu("格式");\x0d\x0aprivate MenuItem[] itemf=new MenuItem[4];\x0d\x0aprivate MenuItem[] iteme=new MenuItem[6];\x0d\x0aprivate MenuItem[] items=new MenuItem[2];\x0d\x0aprivate TextArea tf=new TextArea();\x0d\x0a\x0d\x0apublic int a=0,b=0,c=0,style=Font.PLAIN,size=15;\x0d\x0apublic String s1="red:"+a+" "+"green:"+b+" "+"blue"+c,\x0d\x0as2="宋体";\x0d\x0a\x0d\x0apublic String[] sz1={"10","16","24","30","32","36"},\x0d\x0asz2={"宋体","黑体","幼圆","隶书","行楷","Arial","Georgia"},\x0d\x0asz3={"粗体","倾斜","常规","粗斜"};\x0d\x0a\x0d\x0aJDialog dialog=new JDialog(this,"字体",true);\x0d\x0aContainer cp=dialog.getContentPane();\x0d\x0aJLabel[] lb=new JLabel[8]; \x0d\x0aJLabel lb1=new JLabel(s1,JLabel.LEFT);\x0d\x0aJButton b1=new JButton("确定"),\x0d\x0ab2=new JButton("取消");\x0d\x0aJComboBox jc1=new JComboBox(),\x0d\x0ajc2=new JComboBox(),\x0d\x0ajc3=new JComboBox();\x0d\x0aJScrollBar jb1=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);\x0d\x0aJScrollBar jb2=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);\x0d\x0aJScrollBar jb3=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);

用java编写一个简单的多文档文本编辑器

JDK自带一个简单的Notepad,你可以研究一下他的源代码。

在%JAVA_HOME%\demo\jfc\Notepad 目录下

JDK自带一个稍复杂的Stylepad,你可以研究一下他的源代码。

在%JAVA_HOME%\demo\jfc\Stylepad目录下

如何用JAVA实现可插入图片的文本编辑器,不胜感激~

Java Swing里面有富文本编辑器 JTextPane,不过用法比JTextArea复杂多了,

能够插入图片,设置字体属性之类了。

至于要插入Flash的话,单独的一个组件是可以做到的,建议去网上搜索一下JDIC,是Sun针对Windows开发的一个组件包,其中包括一个Web Browser,是一个组件,设置他的html内容就能显示出来,是使用IE内核的。

将flash嵌入网页的代码使用embed src=""/embed,具体的语法自己查下吧。

java必须用文本编辑器编写源文件对吗?

对的

1、编写源文件

使用一个文本编辑器(如Edit或记事本)编写源文件,不可使用非文本编辑器(如我word编辑器)。

将编写好的源文件保存起来,源文件的扩展名必须是.java。

2、编译源文件

使用Java编译器(javac.exe)编译源文件,得到字节码文件。

3、运行程序

使用Java SE 平台中的Java解释器(java.exe)来解释执行字节码文件。

求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实现文本编辑器的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于编程用的文本编辑器、java实现文本编辑器的信息别忘了在本站进行查找喔。