java高级-记事本编辑

采用java代码实现记事本

其中实现的功能:记事本的界面的生成、实现读取文件(普通文件)、编辑文件、实现保存、以及获取当前的时间的功能。

java高级-记事本编辑_第1张图片java高级-记事本编辑_第2张图片

 

public class NotePad { //实例化notepad对象
	
	public static void main(String[] args){
		Note note = new Note();
	}
}
 
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Note extends JFrame implements ActionListener {//记事本

	private static final long serialVersionUID = 1881420667028093357L;
	
	private JMenuBar mainMenu = new JMenuBar(); //主菜单
	private ToolMenu toolMenu = new ToolMenu();//工具栏
	private JScrollPane textScrollPane = null;//滚动文本域
	private JTextArea text = new JTextArea();//文本域
	
	private JMenu[] menuList = new JMenu[5];//主菜单下面的5个子菜单选项-文件-编辑-格式-查看-帮助
	private JMenuItem[] fileList = new JMenuItem[6]; //文件菜单下面的6个子菜单
	private JMenu newFile = new JMenu();//文件菜单下面的新建菜单作为一个主菜单,下面有新建,和打开的选项
	private JMenuItem[] newList = new JMenuItem[2];//新建菜单下面的两个子菜单
	
	private JMenuItem[] editList = new JMenuItem[6];//编辑菜单下面的6个子菜单选项
	
	private JMenuItem formatList = new JMenuItem();//格式下面的字体菜单
	
	private JMenuItem helpList = new JMenuItem();//帮助菜单下面帮助子菜单
	
	public Note(){
		this.menuList[0] = new JMenu("文件(F)",true);//主菜单下面文件菜单的创建
		this.menuList[0].setMnemonic('F');
		this.menuList[1] = new JMenu("编辑(E)");//主菜单下面编辑菜单的创建
		this.menuList[1].setMnemonic('E');
		this.menuList[2] = new JMenu("格式(O)");//主菜单下面的格式菜单的创建
		this.menuList[2].setMnemonic('O');
		this.menuList[3] = new JMenu("查看(V)",true);//主菜单下面的查看菜单的创建
		this.menuList[3].setMnemonic('V');
		this.menuList[4] = new JMenu("帮助(H)");//主菜单下面的帮助菜单的创建
		this.menuList[4].setMnemonic('H');
		
		this.fileList[0] = new JMenuItem("打开(O)",new ImageIcon("Image/open.jpg"));//文件下面六个子菜单创建
		this.fileList[0].setMnemonic('O');	     //Alt加上设置的符号就能打开此菜单
		this.fileList[0].setActionCommand("open");   //设置子菜单这个组件的唯一标识
		this.fileList[1] = new JMenuItem("保存(S)",new ImageIcon("Image/save.jpg"));//子菜单中前面图片的生成
		this.fileList[1].setMnemonic('S');
		this.fileList[1].setActionCommand("save");
		this.fileList[2] = new JMenuItem("另存为(A)",new ImageIcon("Image/saveAs.jpg"));
		this.fileList[2].setMnemonic('A');
		this.fileList[2].setActionCommand("saveAs");
		this.fileList[3] = new JMenuItem("页面设置(U)",new ImageIcon("Image/set.jpg"));
		this.fileList[3].setMnemonic('U');
		this.fileList[3].setActionCommand("set");
		this.fileList[4] = new JMenuItem("打印(P)",new ImageIcon("Image/print.jpg"));
		this.fileList[4].setMnemonic('P');
		this.fileList[4].setActionCommand("print");
		this.fileList[5] = new JMenuItem("退出(X)",new ImageIcon("Image/exit.jpg"));
		this.fileList[5].setMnemonic('X');
		this.fileList[5].setActionCommand("exit");
		
		this.newFile = new JMenu("新建(N)");//新建菜单创建
		this.newFile.setMnemonic('N');
		
		this.newList[0] = new JMenuItem("新建",new ImageIcon("Image/open.jpg"));//新建菜单下面的子菜单生成
		this.newList[0].setActionCommand("new");
		this.newList[1] = new JMenuItem("保存",new ImageIcon("Image/save.jpg"));
		this.newList[1].setActionCommand("newSave");
		
		this.newFile.add(this.newList[0]);//新建子菜单的生成
		this.newFile.add(this.newList[1]);
		
		this.menuList[0].add(this.newFile);//文件菜单的生成
		this.menuList[0].add(this.fileList[0]);
		this.menuList[0].add(this.fileList[1]);
		this.menuList[0].add(this.fileList[2]);
		this.menuList[0].addSeparator();
		this.menuList[0].add(this.fileList[3]);
		this.menuList[0].add(this.fileList[4]);
		this.menuList[0].addSeparator();
		this.menuList[0].add(this.fileList[5]);
		
		this.editList[0] = new JMenuItem("剪切(T)",new ImageIcon("Image/cut.jpg"));//编辑菜单下面的子菜单的创建
		this.editList[0].setMnemonic('T');
		this.editList[0].setActionCommand("shear");
		this.editList[1] = new JMenuItem("复制(C)",new ImageIcon("Image/copy.jpg"));
		this.editList[1].setMnemonic('C');
		this.editList[1].setActionCommand("copy");
		this.editList[2] = new JMenuItem("删除(L)",new ImageIcon("Image/delete.jpg"));
		this.editList[2].setMnemonic('L');
		this.editList[2].setActionCommand("delete");
		this.editList[3] = new JMenuItem("粘贴(P)",new ImageIcon("Image/paste.jpg"));
		this.editList[3].setMnemonic('P');
		this.editList[3].setActionCommand("paste");
		this.editList[4] = new JMenuItem("替换(R)",new ImageIcon("Image/replace.jpg"));
		this.editList[4].setMnemonic('R');
		this.editList[4].setActionCommand("replace");
		this.editList[5] = new JMenuItem("时间(D)",new ImageIcon("Image/time.jpg"));
		this.editList[5].setMnemonic('D');
		this.editList[5].setActionCommand("date");
		
		this.menuList[1].add(this.editList[0]);//编辑菜单的生成
		this.menuList[1].add(this.editList[1]);
		this.menuList[1].add(this.editList[2]);
		this.menuList[1].add(this.editList[3]);
		this.menuList[1].addSeparator();
		this.menuList[1].add(this.editList[4]);
		this.menuList[1].add(this.editList[5]);
		
		formatList = new JMenuItem("字体(F)",new ImageIcon("Image/font.jpg"));//格式菜单下面字体格式的创建
		this.formatList.setMnemonic('F');
		this.formatList.setActionCommand("font");
		
		this.menuList[2].add(this.formatList);//格式菜单的生成
		
		this.helpList = new JMenuItem("帮助(H)",new ImageIcon("Image/help.jpg"));//帮助菜单下面帮助子菜单的创建
		this.helpList.setMnemonic('H');
		this.helpList.setActionCommand("help");
		
		this.menuList[4].add(this.helpList);//帮助菜单的生成

		
		this.mainMenu.add(this.menuList[0]);//主菜单的生成
		this.mainMenu.add(this.menuList[1]);
		this.mainMenu.add(this.menuList[2]);
		this.mainMenu.add(this.menuList[3]);
		this.mainMenu.add(this.menuList[4]);
		
		
		fileList[0].addActionListener(this);//文件打开被加入监听者this
		fileList[1].addActionListener(this);//文件保存被加入监听者this
		editList[5].addActionListener(this);//编辑时间被加入监听者this
		
		this.textScrollPane = new JScrollPane(this.text);//普通文本域变为滚动文本域
		this.setJMenuBar(this.mainMenu);                 //加入主菜单
		this.add(this.toolMenu,BorderLayout.NORTH);      //加入工具栏在北边,即上边
		this.add(this.textScrollPane);                   //组件中加入滚动文本域
		this.setIconImage(new ImageIcon("image/notepadlogo.jpg").getImage());//生成记事本的logo,image是src下面的一个文件夹
		this.setTitle("记事本");                         //设置标题
		this.setSize(500,600);                           //设置记事本尺寸大小
		this.setLocation(350, 100);                      //设置记事本的位置
		this.setResizable(true);                         //设置显示之后的尺寸大小是否可以改变
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭之后,结束进程,释放资源
		this.setVisible(true);                           //设置显示,如不设置,则不进行显示  
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//文件选择器
		JFileChooser fileChooser = new JFileChooser();
		//打开文件里面的内容
		if(e.getActionCommand().equals("open")){ //文件打开
			fileChooser.setDialogTitle("文件打开");
			fileChooser.showOpenDialog(null);
			fileChooser.setVisible(true);
			String filepath = fileChooser.getSelectedFile().getAbsolutePath();
			FileReader reader = null;
			BufferedReader bReader = null;
			try {
				
				reader = new FileReader(filepath);
				bReader = new BufferedReader(reader);
				String content="";
				String buffer;
				while((buffer=bReader.readLine())!=null){
					content =content+buffer+"\n";
				}
				this.text.setText(content);
				System.out.print(content);
			} catch (Exception e1) {
				e1.printStackTrace();
			}finally{
				try {
					bReader.close();
					reader.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}else if(e.getActionCommand().equals("save")){//文件保存
			fileChooser.setDialogTitle("另存为");
			fileChooser.showSaveDialog(null);
			fileChooser.setVisible(true);
			String filepath = fileChooser.getSelectedFile().getAbsolutePath();
			PrintStream pStream = null;
			try {
				pStream = new PrintStream(filepath);
				System.setOut(pStream);
				System.out.println(this.text.getText());
				
			} catch (Exception e1) {
				e1.printStackTrace();
			}finally{
				pStream.close();
			}
		}else if(e.getActionCommand().equals("date")){ //获取当前时间并显示
			this.text.setText(new Time().getTime());
		}
	}
}
	    
 //Alt加上设置的符号就能打开此菜单
		this.fileList[0].setActionCommand("open");   //设置子菜单这个组件的唯一标识
		this.fileList[1] = new JMenuItem("保存(S)",new ImageIcon("Image/save.jpg"));//子菜单中前面图片的生成
		this.fileList[1].setMnemonic('S');
		this.fileList[1].setActionCommand("save");
		this.fileList[2] = new JMenuItem("另存为(A)",new ImageIcon("Image/saveAs.jpg"));
		this.fileList[2].setMnemonic('A');
		this.fileList[2].setActionCommand("saveAs");
		this.fileList[3] = new JMenuItem("页面设置(U)",new ImageIcon("Image/set.jpg"));
		this.fileList[3].setMnemonic('U');
		this.fileList[3].setActionCommand("set");
		this.fileList[4] = new JMenuItem("打印(P)",new ImageIcon("Image/print.jpg"));
		this.fileList[4].setMnemonic('P');
		this.fileList[4].setActionCommand("print");
		this.fileList[5] = new JMenuItem("退出(X)",new ImageIcon("Image/exit.jpg"));
		this.fileList[5].setMnemonic('X');
		this.fileList[5].setActionCommand("exit");
		
		this.newFile = new JMenu("新建(N)");//新建菜单创建
		this.newFile.setMnemonic('N');
		
		this.newList[0] = new JMenuItem("新建",new ImageIcon("Image/open.jpg"));//新建菜单下面的子菜单生成
		this.newList[0].setActionCommand("new");
		this.newList[1] = new JMenuItem("保存",new ImageIcon("Image/save.jpg"));
		this.newList[1].setActionCommand("newSave");
		
		this.newFile.add(this.newList[0]);//新建子菜单的生成
		this.newFile.add(this.newList[1]);
		
		this.menuList[0].add(this.newFile);//文件菜单的生成
		this.menuList[0].add(this.fileList[0]);
		this.menuList[0].add(this.fileList[1]);
		this.menuList[0].add(this.fileList[2]);
		this.menuList[0].addSeparator();
		this.menuList[0].add(this.fileList[3]);
		this.menuList[0].add(this.fileList[4]);
		this.menuList[0].addSeparator();
		this.menuList[0].add(this.fileList[5]);
		
		this.editList[0] = new JMenuItem("剪切(T)",new ImageIcon("Image/cut.jpg"));//编辑菜单下面的子菜单的创建
		this.editList[0].setMnemonic('T');
		this.editList[0].setActionCommand("shear");
		this.editList[1] = new JMenuItem("复制(C)",new ImageIcon("Image/copy.jpg"));
		this.editList[1].setMnemonic('C');
		this.editList[1].setActionCommand("copy");
		this.editList[2] = new JMenuItem("删除(L)",new ImageIcon("Image/delete.jpg"));
		this.editList[2].setMnemonic('L');
		this.editList[2].setActionCommand("delete");
		this.editList[3] = new JMenuItem("粘贴(P)",new ImageIcon("Image/paste.jpg"));
		this.editList[3].setMnemonic('P');
		this.editList[3].setActionCommand("paste");
		this.editList[4] = new JMenuItem("替换(R)",new ImageIcon("Image/replace.jpg"));
		this.editList[4].setMnemonic('R');
		this.editList[4].setActionCommand("replace");
		this.editList[5] = new JMenuItem("时间(D)",new ImageIcon("Image/time.jpg"));
		this.editList[5].setMnemonic('D');
		this.editList[5].setActionCommand("date");
		
		this.menuList[1].add(this.editList[0]);//编辑菜单的生成
		this.menuList[1].add(this.editList[1]);
		this.menuList[1].add(this.editList[2]);
		this.menuList[1].add(this.editList[3]);
		this.menuList[1].addSeparator();
		this.menuList[1].add(this.editList[4]);
		this.menuList[1].add(this.editList[5]);
		
		formatList = new JMenuItem("字体(F)",new ImageIcon("Image/font.jpg"));//格式菜单下面字体格式的创建
		this.formatList.setMnemonic('F');
		this.formatList.setActionCommand("font");
		
		this.menuList[2].add(this.formatList);//格式菜单的生成
		
		this.helpList = new JMenuItem("帮助(H)",new ImageIcon("Image/help.jpg"));//帮助菜单下面帮助子菜单的创建
		this.helpList.setMnemonic('H');
		this.helpList.setActionCommand("help");
		
		this.menuList[4].add(this.helpList);//帮助菜单的生成

		
		this.mainMenu.add(this.menuList[0]);//主菜单的生成
		this.mainMenu.add(this.menuList[1]);
		this.mainMenu.add(this.menuList[2]);
		this.mainMenu.add(this.menuList[3]);
		this.mainMenu.add(this.menuList[4]);
		
		
		fileList[0].addActionListener(this);//文件打开被加入监听者this
		fileList[1].addActionListener(this);//文件保存被加入监听者this
		editList[5].addActionListener(this);//编辑时间被加入监听者this
		
		this.textScrollPane = new JScrollPane(this.text);//普通文本域变为滚动文本域
		this.setJMenuBar(this.mainMenu);                 //加入主菜单
		this.add(this.toolMenu,BorderLayout.NORTH);      //加入工具栏在北边,即上边
		this.add(this.textScrollPane);                   //组件中加入滚动文本域
		this.setIconImage(new ImageIcon("image/notepadlogo.jpg").getImage());//生成记事本的logo,image是src下面的一个文件夹
		this.setTitle("记事本");                         //设置标题
		this.setSize(500,600);                           //设置记事本尺寸大小
		this.setLocation(350, 100);                      //设置记事本的位置
		this.setResizable(true);                         //设置显示之后的尺寸大小是否可以改变
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭之后,结束进程,释放资源
		this.setVisible(true);                           //设置显示,如不设置,则不进行显示  
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//文件选择器
		JFileChooser fileChooser = new JFileChooser();
		//打开文件里面的内容
		if(e.getActionCommand().equals("open")){ //文件打开
			fileChooser.setDialogTitle("文件打开");
			fileChooser.showOpenDialog(null);
			fileChooser.setVisible(true);
			String filepath = fileChooser.getSelectedFile().getAbsolutePath();
			FileReader reader = null;
			BufferedReader bReader = null;
			try {
				
				reader = new FileReader(filepath);
				bReader = new BufferedReader(reader);
				String content="";
				String buffer;
				while((buffer=bReader.readLine())!=null){
					content =content+buffer+"\n";
				}
				this.text.setText(content);
				System.out.print(content);
			} catch (Exception e1) {
				e1.printStackTrace();
			}finally{
				try {
					bReader.close();
					reader.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}else if(e.getActionCommand().equals("save")){//文件保存
			fileChooser.setDialogTitle("另存为");
			fileChooser.showSaveDialog(null);
			fileChooser.setVisible(true);
			String filepath = fileChooser.getSelectedFile().getAbsolutePath();
			PrintStream pStream = null;
			try {
				pStream = new PrintStream(filepath);
				System.setOut(pStream);
				System.out.println(this.text.getText());
				
			} catch (Exception e1) {
				e1.printStackTrace();
			}finally{
				pStream.close();
			}
		}else if(e.getActionCommand().equals("date")){ //获取当前时间并显示
			this.text.setText(new Time().getTime());
		}
	}
}
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

public class ToolMenu extends JToolBar {//工具栏

	private static final long serialVersionUID = 1222584954697038473L;
	
	private JButton[] toolList = new JButton[6];//工具栏下面的六个按钮
	
	public ToolMenu(){
		this.toolList[0] = new JButton(new ImageIcon("Image/new.jpg"));//工具栏下面的六个按钮的创建
		this.toolList[0].setToolTipText("新建");                       //鼠标放在上面能进行提示的信息
		this.toolList[1] = new JButton(new ImageIcon("Image/open.jpg"));
		this.toolList[1].setToolTipText("打开");
		this.toolList[2] = new JButton(new ImageIcon("Image/save.jpg"));
		this.toolList[2].setToolTipText("保存");
		this.toolList[3] = new JButton(new ImageIcon("Image/cut.jpg"));
		this.toolList[3].setToolTipText("剪切");
		this.toolList[4] = new JButton(new ImageIcon("Image/copy.jpg"));
		this.toolList[4].setToolTipText("复制");
		this.toolList[5] = new JButton(new ImageIcon("Image/paste.jpg"));
		this.toolList[5].setToolTipText("粘贴");
		
		this.add(this.toolList[0]);//工具栏的生成
		this.add(this.toolList[1]);
		this.add(this.toolList[2]);
		this.add(this.toolList[3]);
		this.add(this.toolList[4]);
		this.add(this.toolList[5]);
	}
}
import java.text.DateFormat;  
import java.util.Date;  
  
public class Time { //时间类  
    public String getTime(){  
         Date date = new Date();   
         DateFormat fullDateFormat =  DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);    
         return fullDateFormat.format(date); //时间进行格式化  
    }  
}

 

 

 

 

 

 

 

 

 

 

 

 

 
 

你可能感兴趣的:(杂七杂八)