自己写的,能实现基本功能:
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
/*因为根据个人的电脑路径可能有所偏差,没有源路径的情况下,设置默认保存路径为D盘根目录下
* 若要选择保存其他地方,可以选择 另存为*/
public class TestDemo extends JFrame {
private static final long serialVersionUID = -5355432125621015300L;
private String url = null;//文件路径
private String str=null;//复制或剪切 的字符串
private StringSelection stringSelection=null;
private Clipboard clipboard=new Clipboard(str);
private Transferable transferable=null;
private DataFlavor flavor=null;
public TestDemo() {
init();
}
private void init() {
setTitle("我的记事本");
setSize(500, 600);
setContentPane(createContentPane());//添加主面板
}
/*创建主面板*/
private JPanel createContentPane() {
JPanel pane = new JPanel(new BorderLayout());
pane.add(BorderLayout.NORTH, createChocePane());//添加菜单栏
pane.add(createAreaPane());//添加文本编辑区域
return pane;
}
/*创建菜单栏,以及实现功能*/
private JPanel createChocePane() {
JPanel pane = new JPanel();
JMenuBar menuBar1 = new JMenuBar();
JMenu menu = new JMenu("文件");
menuBar1.add(menu);
JMenuItem menuIt1 = new JMenuItem("新建");
JMenuItem menuIt2 = new JMenuItem("打开");
JMenuItem menuIt3 = new JMenuItem("保存");
JMenuItem menuIt4 = new JMenuItem("另存为");
menu.add(menuIt1);
menu.add(menuIt2);
menu.add(menuIt3);
menu.add(menuIt4);
JMenuBar menuBar2 = new JMenuBar();
JMenu menu2 = new JMenu("编辑");
menuBar2.add(menu2);
JMenuItem menuIt5 = new JMenuItem("复制");
JMenuItem menuIt6 = new JMenuItem("剪切");
JMenuItem menuIt7 = new JMenuItem("粘帖");
menu2.add(menuIt5);
menu2.add(menuIt6);
menu2.add(menuIt7);
JMenuBar menuBar3 = new JMenuBar();
JMenu menu3 = new JMenu("帮助");
menuBar3.add(menu3);
JMenuItem menuIt8 = new JMenuItem("关于记事本");
menu3.add(menuIt8);
pane.add(menuBar1);
pane.add(menuBar2);
pane.add(menuBar3);
menuIt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testArea.setText(null);
}
});
menuIt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final FileDialog fd = new FileDialog(new JFrame(), "查找文件",
FileDialog.LOAD);
fd.setVisible(true);
if (fd.getDirectory() != null && fd.getFile() != null) {
testArea.setText(null);
url = fd.getDirectory() + fd.getFile();
try {
BufferedReader in = new BufferedReader(new FileReader(
url));
for (int i = 0;; i++) {
testArea.append(in.readLine());
if (in.read() == -1) {
break;
} else
continue;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
menuIt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (url==null) {
url="D:\\新建 文本文档.txt";
}
File f = new File(url);
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(url));
f.createNewFile();
out.append(testArea.getText());
out.flush();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
menuIt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FileDialog fd = new FileDialog(new JFrame(), "保存文本",
FileDialog.SAVE);
fd.setVisible(true);
if (url!=null) {
File f = new File(url);
BufferedWriter out = null;
try {
f.createNewFile();
out = new BufferedWriter(new FileWriter(url));
out.append(testArea.getText());
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
menuIt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str=testArea.getSelectedText();
stringSelection=new StringSelection(str);
clipboard.setContents(stringSelection, null);
}
});
menuIt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
str=testArea.getSelectedText();
stringSelection=new StringSelection(str);
clipboard.setContents(stringSelection, null);
int start=testArea.getSelectionStart();
int end=testArea.getSelectionEnd();
testArea.replaceRange( null,start,end);
}
});
menuIt7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
transferable=clipboard.getContents(this);
flavor=DataFlavor.stringFlavor;
if (transferable.isDataFlavorSupported(flavor)) {
int start=testArea.getSelectionStart();
int end=testArea.getSelectionEnd();
testArea.replaceRange( null,start,end);
int pos=testArea.getCaretPosition();
try {
str=(String)transferable.getTransferData(flavor);
testArea.insert(str, pos);
} catch (UnsupportedFlavorException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
menuIt8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"功能简单,绝对原创 ");
}
});
return pane;
}
JTextArea testArea;
private JScrollPane createAreaPane() {
JScrollPane pane = new JScrollPane();
pane.setBorder(new TitledBorder("编辑区域"));
testArea = new JTextArea();
testArea.setFont(new Font("宋体", Font.BOLD, 13));
testArea.setLineWrap(true);
pane.getViewport().add(testArea);
return pane;
}
public static void main(String[] args) {
TestDemo td = new TestDemo();
td.setVisible(true);
}
}
/*
* 我的记事本
* */
package com.test2;
import java.io.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class NotePad extends JFrame implements ActionListener{
//定义需要的组件
JTextArea jta=null;
//菜单条
JMenuBar jmb=null;
//第一JMenu
JMenu jm1=null;
//定义JMenuItem
JMenuItem jmi1=null;
JMenuItem jmi2=null;
//做带有按钮的写字板
public JPanel np;
public JPanel jb;
public JButton open;
public JButton save;
public static void main(final String[] args) {
// TODO Auto-generated method stub
final NotePad np=new NotePad();
}
//构造函数
public NotePad()
{
//创建JPanel
np=new JPanel();
jb=new JPanel();
//注册监听
setLayout(null);
np.setLayout(new BorderLayout());
jb.setLayout(new GridLayout(1,2));
np.setBounds(50,50,800,600);
jta=new JTextArea();
np.add(jta);
jb.setBounds(50,650,800,50);
//设置助记符
//jm1.setMnemonic('F');
open=new JButton("打开");
save=new JButton("保存");
jmi1=new JMenuItem("打开");
open.addActionListener(this);
open.setActionCommand("open");
jmi2=new JMenuItem("保存");
jb.add(open);
jb.add(save);
//对保存菜单处理
save.addActionListener(this);
save.setActionCommand("save");
//加入
this.setJMenuBar(jmb);
add(np);
add(jb);
//放入到JFrame
// this.add(jta);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(900,750);
this.setVisible(true);
}
public void actionPerformed(final ActionEvent arg0) {
//判断是哪个菜单被选中
if(arg0.getActionCommand().equals("open"))
{
//JFileChooseer
JFileChooser jfc1=new JFileChooser();
//设置名字
jfc1.setDialogTitle("请选择文件...");
//默认方式
jfc1.showOpenDialog(null);
//显示
jfc1.setVisible(true);
//得到用户选择的文件全路径
String filename=jfc1.getSelectedFile().getAbsolutePath();
//
FileReader fr=null;
BufferedReader br=null;
try {
fr=new FileReader(filename);
br=new BufferedReader(fr);
//从文件中读取信息到jta
String s="";
String allCon="";
while((s=br.readLine())!=null)
{
allCon+=s+"\r\n";
}
//放置到jta即可
jta.setText(allCon);
}catch(Exception e){
e.printStackTrace();
}finally{
//关闭文件流
try{
br.close();
fr.close();
}catch (Exception e){
}
}
}
else if(arg0.getActionCommand().equals("save"))
{
//出现保存对话框
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("另存为...");
//按默认的方式显示
jfc.showSaveDialog(null);
jfc.setVisible(true);
//得到用户希望把文件保存到何处,文件全路径
String file=jfc.getSelectedFile().getAbsolutePath();
//准备写入到指定文件
FileWriter fw=null;
BufferedWriter bw=null;
try{
fw=new FileWriter(file);
bw=new BufferedWriter(fw);
//自己优化,做个缓存
bw.write(this.jta.getText());
}catch(Exception e){
e.printStackTrace();
}finally{
try{
bw.close();
fw.close();
}catch(Exception e){
}
}
}
}
}
刚刚写的,比较简易点的,你看看吧
要看便什么程序了