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

📄 notepad.java

📁 数据结构中有简单的文本编辑器
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import javax.swing.*;
import javax.swing.event.*;

class FirstWindow extends Frame implements ActionListener,DocumentListener,Runnable
{
  MenuBar menubar;
  Menu menuFile,menuEdit,menuFormat,menuHelp;
  MenuItem itemNew,itemLoad,itemSave,itemSaveAs,itemExit,itemCut,itemCopy,itemPaste,itemRemove,itemSelectAll,itemTime,itemLineWrap,itemFont,itemHelp,itemAbout;
  FileDialog filedialog_Save,filedialog_Load;
  JTextArea text;
  JScrollPane scroll;
  Date nowTime;
  SimpleDateFormat time;
  BufferedReader in;
  FileReader file_reader;
  BufferedWriter out;
  FileWriter  tofile;
  int text_event = 0,clickTimes =1;

  FirstWindow(String s)
  {
	setTitle(s);
	Toolkit tool=getToolkit();
	Dimension dim =tool.getScreenSize();
	setBounds(40,40,dim.width/2,dim.height/2);
    text= new JTextArea();
    scroll = new JScrollPane(text);
	menubar =new MenuBar();
	menuFile =new Menu("文件");
	menuEdit =new Menu("编辑");
	menuFormat =new Menu("格式");
	menuHelp =new Menu("帮助");
    itemNew =new MenuItem("新建");
	itemLoad =new MenuItem("打开");
	itemSave =new MenuItem("保存");
	itemSaveAs =new MenuItem("另存为");
	itemExit=new MenuItem("退出");
    itemCut=new MenuItem("剪切");
    itemCopy=new MenuItem("复制");
    itemPaste=new MenuItem("粘贴");
    itemRemove=new MenuItem("删除");
    itemSelectAll=new MenuItem("全选");
    itemTime=new MenuItem("时间");
	itemLineWrap=new MenuItem("自动换行");
	itemFont=new MenuItem("字体");
	itemHelp=new MenuItem("帮助主题");
	itemAbout=new MenuItem("关于");

	filedialog_Save =new FileDialog(this,"保存文件",FileDialog.SAVE);
    filedialog_Load =new FileDialog(this,"打开文件",FileDialog.LOAD);

    menubar.add(menuFile);
	menubar.add(menuEdit);
	menubar.add(menuFormat);
	menubar.add(menuHelp);
    menuFile.add(itemNew);
    menuFile.add(itemLoad);
	menuFile.add(itemSave);
	menuFile.add(itemSaveAs);
    menuFile.addSeparator();
	menuFile.add(itemExit);
    menuEdit.add(itemCut);
    menuEdit.add(itemCopy);
    menuEdit.add(itemPaste);
    menuEdit.add(itemRemove);
    menuEdit.addSeparator();
    menuEdit.add(itemSelectAll);
    menuEdit.add(itemTime);
	menuFormat.add(itemLineWrap);
	menuFormat.add(itemFont);
	menuHelp.add(itemHelp);
	menuHelp.add(itemAbout);
	add(scroll);

    itemNew.addActionListener(this);
	itemLoad.addActionListener(this);
    itemSave.addActionListener(this);
	itemSaveAs.addActionListener(this);
	itemExit.addActionListener(this);
    itemCut.addActionListener(this);
    itemCopy.addActionListener(this);
    itemPaste.addActionListener(this);
    itemRemove.addActionListener(this);
    itemSelectAll.addActionListener(this);
    itemTime.addActionListener(this);
	itemFont.addActionListener(this);
	itemLineWrap.addActionListener(this);
	itemAbout.addActionListener(this);
	addWindowListener(new WindowAdapter()
	                       {
							  public void windowClosing(WindowEvent e)
							  { System.exit(0); }
						   });
    (text.getDocument()).addDocumentListener(this);

	filedialog_Save.setVisible(false);
    filedialog_Load.setVisible(false);
	setMenuBar(menubar);
	setVisible(true);
  }



  public void actionPerformed(ActionEvent e)
  {
	if(e.getSource()==itemNew)
  	{
	  if( text_event == 1)
		{
		  String message = "文本内容被修改,是否保存?";
		  int ok = JOptionPane.showConfirmDialog(null,message,"确认对话框",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
          if(ok==JOptionPane.YES_OPTION)
           {
			filedialog_Save.setVisible(true);
		    if(filedialog_Save.getFile()!=null)
		    {
			  try{
				 File file =new File(filedialog_Save.getDirectory(),filedialog_Save.getFile());

				 tofile=new FileWriter(file);
				 out =new BufferedWriter(tofile);
			//	 PrintStream ps = new PrintStream(new FileOutputStream("p.txt"));
				 out.write(text.getText(),0,(text.getText()).length());
			// 	 ps.close();
				 out.close();
				 tofile.close();
				}
			  catch(IOException e2){}
		     }
		     text_event = 0; text.setText(null);  setTitle("新建文本文档");
		   }
		   else if(ok==JOptionPane.NO_OPTION)
		         { text_event = 0; text.setText(null);  setTitle("新建文本文档");  }
		   else if(ok==JOptionPane.CANCEL_OPTION) {text_event = 0; }
	     }
	  else       { text_event = 0; text.setText(null);  setTitle("新建文本文档");  }
	}

  	else if(e.getSource()==itemLoad)
     {
		  if( text_event == 1)
		  {
			 String message = "文本内容被修改,是否保存?";
		     int ok = JOptionPane.showConfirmDialog(null,message,"确认对话框",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
             if(ok==JOptionPane.YES_OPTION)
             {
		  			filedialog_Save.setVisible(true);
		  		    if(filedialog_Save.getFile()!=null)
		  		    {
		  			  try{
		  				 File file =new File(filedialog_Save.getDirectory(),filedialog_Save.getFile());
		  				 tofile=new FileWriter(file);
		  				 out =new BufferedWriter(tofile);
		  				 out.write(text.getText(),0,(text.getText()).length());
		  				 out.close();
		  				 tofile.close();
		  				}
		  			catch(IOException e2){}
		  		   }
		  		   text_event = 0;
		     }
		    else if(ok==JOptionPane.NO_OPTION)
			 {
				filedialog_Load.setVisible(true);
				text.setText(null);
				String s;
				if(filedialog_Load.getFile()!=null)
				 {
					try{
					        File file =new File(filedialog_Load.getDirectory(),filedialog_Load.getFile());
						    file_reader=new FileReader(file);
						    setTitle(filedialog_Load.getFile());
							in =new BufferedReader(file_reader);
							while((s=in.readLine())!=null)
							 	text.append(s+'\n');
							in.close();
							file_reader.close();
						 }
					 catch(IOException e2){}
     			 }
     			 text_event = 0;
	      }
		    else if(ok==JOptionPane.CANCEL_OPTION) {}
	     }

         else
         {
		 			filedialog_Load.setVisible(true);
		 			text.setText(null);
		 			String s;
		 			if(filedialog_Load.getFile()!=null)
		 			{
		 				try{
		 					 File file =new File(filedialog_Load.getDirectory(),filedialog_Load.getFile());
		 					 file_reader=new FileReader(file);
		 					 setTitle(filedialog_Load.getFile());
		 					 in =new BufferedReader(file_reader);
		 					 while((s=in.readLine())!=null)
		 					       text.append(s+'\n');
		 					 in.close();
		 					 file_reader.close();
		 					}
		 			    catch(IOException e2){}
		 			}
	      }
	 }
	 else if(e.getSource()==itemSave)
	   	{
	 		filedialog_Save.setVisible(true);
	 		if(filedialog_Save.getFile()!=null)
	 		{
	 			try{
	 				 File file =new File(filedialog_Save.getDirectory(),filedialog_Save.getFile());
	 				 tofile=new FileWriter(file);
	 				 out =new BufferedWriter(tofile);
	 				 out.write(text.getText(),0,(text.getText()).length());
	 				 out.close();
	 				 tofile.close();
	 				}
	 			catch(IOException e2){}
	 		}
	 	}


  	else if(e.getSource()==itemSaveAs)
  	{
		filedialog_Save.setVisible(true);
		if(filedialog_Save.getFile()!=null)
		{
			try{
				 File file =new File(filedialog_Save.getDirectory(),filedialog_Save.getFile());
				 setTitle(filedialog_Save.getFile());
				 tofile=new FileWriter(file);
				 out =new BufferedWriter(tofile);
				 out.write(text.getText(),0,(text.getText()).length());
				 out.close();
				 tofile.close();
				}
			catch(IOException e2){}
		}
	}

	 else if(e.getSource()==itemExit)
	   {
	 	 System.exit(0);
	   }
	 else if(e.getSource()==itemFont)
		 {
			FontDialog dialog =new FontDialog(this,"字体",true);
            dialog.setVisible(true);
		 }
     else if(e.getSource()==itemCut)
		 {
			text.cut();
		 }
     else if(e.getSource()==itemCopy)
		 {
			text.copy();
		 }
     else if(e.getSource()==itemPaste)
		 {
			text.paste();
		 }
     else if(e.getSource()==itemRemove)
		 {
			int start = text.getSelectionStart();
			int end = text.getSelectionEnd();
			text.replaceRange("",start,end);
		 }
     else if(e.getSource()==itemSelectAll)
		 {
			text.selectAll();
		 }
     else if(e.getSource()==itemTime)
		 {
			nowTime = new Date();
			time = new SimpleDateFormat("北京时间:yyyy年MM月dd日 E HH时mm分ss秒");
			text.insert(time.format(nowTime),text.getCaretPosition());
		 }
     else if(e.getSource()==itemLineWrap)
	     {
			if((clickTimes%2)!=0)
			 {
			  text.setLineWrap(true);
			  text.setWrapStyleWord(true);
			  clickTimes++;
		     }
		    else
		    {
			  text.setLineWrap(false);
			  text.setWrapStyleWord(false);
			  clickTimes++;
			}
		 }
     else if(e.getSource()==itemAbout)
	   {
			AboutDialog about=new AboutDialog(this,"关于",true);
			about.setVisible(true);
		}
	}

 public void changedUpdate(DocumentEvent e)
 {
    text_event = 1;
 }
 public void insertUpdate(DocumentEvent e){ text_event = 1; }
 public void removeUpdate(DocumentEvent e){ text_event = 1; }

public void run(){}

 class FontDialog extends Dialog implements ItemListener,ActionListener
{
	Choice choiceFont,choiceSize,choiceStyle;
	Panel panelButton,panelStyle,panelColor;
	JScrollPane scrollPaneModel;
	//JList listFont,listSize;
    Button yes,no,foreColor,backgroundColor;
    String stringName,stringSize,fontstyle;
    int size,style;
    Font f;
    JTextArea model;
    CheckboxGroup g;
    Checkbox 常规,粗体,斜体,粗斜体;

	FontDialog(Frame f,String s,boolean b)
	{
		super(f,s,b);
		setBounds(60,60,400,300);
		setLayout(null);
		choiceFont =new Choice();
		choiceFont.setBounds(30,40,120,130);
		choiceSize =new Choice();
		choiceSize.setBounds(180,40,120,130);
		choiceStyle =new Choice();
		panelButton = new Panel();
		panelButton.setBounds(130,240,300,300);

		model = new JTextArea("中国龙AaBbCc123?!@");
		scrollPaneModel = new JScrollPane(model);
		scrollPaneModel.setBounds(160,100,160,100);
	/*
		listFont = new JList();
		listFont.setBounds(30,230,70,60);
		listSize = new JList();
		listSize.setBounds(0,0,100,80);

*/
		panelStyle = new Panel();
		panelStyle.setBounds(20,80,80,120);

	    g = new CheckboxGroup();
		常规 = new Checkbox("常规",true,g);
		粗体 = new Checkbox("粗体",false,g);
		斜体 = new Checkbox("斜体",false,g);
		粗斜体 = new Checkbox("粗斜体",false,g);

		yes=new Button("确定");
		no=new Button("取消");
		panelColor = new Panel();
		panelColor.setBounds(30,200,80,60);
		foreColor = new Button("设置字体颜色");
		foreColor.setBounds(30,200,80,20);
		backgroundColor = new Button("设置背景颜色");
		backgroundColor.setBounds(30,220,75,20);

        String size[] ={"8","9","10","11","12","13","14","16","18","20","22","24","26","28","36","72"};
        for(int i=0;i<size.length;i++)
		{
			choiceSize.add(size[i]);
		}
		GraphicsEnvironment ge =GraphicsEnvironment.getLocalGraphicsEnvironment();
		String fontName[] = ge.getAvailableFontFamilyNames();
		for(int i=0;i<fontName.length;i++)
		{
			choiceFont.add(fontName[i]);
		}
		String fontStyle[] = { "常规","粗体","斜体","粗斜体"};
		for(int i=0;i<fontStyle .length;i++)
		{
					choiceStyle.add(fontStyle [i]);
		}


      //  listSize.add(listFont);
		add(choiceFont);
		add(choiceSize);
		add(scrollPaneModel);
		add(panelButton);
		add(panelStyle);
//		panelStyle.add(choiceStyle);

		panelStyle.add(常规);
		panelStyle.add(粗体);
		panelStyle.add(斜体);
		panelStyle.add(粗斜体);
//		add(listFont);
//		add(listSize);

		panelButton.add(yes);
		panelButton.add(no);
		add(panelColor);
        panelColor.add(foreColor);
        panelColor.add(backgroundColor);

		choiceFont.addItemListener(this);
		choiceSize.addItemListener(this);
//		choiceStyle.addItemListener(this);

		常规.addItemListener(this);
		粗体.addItemListener(this);
		斜体.addItemListener(this);
		粗斜体.addItemListener(this);

		yes.addActionListener(this);
		no.addActionListener(this);
		foreColor.addActionListener(this);
		backgroundColor.addActionListener(this);
		addWindowListener(new WindowAdapter()
						    {
								public void windowClosing(WindowEvent e)
								{ setVisible(false); }
							});
       validate();
       //setVisible(true);
	 }


     public void itemStateChanged(ItemEvent e)
     {

   	       stringName =choiceFont.getSelectedItem();
   	       stringSize=choiceSize.getSelectedItem();
   		   size=Integer.parseInt(stringSize);
/*
   		   fontstyle = choiceFont.getSelectedItem();

   		   if(fontstyle=="常规"){  style = Font.TRUETYPE_FONT; }
		   else if(fontstyle=="粗体"){  style = Font.BOLD; }
		   else if(fontstyle=="斜体"){  style = Font.ITALIC; }
           else if(fontstyle=="粗斜体"){  style = Font.BOLD +Font.ITALIC ; }
*/
           //g.setSelectedCheckbox(常规);
           Checkbox box = (Checkbox)e.getSource();

           if(box==常规){ g.setSelectedCheckbox(常规); style = Font.TRUETYPE_FONT; }
           else if(box==粗体){ g.setSelectedCheckbox(粗体); style = Font.BOLD; }
           else if(box==斜体){ g.setSelectedCheckbox(斜体); style = Font.ITALIC; }
           else if(box==粗斜体){ g.setSelectedCheckbox(粗斜体); style = Font.BOLD +Font.ITALIC ; }



       	   f =new Font(stringName,style,size);
           model.setFont(f);
   	 }

	 public void actionPerformed(ActionEvent e)
	 {
		if(e.getSource()==yes)
		{
     		   text.setFont(f);
			   setVisible(false);
	    }
	   else if(e.getSource()==no)
		        setVisible(false);
	   else if(e.getSource()==foreColor)
		{
		   Color foreColor = JColorChooser.showDialog(this,"设置字体颜色",text.getForeground());
		   if(foreColor!= null)
		   text.setForeground(foreColor);
		}
	  else if(e.getSource()==backgroundColor)
		{
			Color backgroundColor = JColorChooser.showDialog(this,"设置背景颜色",text.getBackground());
			if(backgroundColor != null)
			text.setBackground(backgroundColor);
		}
	 }

   }


}


public class NotePad
{
  public static void main(String args[])
  {
	  FirstWindow win =new FirstWindow("新建文本文档");

  }
}

 class AboutDialog extends Dialog implements ActionListener
   {
      AboutDialog(Frame f,String s,boolean b)
      {
		 super(f,s,b);
		 setBounds(200,200,300,200);
		 addWindowListener(new WindowAdapter()
		 						    {
		 								public void windowClosing(WindowEvent e)
		 								{ setVisible(false); }
							});
	  }

	  public void actionPerformed(ActionEvent e)
	  {

	  }
   }

⌨️ 快捷键说明

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