简单的java文本编辑器源码

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;

public class TxtEditor extends JFrame implements ActionListener{

	String file_name;
	String file_dir;
	String tempString;
	//上次保存后的文件名和地址
	String fileName = "";
	 JPanel x=new JPanel();
     JTextArea wen=new JTextArea(20,50);
     
	   JMenuItem ziti=new  JMenuItem("字体");
	   JMenuItem a=new JMenuItem("普通");
         //定义菜单项
	   JMenuItem xin=new  JMenuItem("新建");
	   JMenuItem open=new JMenuItem("打开");
	   JMenuItem save=new JMenuItem("保存 ");
	   JMenuItem lsave=new  JMenuItem("另存为");
	   JMenuItem tui=new  JMenuItem("退出");
	   
	   JMenuItem cut=new JMenuItem("剪切 ");
	   JMenuItem copy=new JMenuItem("复制");
	   JMenuItem cast=new  JMenuItem("粘贴");
	   JMenuItem delete=new  JMenuItem("删除 ");
	  
	   JMenuItem b=new JMenuItem("粗体");
	   JMenuItem c=new JMenuItem("斜体");
	TxtEditor(){
	 super ("文本编辑器       By  强凯 V1.0");
	//小小对话框
	   setBounds(250,100,700,450);
 	  setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
 	  addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent e) {
       int   option=   JOptionPane.showConfirmDialog( 
       TxtEditor.this, "你真的想退出吗... ", "系统和你对话 ",JOptionPane.YES_NO_OPTION); 
 			  
 			  if(option==JOptionPane.YES_OPTION) 
 				  if(e.getWindow()   ==   TxtEditor.this) 
 				  { 
 				         System.exit(0); 
 				         } 
 				         else 
 				         { 
 				         return; 
 				         
 				         } 
   }    
   });
      //热键设置
 	 xin.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));
 	 open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
 	 save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
 	 cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));
 	 copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));
 	 cast.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));
 
 	//定义面板
       // x.add(
        	add(new  JScrollPane (wen));//);//滚动条
         wen.setFont(new Font("楷体" , Font.PLAIN ,20));
        // wen.setBackground(Color.blue);
         // add(x);
		 
          //菜单栏的创建
		   JMenuBar cai=new JMenuBar();
		   this.setJMenuBar(cai);
		   cai.setOpaque(true);
		   JMenu jian=new JMenu("文件");
		   jian.add(xin);
		   jian.add(open);
		   jian.add(save);
		   jian.add(lsave);
		   jian.addSeparator( );
		   jian.add(tui);
		  cai.add(jian);
		  
		  JMenu bian= new JMenu("编辑  ");
		  bian.add(cut);
		  bian.add(copy);
		  bian.add(cast);
		  bian.add(delete);
		   cai.add(bian);
		   
		  
		  JMenu geshi = new JMenu ("格式");
    	  JMenu optionsMenu=new JMenu("字体");
    	  geshi.add(optionsMenu);
    	  optionsMenu.add(a);
    	  optionsMenu.add(b);
    	  optionsMenu.add(c);
		  cai.add(geshi);
		   
		   
		 //增加监听器
		 xin.addActionListener(this);
		 open.addActionListener(this);
		 save.addActionListener(this);
		 lsave.addActionListener(this);
		 tui.addActionListener(this);
		 cut.addActionListener(this);
		 copy.addActionListener(this);
		 cast.addActionListener(this);
		 delete.addActionListener(this);
		 ziti.addActionListener(this);
		 a.addActionListener(this);
		 b.addActionListener(this);
		 c.addActionListener(this);
		
		// 文本框锁定
         //this.setResizable(false);
	} 
	
	
	//重写方法
	public void actionPerformed(ActionEvent e){
		
		String actionCommand=e.getActionCommand();
		if(e.getSource()instanceof JMenu);
		{
		  if(e.getSource()==xin){
			newfile();  
		  }
	       
		  else if(e.getSource()==open){    
		    openfile();
		    }
		 else if(e.getSource()==save){
		  savefile();
		   }
		  else if(e.getSource()==lsave){
		    lsavefile();
		   }
		  else if(e.getSource()==cut){
		    cutfile();
		    }
		  else if(e.getSource()==copy){
		     copyfile();
		    }
		  else if(e.getSource()==cast){
			  castfile();
		  }
		  else if(e.getSource()==delete){
			  deletefile();
		  }
		  else if(e.getSource()==a){
			  afile();
			 }
		  else if(e.getSource()==b){
			  bfile();
		  }
		  else if(e.getSource()==c){
			  cfile();
		  }
		  else if("退出".equals(actionCommand))
			 System.exit(0);	
			
		}
		
	}
	    // 方法定义
	    public void newfile(){
	    	 
	    	savefile();
	    	wen.setText(null);
	        fileName = "";
	    	     
	    }
	    //打开
         public void openfile(){
        	 String fileName = null; 
        	 FileDialog df = new FileDialog(this,"打开文件",FileDialog.LOAD); 
        	 df.setVisible(true);
        	 //建立新文件
        	  File f = new File( df.getDirectory()+df.getFile() );
        	  //得到文件名
        	  fileName = df.getDirectory()+df.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);
        		     wen.setText(s);
                     }
         
        		  //保存
		        public void savefile(){
			        if( fileName.equals("") ){
			    	 FileDialog df = new FileDialog(this,"保存文件",FileDialog.SAVE); 
			         df.addWindowListener( new WindowAdapter(){ 
			     public void windowClosing(WindowEvent ee){
			       System.exit(0);
			      }
			     });
			     df.setVisible(true);
			     String s = wen.getText();
			    
			     try
			     {
			      File f = new File( df.getDirectory()+df.getFile());
			      fileName = df.getDirectory()+df.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 = wen.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);
			     }
			     
			     }   
			         
			    }
		
	     //另存为
        public void lsavefile(){
        	FileDialog df = new FileDialog(this,"另存为",FileDialog.SAVE);
        	df.addWindowListener( new WindowAdapter(){
        	      public void windowClosing(WindowEvent ee){
        	       System.exit(0);
        	      }
        	     });
        	     df.setVisible(true);
        	     String s = wen.getText();
        	     try
        	     {
        	      File f = new File( df.getDirectory()+df.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);
        	     }     
        	    }
        
          //剪切
           public void cutfile(){
        	
        		     tempString = wen.getSelectedText(); 
        		     StringBuffer tmp = new StringBuffer ( wen.getText());
        		     int start = wen.getSelectionStart();
        		     int len = wen.getSelectedText().length(); 
        		     tmp.delete( start , start+len); 
        		     wen.setText(tmp.toString());    
            }
	   //复制
       public void copyfile(){
    	   tempString = wen.getSelectedText(); 
       }
       
	  //粘贴
       public void castfile(){
    	   StringBuffer tmp = new StringBuffer ( wen.getText());
    	     //得到要粘贴的位置
    	     int start = wen.getSelectionStart(); 
    	     tmp.insert(start , tempString);
    	    //用新文本设置原文本 
    	     wen.setText(tmp.toString());
    	    }
       
       //删除
       public void deletefile(){
    	   StringBuffer tmp = new StringBuffer ( wen.getText());
    	   int start = wen.getSelectionStart(); 
    	   int len = wen.getSelectedText().length(); 
    	    tmp.delete( start , start+len); 
    	    wen.setText(tmp.toString());
       }
	//字体
       public void afile(){ 
    	   wen.setFont(new Font("楷体", Font.PLAIN ,wen.getFont().getSize()) );//普通文字
       }
       
       public void bfile(){ 
    	   wen.setFont(new Font("楷体" , Font.BOLD ,wen.getFont().getSize()) );//粗体文字
    	   
    	          }
       public void cfile(){
    	   wen.setFont(new Font("楷体" , Font.ITALIC ,wen.getFont().getSize()) );//斜体文字
       }
	
	public static void main(String[] args) {
		TxtEditor w=new TxtEditor();
		w.pack();
		w.setVisible(true);
		

	}

  • 9
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
原96编辑器是一个非常受欢迎的文本编辑器,它提供了丰富的功能和用户友好的界面。为了仿造它的码,我们可以使用Java编程语言来实现相似的功能和界面设计。 首先,我们需要创建一个基于Java的用户界面,并添加各种文本编辑功能,如插入文本、删除文本、复制粘贴等。我们可以利用Java的Swing库来实现这些功能,同时确保界面具有一定的美观性和易用性。接着,我们可以通过Java的输入输出流来实现打开、保存、关闭文本文件等功能,并确保能够支持多种文件格式,如.txt、.doc等。 另外,为了仿造原96编辑器的一些特色功能,我们可以使用Java的正则表达式来实现文字查找替换功能,以及添加代码高亮显示和自动缩进等特性。另外,我们还需要考虑支持多语言编码以及字体大小和颜色等个性化设置。这些功能的实现需要对Java的字符串处理和图形用户界面编程有一定的了解。 最后,为了确保仿造码的质量和稳定性,我们需要进行充分的测试和调试工作,确保程序的各项功能和性能都符合预期。因为原96编辑器码的功能非常丰富,因此仿造需要考虑的功能细节非常多,需要充分的时间和精力。 总而言之,要用Java来仿造原96编辑器码,需要我们充分理解原软件的功能和设计,并利用Java语言和相关的库来实现相应的功能和界面交互。同时也需要不断改进和完善,以达到与原软件相媲美的水平。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值