`
Etwo
  • 浏览: 104396 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

java 编写的记事本

阅读更多

这是一个java编写的记事本

package net.etwo.notepad;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 
 * @author etwo
 * @version 1.0
 *
 */
public class TestNotepad {

	private JFrame jframe;// 主窗体
	JPanel jpanel;// 面板
	JMenuBar jmenuBar = new JMenuBar();// 菜单面板
	JMenu jmenuFile, jmenuEdit;// 主菜单
	JMenuItem jmenuItemOpen, jmenuItemNew, jmenuItemSave, jmenuItemSaveAs,
			jmenuItemExit;// 菜单中的子菜单
	JTextArea jtextArea;// 文本框
	JScrollPane jscrollPane;// 带滑动条的
	JFileChooser jfileChooser;//文件选择器
	File currentFile;

	public TestNotepad() {
		// TODO Auto-generated constructor stub
		jframe = new JFrame("我的记事本");
		jpanel = new JPanel();
		jmenuBar = new JMenuBar();
		jtextArea = new JTextArea();
		jtextArea.setLineWrap(true);//设置自动换行
		jscrollPane = new JScrollPane(jtextArea);
		jfileChooser = new JFileChooser();
		jfileChooser.setFileFilter(new FileNameExtensionFilter("文本文件(*.txt)", "txt"));
		
		jmenuFile = new JMenu("文件(F)");
		jmenuFile.setMnemonic('F');
		jmenuEdit = new JMenu("编辑(E)");
		jmenuEdit.setMnemonic('E');
		jmenuItemNew = new JMenuItem("新建(N)", 'N');
		jmenuItemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
		
		jmenuItemOpen = new JMenuItem("打开(O)", 'O');
		jmenuItemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
		jmenuItemOpen.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int res = jfileChooser.showOpenDialog(jframe);
				if(res == jfileChooser.APPROVE_OPTION) {
					currentFile = jfileChooser.getSelectedFile();
					jtextArea.setText("");
					openFile(currentFile);
					jframe.setTitle(currentFile + "-记事本");
					System.out.println("s");
				}
			}
		});
		jmenuItemSave = new JMenuItem("保存(S)", 'S');
		jmenuItemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
		jmenuItemSave.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				saveFile();
			}
		});
		jmenuItemSaveAs = new JMenuItem("另存为(A)", 'A');
		jmenuItemSaveAs.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int res = jfileChooser.showSaveDialog(jframe);
				if(res == jfileChooser.APPROVE_OPTION) {
					currentFile = jfileChooser.getSelectedFile();
					jframe.setTitle(currentFile + "-记事本");
					saveFileAs(currentFile, jtextArea.getText().replaceAll("\n", "\r\n"));
				}
			}
		});
		
		jmenuBar.add(jmenuFile);
		jmenuBar.add(jmenuEdit);
		jmenuFile.add(jmenuItemNew);
		jmenuFile.add(jmenuItemOpen);
		jmenuFile.add(jmenuItemSave);
		jmenuFile.add(jmenuItemSaveAs);
		jpanel.setLayout(new BorderLayout(2, 1));
		jpanel.add(jmenuBar, "North");
		jpanel.add(jscrollPane, "Center");
		jframe.add(jpanel);
		jframe.setSize(500, 500);
		jframe.setLocationRelativeTo(null);
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jframe.setVisible(true);
	}
	
	public void saveFile() {
		if(currentFile == null) {
			int res = jfileChooser.showSaveDialog(jframe);
			if(res == jfileChooser.APPROVE_OPTION) {
				currentFile = jfileChooser.getSelectedFile();
				jframe.setTitle(currentFile + "-记事本");
			}
		}
		if(currentFile != null) {
			saveFileAs(currentFile, jtextArea.getText().replaceAll("\n", "\r\n"));
		}
	}
	
	public void saveFileAs(File file, String text) {
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(file);
			pw.write(text);
			pw.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(pw != null) {
				pw.close();
			}
		}
	}
	
	public void openFile(File file) {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file));
			String str = br.readLine();
			while (str != null) {
				jtextArea.append(str + "\n");
				System.out.println(str);
				str = br.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(br != null) {
					br.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		new TestNotepad();
	}
}
 
1
9
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics