⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 texteditor.java

📁 这是一个功能较为完善的Java记事本程序
💻 JAVA
字号:
import java.lang.String;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*; //打印
import javax.swing.*;
import java.lang.String.*; 
import java.awt.datatransfer.*;//数据传送 
import javax.swing.event.*;
import java.util.*;    //日期
import java.io.*;
import java.applet.*;
public class TextEditor extends Frame implements ActionListener,ItemListener
{
	char buffer[]=new char[1024]; 
	String sou=""; 
	String des=""; 
	long souTime,desTime; 
	
	MenuBar mb=new MenuBar();
	Menu file=new Menu("文件(F)");
	Menu compilation=new Menu("编辑(E)");
	Menu form=new Menu("格式(O)");
	Menu examine=new Menu("查看(V)");
	Menu help=new Menu("帮助(H)");
	
	
	TextArea msg=new TextArea("please input the text from here",40,100);
	PopupMenu pm=new PopupMenu();
	
	Clipboard clipboard;//定义剪切板对象 
	FileDialog filedialog_save,filedialog_load;//定义文件对话框 
	JFileChooser openDialog=new JFileChooser("F:\\"); 
	PrinterJob prt=PrinterJob.getPrinterJob();  
	JLabel labelInfo=new JLabel("状态栏");
	MenuItem New=new MenuItem("新建(N) Ctrl+N"); 
	MenuItem open=new MenuItem("打开(O) Ctrl+O");
	MenuItem Save=new MenuItem("保存(S) Ctrl+S");
	MenuItem As=new MenuItem("另存为(A)...");
	MenuItem Set=new MenuItem("页面设置(U)...");
	MenuItem Print=new MenuItem("打印(P)...Ctrl+P");
	Menu Run=new Menu("运行");
	MenuItem exit=new MenuItem("退出");
	
	MenuItem attribute=new MenuItem("文档属性(I)", new MenuShortcut('I'));
	MenuItem deskShortCut=new MenuItem("创建桌面快捷方式(D)", new MenuShortcut('D'));
	CheckboxMenuItem checkbox=new CheckboxMenuItem("只读模式(R)",false);
	
	MenuItem Copy=new MenuItem("复制 Ctrl+C");
	MenuItem Cut=new MenuItem("剪切 Ctrl+X");
	MenuItem Paste=new MenuItem("粘贴 Ctrl+V");
	MenuItem Cancel=new MenuItem("撤销(U) Ctrl+Z");
	MenuItem Delete=new MenuItem("删除 (L) Del");
	MenuItem Find=new MenuItem("查找(F)... Ctrl+F");
	MenuItem FindNext=new MenuItem("查找下一个(N) F3");
	MenuItem Replace=new MenuItem("替换(R) Ctrl+H");
	MenuItem Transfer=new MenuItem("转到(G) Ctrl+G");
	MenuItem SelectAll=new MenuItem("全选(A) Ctrl+A");
	MenuItem date=new MenuItem("时间/日期(D) F5");
	CheckboxMenuItem Autolinewrap=new CheckboxMenuItem("自动换行(W)",true);
	MenuItem font =new MenuItem("字体(F)...");
	MenuItem HelpTheme=new MenuItem("帮助主题(H)...");
	MenuItem Introduction=new MenuItem("关于记事本(A)");
	MenuItem StateItem =new MenuItem("状态栏(S)");
  
   
  TextEditor() 
   {
		setTitle("新建 文本文档 - 记事本");
		setSize(500,550);
		add(msg);
		add("South",labelInfo); 
		
		mb.add(file);
		mb.add(compilation);
		mb.add(form);
		mb.add(examine);
		mb.add(help);
		
		clipboard = null; 
		clipboard = getToolkit().getSystemClipboard();//获取系统剪切板 
		
		filedialog_save=new FileDialog(this,"保存文件对话框",FileDialog.SAVE); 
		filedialog_save.setVisible(false); 
		filedialog_load=new FileDialog(this,"打开文件对话框",FileDialog.LOAD); 
		filedialog_load.setVisible(false); 
		file.add(New);
		file.add(open); 
		file.add(Save);
		file.add(As);
		file.add(Set);
		file.add(Print);
		file.addSeparator();
		file.add(Run); 
		file.add(attribute);
		file.add(deskShortCut);
		file.add(checkbox);
		file.addSeparator();
		file.add(exit);
		// 为二级菜单加入菜单项
		Run.add("新窗口(N) Alt+N");  
		Run.add("空白窗口(E) Alt+O");
		Run.add("使用默认程序打开(D) Ctrl+L");
		Run.add("使用外部程序打开(O) Alt+L");
		Run.add("命令(C) Ctrl+R");    
		
		
		compilation.add(Cancel);
		compilation.addSeparator();
		compilation.add(Cut);
		compilation.add(Copy);
		compilation.add(Paste);
		compilation.add(Delete);
		compilation.addSeparator();
		compilation.add(Find);
		compilation.add(FindNext);
		compilation.add(Replace);
		compilation.add(Transfer);
		compilation.addSeparator();
		compilation.add(SelectAll);
		compilation.add(date);
		
		form.add(Autolinewrap);   
		form.add(font); 
		examine.add(StateItem);
		help.add(Introduction);
		help.add(HelpTheme);
		//注册事件监听器
		Copy.addActionListener(this);
		Cut.addActionListener(this);
		Paste.addActionListener(this);
		New.addActionListener(this);
		open.addActionListener(this);
		Save.addActionListener(this);
		As.addActionListener(this);
		Set.addActionListener(this);
		Print.addActionListener(this);
		Run.addActionListener(this); 
		attribute.addActionListener(this);
		deskShortCut.addActionListener(this);
		checkbox.addActionListener(this);
		Cancel.addActionListener(this);
		Delete.addActionListener(this);
		Find.addActionListener(this);
		FindNext.addActionListener(this);
		Replace.addActionListener(this);
		Transfer.addActionListener(this);
		SelectAll.addActionListener(this);
		date.addActionListener(this);
		Autolinewrap.addActionListener(this);
		font.addActionListener(this);
		HelpTheme.addActionListener(this);
		Introduction.addActionListener(this);
		StateItem.addActionListener(this);
		attribute.addActionListener(this);  
		checkbox.addItemListener(this);
		exit.addActionListener(this);
		
		
		addWindowListener(new WindowAdapter()
		  {
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}});
		
		setMenuBar(mb);  // 将菜单栏设置为指定的菜单栏
		show();  // 显示窗口
   }

	
  public void actionPerformed(ActionEvent e) 
   {
	    if(e.getSource()==exit)
 	      System.exit(0);
	    if (e.getSource()==Save||e.getSource()==As)  
	      {
		    int len=openDialog.showSaveDialog(this); 
			if(len==JFileChooser.APPROVE_OPTION)
			{ 
				File resaveFile=openDialog.getSelectedFile(); 
				des=msg.getText(); 
				save(resaveFile); 
			    sou=des; 
		    }
		  }
	    if(e.getSource()==New)
	       msg.setText(null);
	    if(e.getSource()==open)
	    { 
		    int len=openDialog.showOpenDialog(this); 
			File openFile=openDialog.getSelectedFile(); 
			if(len==JFileChooser.APPROVE_OPTION)
			{ 
				open(openFile); 
				souTime=openFile.lastModified(); 
			} 
		}
	    if(e.getSource()==Set)
	      Set();
	    if(e.getSource()==Print)
	      prt.printDialog();
	    if(e.getSource()==attribute)
	      attribute();
	    if(e.getSource()==deskShortCut)
	      deskShortCut();
	    if(e.getSource()==Cancel)
	      Cancel();
	    if(e.getSource()==Cut)
	      Cut();
	    if(e.getSource()==Copy)
	      Copy();
	    if(e.getSource()==Paste)
	      Paste();
	    if(e.getSource()==Delete)
	      Delete();
	    if(e.getSource()==Find)
	      Find();
	    if(e.getSource()==FindNext)
	      FindNext();
	    if(e.getSource()==Replace)
	      Replace();
	    if(e.getSource()==SelectAll)
	      msg.selectAll();
	    if(e.getSource()==date)
	      date();
	    if(e.getSource()==Autolinewrap)
	      Autolinewrap();
	    if(e.getSource()==font)
	      font();
	    if(e.getSource()==HelpTheme)
	      HelpTheme();
	    if(e.getSource()==Introduction)
		  JOptionPane.showMessageDialog(null,"彭成的记事本2008年11月"); 
		  labelInfo.setText("返回原程序"); 
   }
   public void itemStateChanged(ItemEvent e) 
    {   
	   if (e.getSource()==Autolinewrap)
	   //  msg.setLineWrap(true);
	    if (e.getSource()==checkbox)
	      if (checkbox.getState())
	        msg.setText(checkbox.getLabel()+"被选中");
	       	 else
	          msg.setText(checkbox.getLabel()+"被取消");
	        
  
    }
   
   //保存文件方法 
   public void save(File file){ 
		try
		{ 
			FileWriter out=new FileWriter(file); 
			  buffer=des.toCharArray(); 
			    out.write(buffer); 
			      out.close(); 
			        desTime=file.lastModified(); 
			          if(desTime!=souTime&&file.exists()) { 
			            labelInfo.setText(file.getPath()+"保存成功"); 
			              souTime=desTime;} 
		} 
		catch(Exception e){} 
		} 

   //打开文件方法 
   public void open(File file)
   { 
		try
		{ 
			FileReader in=new FileReader(file); 
			  int len=in.read(buffer); 
			    msg.setText(new String(buffer,0,len)); 
			      sou=new String(buffer,0,len); 
			        labelInfo.setText("打开"+file.getPath()); 
		 	          in.close(); 
		} 
		catch(Exception e){} 
	} 
   
   //全选方法	
   public void SelectAll()
   {
   	   msg.selectAll();
   } 
   
   //剪切方法
   public void Cut()
   {
   	    String s1 = msg.getSelectedText();//剪切到剪切板 
		  StringSelection text1 = new StringSelection(s1);//拖动鼠标选取文本 
		    clipboard.setContents(text1, null); 
		      int j = msg.getSelectionStart(); 
		        int k = msg.getSelectionEnd(); 
		          msg.replaceRange("", j, k);//从Text中删除被选取文本 
   }
   
   //复制方法
   public void Copy()
   {
   	    String s = msg.getSelectedText();//复制到剪切板 
		  StringSelection text1= new StringSelection(s);//拖动鼠标选取文本 
		    clipboard.setContents(text1, null); 
   }
   
   //粘贴方法
   public void Paste()
   {
   	    Transferable transferable = clipboard.getContents(this); 
		  DataFlavor dataflavor = DataFlavor.stringFlavor; 
		    if(transferable.isDataFlavorSupported(dataflavor)) 
		    { 
				try//处理异常事件的发生(类型不符合) 
					{ 
						String s3 = (String)transferable.getTransferData(dataflavor); 
						  msg.insert(s3, msg.getCaretPosition()); 
					} 
				catch(Exception e1) { } 
		    } 
   }
   
   //使用系统日期
   public void date()
   {
   	   Date d=new Date();
	     msg.append(d.toString());
   }
   public void Delete()
   {
   	   msg.replaceRange("",msg.getSelectionStart(),msg.getSelectionEnd()); 
   }   
   
   //下面是暂时未实现的方法

   public void Find()
   {
   	   msg.setText("暂未实现的功能");
   }
   public void FindNext()
   {
   	   msg.setText("暂未实现的功能");
   }
   public void HelpTheme()
   {
   	   msg.setText("暂未实现的功能");
   }   
   public void Replace()
   {
   	   msg.setText("暂未实现的功能");
   }
   public void Autolinewrap()
   {
   	   msg.setText("暂未实现的功能");
   }
   public void font()
   {
   	   msg.setText("暂未实现的功能");
   }  
   public void Introduction()
   {
   	   msg.setText("暂未实现的功能");
   } 
   public void Set()
   {
   	   msg.setText("暂未实现的功能");
   }
   public void attribute()
   {
   	   msg.setText("暂未实现的功能");
   }
   public void deskShortCut()
   {
   	   msg.setText("暂未实现的功能");
   }
   public void Cancel()
   {
   	   msg.setText("暂未实现的功能");
   }  

   
   //程序入口
   public static void main(String arg[]) 
	{
	    new TextEditor();	        
	}
}
 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -