java简单记事本 下载本文

Java简易记事本编写

课题:设计一个简单的Windows记事本,可以实现以下功能 1、文件的新建、打开、保存、另存为和退出; 2、对文件的编辑,如复制、粘贴、剪切、替换等; 3、执行完操作后,能够顺利关闭记事本。 源码:

package MyPad;

import java.awt.Event; import java.awt.FlowLayout; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File;

import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException;

import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.KeyStroke;

public class MyPad {

static String path = \;

public static void main(String[] args) { // 设置主窗体

JFrame jf = new JFrame(\);

jf.setBounds(220, 90, 800, 600);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JFrame frame = new JFrame(\替换\); frame.setBounds(220, 90, 300, 250); JLabel l1 = new JLabel(\原字符串\); frame.add(l1);

final JTextField tf1 = new JTextField(); tf1.setColumns(15); frame.add(tf1);

JLabel l2 = new JLabel(\替换为\);

final JTextField tf2 = new JTextField(); tf2.setColumns(15);

JButton button = new JButton(\确定\); frame.setLayout(new FlowLayout()); frame.add(l2); frame.add(tf2); frame.add(button);

// 设置菜单栏

JMenuBar jmbar = new JMenuBar(); jf.setJMenuBar(jmbar);

// ===============设置文件菜单===================== JMenu filemenu = new JMenu(\文件(F)\);

JMenuItem newitem = new JMenuItem(\新建(N)\); JMenuItem openitem = new JMenuItem(\打开(O)\); // openitem.setMnemonic('O');

JMenuItem saveitem = new JMenuItem(\保存(S)\); JMenuItem lsaveitem = new JMenuItem(\另存为(A)\); JMenuItem exititem = new JMenuItem(\退出(X)\); jmbar.add(filemenu); filemenu.add(newitem); filemenu.add(openitem); filemenu.add(saveitem); filemenu.add(lsaveitem); filemenu.addSeparator(); filemenu.add(exititem);

// ===============设置编辑菜单===================== JMenu editmenu = new JMenu(\编辑(E)\);

JMenuItem cutitem = new JMenuItem(\剪切(T)\); JMenuItem copyitem = new JMenuItem(\复制(C)\); JMenuItem pasteitem = new JMenuItem(\粘贴(P)\); JMenuItem replaceitem = new JMenuItem(\替换(R)\); jmbar.add(editmenu);

editmenu.add(cutitem); editmenu.add(copyitem); editmenu.add(pasteitem); editmenu.add(replaceitem);

// ===============设置帮助菜单===================== JMenu helpmenu = new JMenu(\帮助(H)\);

JMenuItem helpitem = new JMenuItem(\说明(A)\); jmbar.add(helpmenu); helpmenu.add(helpitem);

final JTextArea text = new JTextArea(); JScrollPane jsp = new JScrollPane(text); jf.add(jsp);

jf.setVisible(true);

// ===============设置快捷键=====================

newitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,

Event.CTRL_MASK)); Event.CTRL_MASK)); Event.CTRL_MASK)); Event.CTRL_MASK)); Event.CTRL_MASK)); Event.CTRL_MASK));

openitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, saveitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, cutitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, copyitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, pasteitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,

replaceitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,

Event.CTRL_MASK));

// ===============实现新建功能===================== newitem.addMouseListener(new MouseAdapter() {

@Override

public void mouseReleased(MouseEvent e) {

if (text.getText().equals(\))

text.setText(\);

int value = JOptionPane.showConfirmDialog(null, \是否

\提示\, JOptionPane.YES_NO_OPTION);

else {

要保存文本?\,