仿Windows记事本简要分析 (Java)

                      仿Windows记事本简要分析 (Java)

  学Java的同学大部分都会做过一个小练习 ,用Java 做出一个记事本 。其中主要涉及的是界面和 io流 方面的知识 。 

  学完之后我在这做一个小总结,以下代码为部分练习代码,如果有错误或则可以改进的地方可以加我微信macforyou1。

  记事本主要功能为存入文本和打开 ,在这里我的记事本需求为




1 菜单栏中包含文件,编辑,查看和帮助菜单
2  文件菜单中包括 新建,打开,保存,另存为,打印和退出功能
3 编辑菜单中具有的功能有撤销,重做,剪切,复制,删除,全选,以及查找和替换
4 查看菜单中具有的功能包括字体,颜 色等
5 ·帮助菜单中可以查看帮助文档


在这里我将这些逐步实现这些功能  一共做了四次改进

1.0  版本  界面可以运行成功

2.0版本  有打开和保存选项,为其添加功能

3.0版本  丰富界面



  1.0版本 代码为  

 

 public class MynoteBook  extends JFrame  implements ActionListener {
      JTextArea jta=null;
      //菜单条
      JMenuBar jmb=null;
      JMenu jMenu=null;

      public MynoteBook() {
          //jta默认布局式border
        jta=new JTextArea();
        jmb=new JMenuBar();
        jMenu=new JMenu("文件");
        jMenu.setMnemonic('F');

jmb.add(jMenu);


   
        this.setTitle("我的记事本");
        this.add(jta);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300,400);
        this.setVisible(true);
    }
      public static void main(String[] args) {
        MynoteBook m1=new MynoteBook();
    }
    @Override
    public void actionPerformed(ActionEvent e) {

}
}
界面为

1.0版本运行成功  我们开始逐步添加子菜单  首先在文件选项中添加打开和保存

并且为其注册监听事件

界面为

2.0版本



其代码为

import java.io.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * 我的记事本
 * @author mac
 *
 */
public class MynoteBook  extends JFrame  implements ActionListener {
      JTextArea jta=null;
      //菜单条 
      JMenuBar jmb=null;
      JMenu jMenu=null;
      JMenuItem jMenuItem1=null;
      JMenuItem jMenuItem2=null;
      //构造函数初始化
      public MynoteBook() {
     //jta默认布局式border
jta=new JTextArea();
jmb=new JMenuBar();
jMenu=new JMenu("文件");
jMenu.setMnemonic('F');
jMenuItem1=new JMenuItem("打开");
jMenuItem1.setActionCommand("open");
//打开  注册监听 
jMenuItem1.addActionListener(this);
jMenuItem2=new JMenuItem("保存");
//保存  注册监听
jMenuItem2.addActionListener(this);
jMenuItem2.setActionCommand("save");
//加入menubar
this.setJMenuBar(jmb);
//把菜单翻入jmenubar
jmb.add(jMenu);
//把子菜单放入menu中
jMenu.add(jMenuItem1);
jMenu.add(jMenuItem2);
this.add(jta);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,400);
this.setVisible(true);
}
      public static void main(String[] args) {
MynoteBook m1=new MynoteBook();
}
@Override
public void actionPerformed(ActionEvent e) {
 if (e.getActionCommand().equals("open")) {
//point:  JFileChooser
 //创建一个文件选择对象 
 JFileChooser jfc1=new JFileChooser();
 jfc1.setDialogTitle("请选择文件");
 
 jfc1.showOpenDialog(null);
 jfc1.setVisible(true);
 
 //得到用户选择的文件的全路径
 String filename=jfc1.getSelectedFile().getAbsolutePath();
 System.out.println(filename);
 //使用bufferreader  来进行文件的输入输出
 FileReader fr=null;
 BufferedReader  br=null;
 
 try {
fr=new FileReader(filename);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
 
 br=new BufferedReader(fr);
 
 //将得到的文件内容显示在记事本上面 
//输出到记事本上面 
 
 String string="";
String allString="";
try {
while((string=br.readLine())!=null){
 allString+=string;
 jta.setText(allString);
 }
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
     //输出到记事本上面之后千万不要忘记
     try {
br.close();
fr.close();
} catch (Exception e2) {
// TODO: handle exception
}
 
}else if (e.getActionCommand().equals("save")) {
//创建一个文件选择对象 
 JFileChooser jfc1=new JFileChooser();
 jfc1.setDialogTitle("另存为");
 
 jfc1.showSaveDialog(null);
 jfc1.setVisible(true);
 
 //得到用户希望把文件保存在何处,文件全文件 
 
 String file=jfc1.getSelectedFile().getAbsolutePath();
 
 //准备写入到指定文件  
 FileWriter fw=null;
 BufferedWriter bw=null;
 
 try {
fw=new FileWriter(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
 bw=new BufferedWriter(fw);
 try {
bw.write(this.jta.getText());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally {
try {
bw.close();
fw.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
 
 
 
}
 
}
}
其中的难点为  实现打开和保存功能,我们无需再自己设计界面,只需调用JFileChooser 其中我们可以发现JFileChooser主要功能就是出现打开和保存功能


但是打开和保存功能是需要我们自己实现的,在这里我们使用io流中的字符流,因为使用字节流输入输出太慢,同时如果使用字节流的话,读入文件可能造成所有文件都在一行,造成格式的混乱,使用字符流,实现逐行的读入,符合产品需求

3.0版本

import java.io.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * 我的记事本
 * @author mac
 *
 */
public class MynoteBook  extends JFrame  implements ActionListener {
      JTextArea jta=null;
      //菜单条 
      JMenuBar jmb=null;
      JMenu jMenu=null;
      JMenu jMenu2=null;
      JMenu jMenu3=null;
      JMenu jMenu4=null;
     
      JMenuItem jMenuItem1=null;
      JMenuItem jMenuItem2=null;
      JMenuItem jMenuItem3=null;
      JMenuItem jMenuItem4=null;
      JMenuItem jMenuItem5=null;
      JMenuItem jMenuItem6=null;
      JMenuItem jMenuItem7=null;
      JMenuItem jMenuItem8=null;
      //构造函数初始化
      public MynoteBook() {
    	  //jta默认布局式border
		jta=new JTextArea();
		jmb=new JMenuBar();
		jMenu=new JMenu("文件");
		jMenu.setMnemonic('F');
		
		jMenu2=new JMenu("编辑");
		jMenu2.setMnemonic('E');
		
		jMenu3=new JMenu("查看");
		jMenu3.setMnemonic('L');
		
		jMenu4=new JMenu("帮助");
		jMenu4.setMnemonic('H');
		
		
		jMenuItem1=new JMenuItem("打开");
		jMenuItem1.setActionCommand("open");
		//打开  注册监听 
		jMenuItem1.addActionListener(this);
		
	
		jMenuItem2=new JMenuItem("保存");
		//保存  注册监听
		jMenuItem2.addActionListener(this);
		jMenuItem2.setActionCommand("save");
		
		
		jMenuItem3=new JMenuItem("复制");
		jMenuItem3.setActionCommand("copy");
		//打开  注册监听 
		jMenuItem3.addActionListener(this);
		
		jMenuItem4=new JMenuItem("粘贴");
		jMenuItem4.setActionCommand("paste");
		//打开  注册监听 
		jMenuItem4.addActionListener(this);
		
		jMenuItem5=new JMenuItem("字体");
		jMenuItem5.setActionCommand("font");
		//打开  注册监听 
		jMenuItem5.addActionListener(this);
		
		jMenuItem6=new JMenuItem("颜色");
		jMenuItem6.setActionCommand("color");
		//打开  注册监听 
		jMenuItem6.addActionListener(this);
		
		jMenuItem7=new JMenuItem("帮助文档");
		jMenuItem7.setActionCommand("help");
		//打开  注册监听 
		jMenuItem7.addActionListener(this);
		
		jMenuItem8=new JMenuItem("版本");
		jMenuItem8.setActionCommand("version");
		//打开  注册监听 
		jMenuItem8.addActionListener(this);
		
		//把子菜单放入menu中
		jMenu.add(jMenuItem1);
		jMenu.add(jMenuItem2);
		
		jMenu2.add(jMenuItem3);
		jMenu2.add(jMenuItem4);
		
		jMenu3.add(jMenuItem5);
		jMenu3.add(jMenuItem6);
		
		jMenu4.add(jMenuItem7);
		jMenu4.add(jMenuItem8);
		
		//设置menubar
		this.setJMenuBar(jmb);
		
		//把菜单翻入jmenubar
		jmb.add(jMenu);
	    jmb.add(jMenu2);
	    jmb.add(jMenu3);
	    jmb.add(jMenu4);
		
		
		
		
		this.setTitle("我的记事本");
		this.add(jta);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(300,400);
		this.setVisible(true);
	}
      public static void main(String[] args) {
		MynoteBook m1=new MynoteBook();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
	  if (e.getActionCommand().equals("open")) {
		//point:  JFileChooser
		  //创建一个文件选择对象 
		  JFileChooser jfc1=new JFileChooser();
		  jfc1.setDialogTitle("请选择文件");
		  
		  jfc1.showOpenDialog(null);
		  jfc1.setVisible(true);
		  
		  //得到用户选择的文件的全路径
		  String filename=jfc1.getSelectedFile().getAbsolutePath();
			
		  System.out.println(filename);
		  //使用bufferreader  来进行文件的输入输出
		  FileReader fr=null;
		  BufferedReader  br=null;
		  
		  try {
			fr=new FileReader(filename);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		  
		  br=new BufferedReader(fr);
		  
		  //将得到的文件内容显示在记事本上面 
		 //输出到记事本上面 
		  
		  String string="";
		 String allString="";
			try {
				while((string=br.readLine())!=null){
					  allString+=string;
					  jta.setText(allString);
				  }
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	      //输出到记事本上面之后千万不要忘记
	      try {
			br.close();
			fr.close();
		} catch (Exception e2) {
			// TODO: handle exception
		}
		  
	}else if (e.getActionCommand().equals("save")) {
		 //创建一个文件选择对象 
		  JFileChooser jfc1=new JFileChooser();
		  jfc1.setDialogTitle("另存为");
		  

		  jfc1.showSaveDialog(null);
		  jfc1.setVisible(true);
		  
		  //得到用户希望把文件保存在何处,文件全文件 
		  
		  String file=jfc1.getSelectedFile().getAbsolutePath();
		  
		  //准备写入到指定文件  
		  FileWriter fw=null;
		  BufferedWriter bw=null;
		  
		  try {
			fw=new FileWriter(file);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		  bw=new BufferedWriter(fw);
		  try {
			bw.write(this.jta.getText());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally {
			try {
				bw.close();
				fw.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		  
		  
		  
	}
	
	 
	}
}


然后我们再逐步添加之后的功能,只需复制粘贴源代码即可 ,

将所有的组件添加完成后,outline可以很清楚的看到所需组件和实现,再对代码改进十分简单方便

以上为记事本功能的基本实现,之后复制粘贴功能实现的代码,我将在下节展示






实现了记事本的大部分功能,适合新手看。 // 文本编辑区 private JTextArea editArea = new JTextArea(); // 菜单项 private JMenu[] menus = { new JMenu("文件(F)"), new JMenu("编辑(E)"), new JMenu("格式(E)"), new JMenu("查看(V)"), new JMenu("帮助(H)") }; private JMenuItem[] fileMenu = { new JMenuItem("新建(N) "), new JMenuItem("打开(O)... "), new JMenuItem("保存(S) "), new JMenuItem("另存为(A)... "), new JMenuItem("页面设置(U)... "), new JMenuItem("打印(P)... "), new JMenuItem("退出(X) ") }; private JMenuItem[] editMenu = { new JMenuItem("撤销(U) "), new JMenuItem("剪切(T) "), new JMenuItem("复制(C) "), new JMenuItem("黏贴(P) "), new JMenuItem("删除(L) "), new JMenuItem("查找(F)... "), new JMenuItem("查找下一个(N) "), new JMenuItem("替换(R)... "), new JMenuItem("转到(G)... "), new JMenuItem("全选(A) "), new JMenuItem("时间/日期(D) ") }; private JCheckBoxMenuItem formatMenu1 = new JCheckBoxMenuItem( "自动换行(W) "); private JMenuItem formatMenu2 = new JMenuItem("字体(F)... "); private JMenuItem checkMenu = new JMenuItem("状态栏(S) "); private JMenuItem[] helpMenu = { new JMenuItem("查看帮助(H) "), new JMenuItem("关于记事本(A) ") }; private JPopupMenu pmenus = new JPopupMenu(); private JMenuItem[] popupMenu = { new JMenuItem("撤销(U) "), new JMenuItem("剪切(T) "), new JMenuItem("复制(C) "), new JMenuItem("黏贴(P) "), new JMenuItem("删除(L) "), new JMenuItem("全选(A) "), }; // 剪切板引用 private Toolkit toolKit = Toolkit.getDefaultToolkit(); private Clipboard clipBoard = toolKit.getSystemClipboard(); // 其他变量 private boolean isNewFile = true; private File currentFile; private String oldTxt; // 撤销管理器 private UndoManager undo = new UndoManager(); private UndoableEditListener undoHandler = new UndoHandler();
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值