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();
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的Java记事本程序的框架,您可以根据您的需求进行修改和扩展。 ``` import java.awt.*; import java.awt.event.*; import java.io.*; public class SimpleNotepad extends Frame implements ActionListener { private TextArea textArea; private MenuBar menuBar; private Menu fileMenu, editMenu; private MenuItem newMenuItem, openMenuItem, saveMenuItem, exitMenuItem, cutMenuItem, copyMenuItem, pasteMenuItem; public SimpleNotepad() { setTitle("Simple Notepad"); setSize(500, 500); setLocationRelativeTo(null); textArea = new TextArea(); add(textArea); menuBar = new MenuBar(); fileMenu = new Menu("File"); newMenuItem = new MenuItem("New"); openMenuItem = new MenuItem("Open"); saveMenuItem = new MenuItem("Save"); exitMenuItem = new MenuItem("Exit"); fileMenu.add(newMenuItem); fileMenu.add(openMenuItem); fileMenu.add(saveMenuItem); fileMenu.addSeparator(); fileMenu.add(exitMenuItem); editMenu = new Menu("Edit"); cutMenuItem = new MenuItem("Cut"); copyMenuItem = new MenuItem("Copy"); pasteMenuItem = new MenuItem("Paste"); editMenu.add(cutMenuItem); editMenu.add(copyMenuItem); editMenu.add(pasteMenuItem); menuBar.add(fileMenu); menuBar.add(editMenu); setMenuBar(menuBar); newMenuItem.addActionListener(this); openMenuItem.addActionListener(this); saveMenuItem.addActionListener(this); exitMenuItem.addActionListener(this); cutMenuItem.addActionListener(this); copyMenuItem.addActionListener(this); pasteMenuItem.addActionListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); } }); } public void actionPerformed(ActionEvent e) { if (e.getSource() == newMenuItem) { textArea.setText(""); } else if (e.getSource() == openMenuItem) { FileDialog fileDialog = new FileDialog(this, "Open File", FileDialog.LOAD); fileDialog.setVisible(true); if (fileDialog.getFile() != null) { String fileName = fileDialog.getDirectory() + fileDialog.getFile(); setTitle(fileName); readFile(fileName); } } else if (e.getSource() == saveMenuItem) { FileDialog fileDialog = new FileDialog(this, "Save File", FileDialog.SAVE); fileDialog.setVisible(true); if (fileDialog.getFile() != null) { String fileName = fileDialog.getDirectory() + fileDialog.getFile(); setTitle(fileName); saveFile(fileName); } } else if (e.getSource() == exitMenuItem) { dispose(); } else if (e.getSource() == cutMenuItem) { textArea.cut(); } else if (e.getSource() == copyMenuItem) { textArea.copy(); } else if (e.getSource() == pasteMenuItem) { textArea.paste(); } } private void readFile(String fileName) { try { BufferedReader reader = new BufferedReader(new FileReader(fileName)); String line = null; while ((line = reader.readLine()) != null) { textArea.append(line + "\n"); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } private void saveFile(String fileName) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); writer.write(textArea.getText()); writer.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { SimpleNotepad notepad = new SimpleNotepad(); notepad.setVisible(true); } } ``` 这个程序提供了基本的记事本功能,包括打开、保存、剪切、复制和粘贴。您可以根据您的需要进行修改和扩展,例如添加其他菜单项或功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值