java实现记事本

#java# /*
 * NotePad.java
 *
 * Created on __DATE__, __TIME__
 */

package com.MyJob;

import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;

/**
 *
 * @author  __USER__
 */
public class NotePad extends javax.swing.JFrame implements ActionListener {

	/** Creates new form NotePad */
	public NotePad() {
		initComponents();                         //注册监听
		this.itemCopy.addActionListener(this);
		this.itemZhuangTai.addActionListener(this);
		this.itemSave.addActionListener(this);
		this.itemPaste.addActionListener(this);
		this.itemOpen.addActionListener(this);
		this.itemNew.addActionListener(this);
		this.itemCut.addActionListener(this);
		this.itemDelete.addActionListener(this);
		this.itemExit.addActionListener(this);
		this.itemFont.addActionListener(this);
		this.mPaste.addActionListener(this);
		this.mCut.addActionListener(this);
		this.mCopy.addActionListener(this);
		this.mDelete.addActionListener(this);
		this.setLocationRelativeTo(null);               //窗体显示位置
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// 
	private void initComponents() {

		popMenu = new javax.swing.JPopupMenu();
		mCut = new javax.swing.JMenuItem();
		mCopy = new javax.swing.JMenuItem();
		mPaste = new javax.swing.JMenuItem();
		mDelete = new javax.swing.JMenuItem();
		jScrollPane1 = new javax.swing.JScrollPane();
		ta = new javax.swing.JTextArea();
		jMenuBar1 = new javax.swing.JMenuBar();
		mFile = new javax.swing.JMenu();
		itemNew = new javax.swing.JMenuItem();
		itemOpen = new javax.swing.JMenuItem();
		itemSave = new javax.swing.JMenuItem();
		jSeparator1 = new javax.swing.JSeparator();
		itemExit = new javax.swing.JMenuItem();
		mEdit = new javax.swing.JMenu();
		itemCut = new javax.swing.JMenuItem();
		itemCopy = new javax.swing.JMenuItem();
		itemPaste = new javax.swing.JMenuItem();
		itemDelete = new javax.swing.JMenuItem();
		mStyle = new javax.swing.JMenu();
		itemFont = new javax.swing.JMenuItem();
		mSee = new javax.swing.JMenu();
		itemZhuangTai = new javax.swing.JMenuItem();
		mHelp = new javax.swing.JMenu();
		jMenuItem1 = new javax.swing.JMenuItem();
		jMenuItem2 = new javax.swing.JMenuItem();

		mCut.setText("\u526a\u5207");
		popMenu.add(mCut);

		mCopy.setText("\u590d\u5236");
		popMenu.add(mCopy);

		mPaste.setText("\u7c98\u8d34");
		popMenu.add(mPaste);

		mDelete.setText("\u5220\u9664");
		popMenu.add(mDelete);

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		ta.setColumns(20);
		ta.setRows(5);
		ta.addMouseListener(new java.awt.event.MouseAdapter() {//为鼠标注册监听
			public void mouseReleased(java.awt.event.MouseEvent evt) {
				taMouseReleased(evt);
			}
		});
		jScrollPane1.setViewportView(ta);

		mFile.setText("\u6587\u4ef6(F)");

		itemNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_N,
				java.awt.event.InputEvent.CTRL_MASK));
		itemNew.setText("\u65b0\u5efa");
		mFile.add(itemNew);

		itemOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_O,
				java.awt.event.InputEvent.CTRL_MASK));
		itemOpen.setText("\u6253\u5f00");
		mFile.add(itemOpen);

		itemSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_S,
				java.awt.event.InputEvent.CTRL_MASK));
		itemSave.setText("\u4fdd\u5b58");
		mFile.add(itemSave);
		mFile.add(jSeparator1);

		itemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_E,
				java.awt.event.InputEvent.CTRL_MASK));
		itemExit.setText("\u9000\u51fa");
		mFile.add(itemExit);

		jMenuBar1.add(mFile);

		mEdit.setText("\u7f16\u8f91(E)");

		itemCut.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_X,
				java.awt.event.InputEvent.CTRL_MASK));
		itemCut.setText("\u526a\u5207");
		mEdit.add(itemCut);

		itemCopy.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_C,
				java.awt.event.InputEvent.CTRL_MASK));
		itemCopy.setText("\u590d\u5236");
		mEdit.add(itemCopy);

		itemPaste.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_V,
				java.awt.event.InputEvent.CTRL_MASK));
		itemPaste.setText("\u7c98\u8d34");
		mEdit.add(itemPaste);

		itemDelete.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_DELETE,
				java.awt.event.InputEvent.CTRL_MASK));
		itemDelete.setText("\u5220\u9664");
		mEdit.add(itemDelete);

		jMenuBar1.add(mEdit);

		mStyle.setText("\u683c\u5f0f(O)");

		itemFont.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_F,
				java.awt.event.InputEvent.CTRL_MASK));
		itemFont.setText("\u5b57\u4f53");
		mStyle.add(itemFont);

		jMenuBar1.add(mStyle);

		mSee.setText("\u67e5\u770b(V)");

		itemZhuangTai.setText("\u72b6\u6001\u680f");
		mSee.add(itemZhuangTai);

		jMenuBar1.add(mSee);

		mHelp.setText("\u5e2e\u52a9(H)");

		jMenuItem1.setText("\u4ea7\u770b\u5e2e\u52a9");
		mHelp.add(jMenuItem1);

		jMenuItem2.setText("\u5173\u4e8e\u8bb0\u4e8b\u672c");
		mHelp.add(jMenuItem2);

		jMenuBar1.add(mHelp);

		setJMenuBar(jMenuBar1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING,
				javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING,
				javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE));

		pack();
	}// 
	//GEN-END:initComponents

	private void taMouseReleased(java.awt.event.MouseEvent evt) {      //鼠标监听的实现方法
		// TODO add your handling code here: 
		if(evt.isPopupTrigger()){                                     //如果触发鼠标右击执行操作
			this.popMenu.show(this.ta, evt.getX(),evt.getY());
		}
		
		
	}
	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new NotePad().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JMenuItem itemCopy;
	private javax.swing.JMenuItem itemCut;
	private javax.swing.JMenuItem itemDelete;
	private javax.swing.JMenuItem itemExit;
	private javax.swing.JMenuItem itemFont;
	private javax.swing.JMenuItem itemNew;
	private javax.swing.JMenuItem itemOpen;
	private javax.swing.JMenuItem itemPaste;
	private javax.swing.JMenuItem itemSave;
	private javax.swing.JMenuItem itemZhuangTai;
	private javax.swing.JMenuBar jMenuBar1;
	private javax.swing.JMenuItem jMenuItem1;
	private javax.swing.JMenuItem jMenuItem2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JMenuItem mCopy;
	private javax.swing.JMenuItem mCut;
	private javax.swing.JMenuItem mDelete;
	private javax.swing.JMenu mEdit;
	private javax.swing.JMenu mFile;
	private javax.swing.JMenu mHelp;
	private javax.swing.JMenuItem mPaste;
	private javax.swing.JMenu mSee;
	private javax.swing.JMenu mStyle;
	private javax.swing.JPopupMenu popMenu;
	private javax.swing.JTextArea ta;
	
	String clipboard;//用于剪切或是复制时临时储存数据
	
	// End of variables declaration//GEN-END:variables
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String s = e.getActionCommand();              //接收触发的按键名称
		JFileChooser jfc = new JFileChooser();        //JFileChooser用来提供一个文件对话框,可以通过其showXxxDialog打开一个模态对话框,或直接实例化并加入到其他组件。
		if(s.equals("打开")){			
			int result = jfc.showOpenDialog(this);    //创建变量接收文件选择器状态
			if(result == JFileChooser.OPEN_DIALOG){ //判断是否打开视图
				File fname = jfc.getSelectedFile();  //用fname将取出的文件名及路径保存
				FileReader fr = null;                //定义文件字节输入流,并初始化为null
				BufferedReader br = null;
				try {
					fr=new FileReader(fname);       //创建文件输入流对象,以fname为参数
					br=new BufferedReader(fr);		//创建文件输出流对象,以文件输入流对象做实参
					String line = br.readLine();	//读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。 包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 
					while (line!=null){				//判断是否读到行尾
						this.ta.append(line +"\n");//像指定的文档内添加指定字符
						line = br.readLine();		//从新读入下一行
					}
								
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}finally{
					try {
						fr.close();
						br.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}					
				}			
			}
		}		
		else if(s.equals("保存")){
			int result = jfc.showSaveDialog(this);
			if(result == JFileChooser.APPROVE_OPTION){
				File f = jfc.getSelectedFile();        //返回选中的文件。可由程序员通过 setFile 或者通过用户操作(如在 UI 中键入文件名,或者从 UI 中的列表内选择文件)来进行此设置。 
				FileWriter fw = null;
				BufferedWriter bw = null;
				try {
					fw = new FileWriter(f);        //创建文件输出流,用文件路径做参数
					bw = new BufferedWriter(fw);	//创建缓冲输出流
					String con = this.ta.getText();
					String[] strs = con.split("\n");
					for(int i = 0;i

你可能感兴趣的:(JAVA,Java,总结)