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

📄 notepad.java

📁 用java编写的一个记事本程序,具有和windows Notepad相似的功能
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextArea;
import java.awt.datatransfer.*;
import java.io.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.border.*;
class MyFrame extends Frame implements ActionListener 
{
    Menu fileMenu,editMenu,formatMenu,helpMenu;
  	MenuItem newFile,openFile,saveFile,saveasFile,exitFile;
  	MenuItem removeEdit,copyEdit,pasteEdit,cutEdit,deleteEdit,findEdit,replaceEdit,selectallEdit;
  	MenuItem fontFormat;
  	MenuItem aboutHelp;
  	MenuBar bar;
  	public static JTextArea text;
  	FileDialog filedialog_save;
  	FileDialog filedialog_load;
  	FileReader fileReader;
  	FileWriter fileWriter;
 	BufferedReader in;
  	BufferedWriter out; 
  	Clipboard clipboard=null;
  	 int size=20;
  	static Font font;
  	ImageDialog imagedialog;
  	FontDialog fontdialog;
  	
  MyFrame()
  {
  	super("Notepad");

   Image icon=Toolkit.getDefaultToolkit().getImage("d:\\1.1.jpg");
  	this.setIconImage(icon);
  	 clipboard=getToolkit().getSystemClipboard();
  	 filedialog_save=new FileDialog(this,"保存对话框",FileDialog.SAVE);
  	 filedialog_load=new FileDialog(this,"打开对话框",FileDialog.LOAD);
  	 filedialog_save.setVisible(false);
  	 filedialog_load.setVisible(false);
 	 imagedialog=new ImageDialog(this);
 	 fontdialog=new FontDialog(this);
  	 filedialog_save.addWindowListener(new WindowAdapter()
  	 {
  	 	public void windowClosing(WindowListener e)
  	 	{
  	 		filedialog_save.setVisible(false);
  	 	}
  	 	
  	 });
  	 filedialog_load.addWindowListener(new WindowAdapter()
  	 {
  	 	public void windowClosing(WindowListener e)
  	 	{
  	 		filedialog_load.setVisible(false);
  	 	}
  	 	
  	 });
  	 
     text=new JTextArea(10,16);
     font=new Font("Batang",Font.PLAIN,size);
     
     text.setFont(font);
 	 bar=new MenuBar();
     fileMenu=new Menu("文件");
     editMenu=new Menu("编辑");
     formatMenu=new Menu("字体");
     
     helpMenu=new Menu("帮助");
     
     MenuShortcut newshort=new MenuShortcut(KeyEvent.VK_N,false);
     newFile=new MenuItem("新建",newshort);
     
     MenuShortcut openshort=new MenuShortcut(KeyEvent.VK_O,false);
     openFile=new MenuItem("打开",openshort);
     
     MenuShortcut saveshort=new MenuShortcut(KeyEvent.VK_S,false);
     saveFile=new MenuItem("保存",saveshort);
     
     MenuShortcut saveasshort=new MenuShortcut(KeyEvent.VK_X,false);
     saveasFile=new MenuItem("另存为",saveasshort);
      MenuShortcut exitshort=new MenuShortcut(KeyEvent.VK_E,false);
     exitFile=new MenuItem("退出",exitshort);
     fileMenu.add(newFile);
     fileMenu.add(openFile);
     fileMenu.add(saveFile);
     fileMenu.add(saveasFile);
     fileMenu.add(exitFile);
     
     newFile.addActionListener(this);
     openFile.addActionListener(this);
     saveFile.addActionListener(this);
     saveasFile.addActionListener(this);
     exitFile.addActionListener(this);
     
     MenuShortcut removeshort=new MenuShortcut(KeyEvent.VK_Z,false);
     removeEdit=new MenuItem("撤消",removeshort);
     
     MenuShortcut copyshort=new MenuShortcut(KeyEvent.VK_C,false);
     copyEdit=new MenuItem("复制",copyshort);
     
     MenuShortcut pasteshort=new MenuShortcut(KeyEvent.VK_V,false);
     pasteEdit=new MenuItem("粘贴",pasteshort);
     
     MenuShortcut cutshort=new MenuShortcut(KeyEvent.VK_X,false);
     cutEdit=new MenuItem("剪切",cutshort);
     
     MenuShortcut deleteshort=new MenuShortcut(KeyEvent.VK_D,false);
     deleteEdit=new MenuItem("删除",deleteshort);
     
     MenuShortcut findshort=new MenuShortcut(KeyEvent.VK_F,false);
     findEdit=new MenuItem("查找",findshort);
     
     MenuShortcut replaceshort=new MenuShortcut(KeyEvent.VK_R,false);
     replaceEdit=new MenuItem("替换",replaceshort);
     
     MenuShortcut selectallshort=new MenuShortcut(KeyEvent.VK_A,false);
     selectallEdit=new MenuItem("全选",selectallshort);
     
     fontFormat=new MenuItem("字体");
     aboutHelp=new MenuItem("关于");
     
 	 editMenu.add(removeEdit);
  	 editMenu.add(copyEdit);
  	 editMenu.add(pasteEdit);
  	 editMenu.add(cutEdit);
  	 editMenu.add(findEdit);
  	 editMenu.add(replaceEdit);
  	 editMenu.add(selectallEdit);
  	 
  	 removeEdit.addActionListener(this);
  	 copyEdit.addActionListener(this);
  	 pasteEdit.addActionListener(this);
  	 cutEdit.addActionListener(this);
  	 findEdit.addActionListener(this);
  	 replaceEdit.addActionListener(this);
  	 selectallEdit.addActionListener(this);
  	 
  	 formatMenu.add(fontFormat);
  	 helpMenu.add(aboutHelp);
      bar.add(fileMenu);
  	 bar.add(editMenu);
 	 bar.add(formatMenu);
  	 bar.add(helpMenu); 	
     setMenuBar(bar);
     
     fontFormat.addActionListener(this);
     aboutHelp.addActionListener(this);

  
 	 add(text,BorderLayout.CENTER);
  	 this.setVisible(true);
  	 this.setBounds(150,150,700,500);
  	 
  	 this.validate();
  	 addWindowListener(new WindowAdapter()
  	 {
  	 	public void windowClosing(WindowEvent e)
  	 	{
  	 		System.exit(0);
  	 	}
  	 	
  	 });

  }
  public void actionPerformed(ActionEvent e)
  {
  	if(e.getSource()==newFile)
  	{
  		
  	}
  	 	
  	if(e.getSource()==openFile)
  	{
  		filedialog_load.setVisible(true);
  		if(filedialog_load.getFile()!=null)
  		{
  			String name;
  			try
  			{
  	            fileReader=new FileReader(new File(filedialog_load.getDirectory(),filedialog_load.getFile()));				
  				in=new BufferedReader(fileReader);
  				while((name=in.readLine())!=null)
  				{
  					text.append(name+'\n');
  				}
  				in.close();
  				fileReader.close();
  			}
  			catch(IOException e1){
  				
  			}
  		}

  	}
  	
  	if(e.getSource()==saveFile)
  	{
  		filedialog_save.setVisible(true);
  		if(filedialog_save.getFile()!=null)
  		{
  			String name;
  			try
  			{
  				fileWriter=new FileWriter(new File(filedialog_save.getDirectory(),filedialog_save.getFile()));
  				out=new BufferedWriter(fileWriter);
  				name=text.getText();
  				out.write(name,0,name.length());
  				out.flush();
  				out.close();
  				fileWriter.close();
  			}
  			catch(IOException e2)
  			{
  				
  			}
  			
  		}
  		
  	}
  	
  	if(e.getSource()==saveasFile)
  	{
  		filedialog_save.setVisible(true);
  	}
  	if(e.getSource()==exitFile)
  	{
  		System.exit(0);
  	}
  	
  	  	if(e.getSource()==removeEdit)
  	{
  		int start=text.getSelectionStart();
  		int end=text.getSelectionEnd();
  		text.replaceRange("",start,end);
  	}
  	
  	
  	
  	if(e.getSource()==copyEdit)
  	{
  		String temp=text.getSelectedText();
  		StringSelection mytext=new StringSelection(temp);
  		clipboard.setContents(mytext,null);
  		
  	}
  	if(e.getSource()==pasteEdit)
  	{
  		Transferable contents=clipboard.getContents(this);
  		DataFlavor flavor=DataFlavor.stringFlavor;
  		if(contents.isDataFlavorSupported(flavor))
  		{
  			try
  			{
  				String str;
  				str=(String)contents.getTransferData(flavor);
  			    text.append(str);
  			}
  			catch(Exception ee)
  			{
  				
  			}
  		} 
  	}
  	
  	if(e.getSource()==cutEdit)
  	{
  		String temp=text.getSelectedText();
  		StringSelection mytext=new StringSelection(temp);
  		clipboard.setContents(mytext,null);
  		int start=text.getSelectionStart();
  		int end=text.getSelectionEnd();
  		text.replaceRange("",start,end);
  	}
  	
  	if(e.getSource()==selectallEdit)
  	{
  		text.setSelectionStart(0);
        try
        {
       
  		text.setSelectionEnd(text.getLineEndOffset(text.getRows()-1));
  	   } 
  	   catch(Exception e2)
  	   {
  	   }
  	}
  	
  	
  	if(e.getSource()==fontFormat)
  	{
  	//	FontPanel myframe=new FontPaenl();
  		fontdialog.setVisible(true);
  	}
  	if(e.getSource()==aboutHelp)
  	{
  		imagedialog.setVisible(true);
  		
 
  	}
  	
  }	 
  	 	
 }
 
 class ImageDialog extends Dialog 
 {	
 	static final int YES=1,NO=0;
 	int message=1;
 	Button yes,no;
 	Image im;

 ImageDialog(Frame f)
 {
 	super(f);
 	this.setTitle("关于记事本");
 	this.setModal(true);
 	this.setLayout(new FlowLayout());
 	this.setBounds(350,280,340,230);
 	im=Toolkit.getDefaultToolkit().createImage("d:\\help.jpg");
 	addWindowListener(new WindowAdapter()
 	{
 		public void windowClosing(WindowEvent e)
 		{
 			dispose();
 		}
 	});

 }
public void paint(Graphics g)
{
	g.drawImage(im,0,0,this);
} 
 
 }
 	
 


 class FontPanel extends Panel implements ItemListener ,ActionListener
{
	List list;
	Box baseBox,boxH1,boxH2,boxH3,boxH4,boxH5;	
	//JTextArea mytext;
	List fontList;
	List sizeList;
	TextField fontText;
	TextField sizeText;
	Button ok;
	Button no;
	JTextArea exampleText;
	static int size=20;
	int tempsize=20;
	static String name="character list";
	int message=-1;
	FontPanel()
	{
		//super("字体");
//		Image im=Toolkit.getDefaultToolkit().getImage("d:\\1.1.jpg");
//		this.setIconImage(im);
	//	mytext=text;
		Color c=new Color(236,233,216);
		this.setBackground(c);
        Panel p1=new Panel();
        boxH1=Box.createHorizontalBox();
        Label charterLabel=new Label("字体");
        charterLabel.setAlignment(1);
        boxH1.add(charterLabel);
        boxH1.add(Box.createHorizontalStrut(1));
        

        
        Label sizeLabel=new Label("大小");
        sizeLabel.setAlignment(1);        
        boxH1.add(sizeLabel);        
     
        boxH2=Box.createHorizontalBox();
        fontText=new TextField(8);
        boxH2.add(fontText);
        
        boxH2.add(Box.createHorizontalStrut(1));
        sizeText=new TextField(8);       
        boxH2.add(sizeText);

        
       
         fontList=new List(6,false);    
        fontList.addItemListener(this);
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        String fontName[]=ge.getAvailableFontFamilyNames();
        for(int i=0;i<fontName.length;i++)
        {
        	fontList.add(fontName[i]);
        }
        
         
        sizeList=new List(6,false);
        sizeList.addItemListener(this);
        for(int i=8;i<73;i++)
        {
        sizeList.add(String.valueOf(i));
        }
        Panel listpanel=new Panel();        
        GridLayout g2=new GridLayout(1,2);
        listpanel.setLayout(g2);
        listpanel.add(fontList);
        listpanel.add(sizeList);
        
        
        
        boxH4=Box.createHorizontalBox();
        Label exampleLabel=new Label("示例");
        exampleLabel.setAlignment(1);
        boxH4.add(exampleLabel);
        boxH4.add(Box.createHorizontalStrut(1));
        exampleText=new JTextArea(1,1);
       exampleText.append("Welcom to YONA studio!!");
        boxH4.add(exampleText);
        
        
        boxH5=Box.createHorizontalBox();
         ok=new Button("确定");
         ok.addActionListener(this);
        boxH5.add(ok);
        boxH5.add(Box.createHorizontalStrut(1));
        no=new Button("取消");
        no.addActionListener(this);
        boxH5.add(no);
     	
        GridLayout g1=new GridLayout(2,1);
        p1.setLayout(g1);
		p1.add(boxH1);
		p1.add(boxH2);

		
		GridLayout g3=new GridLayout(2,1);
		Panel p2=new Panel();
		p2.setLayout(g3);
		p2.add(boxH4);
		//p2.add(boxH5);
		this.setLayout(new BorderLayout());
		
		add(p1,BorderLayout.NORTH);
		add(listpanel,BorderLayout.CENTER);	    
	    add(p2,BorderLayout.SOUTH);

	    this.setVisible(true);
	    this.setBounds(280,250,300,220);
		this.validate();
		
	}	

public int getMessage()
{
	return message;
}	
public void itemStateChanged(ItemEvent e)
{
	

	if(e.getSource()==fontList)
	{
		name=fontList.getSelectedItem();
		Font f=new Font(name,Font.PLAIN,size);


		exampleText.setFont(f);

		fontText.setText(name);

	}
	if(e.getSource()==sizeList)
	{
		
		size=Integer.parseInt(sizeList.getSelectedItem());
		tempsize=size;
		Font f=new Font(fontList.getSelectedItem(),Font.PLAIN,size);
		exampleText.setFont(f);
		sizeText.setText(String.valueOf(size));
		
		

	}
}
public void actionPerformed(ActionEvent e1)
{
	
} 
 
  class FontDialog extends Dialog implements ActionListener
 {	
 	static final int YES=1,NO=0;
 	int message=1;
 	Button yes,no;
 	Image im;
 	FontPanel fontPanel;

   FontDialog(Frame f)
 {
 	super(f);

 	boxH5=Box.createHorizontalBox();
    ok=new Button("确定");
    ok.addActionListener(this);
    boxH5.add(ok);
    boxH5.add(Box.createHorizontalStrut(1));
    no=new Button("取消");
    no.addActionListener(this);
    boxH5.add(no);
    Panel tempPanel=new Panel();
    tempPanel.add(boxH5);
    
 	this.setTitle("字体对话框");
 	this.setModal(true);
 	this.setLayout(new BorderLayout());
 	this.setBounds(350,280,340,260);
   // fontPanel=new FontPanel();
  //  this.add(fontPanel,BorderLayout.CENTER);
    this.add(tempPanel,BorderLayout.SOUTH);
    
 	addWindowListener(new WindowAdapter()
 	{
 		public void windowClosing(WindowEvent e)
 		{
 			dispose();
 		}
 	});


 }
 
 public void actionPerformed(ActionEvent e1)
 {
	String name;
	int size=20;
	if(e1.getSource()==ok)
	{	

	    name=fontList.getSelectedItem();
	    
	   //size=Integer.parseInt(sizeList.getSelectedItem());
		Font f=new Font(name,Font.PLAIN,tempsize);
        MyFrame.text.setFont(f);
		MyFrame.font=f;		
		message=1;
		

	}
	if(e1.getSource()==no)
	{
		message=1;
	//	this.disable();
	}
}	

 }
 


 
 }
 
 
public class Notepad
{
	public static void main(String args[])
	{
		MyFrame myframe=new MyFrame();
	}
} 
  
  
  
  
  
  
  
  
  
  
	
	

⌨️ 快捷键说明

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