很久之前的,忘记记录了,现在贴出来
package AWT记事本;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import javax.swing.JOptionPane;
public class NoteBook extends Frame implements ActionListener {
public static void main(String[] args) {
new NoteBook().init();
}
MenuBar menubar;
TextArea textarea;
Menu filemenu, editmenu, helpmenu;
MenuItem m1, m2, m3, m4, m5, n1, n2, n3, n4, n5, h1;
String fileName;
Clipboard clipboard;
FileDialog openfliedialog, savefliedialog;
public NoteBook() {
menubar = new MenuBar();
textarea = new TextArea();
// 文件菜单
filemenu = new Menu("文件(F)");
m1 = new MenuItem("新建(N)");
m2 = new MenuItem("打开(O)");
m3 = new MenuItem("保存(S)");
m4 = new MenuItem("另存(A)");
m5 = new MenuItem("退出(X)");
// 编辑菜单
editmenu = new Menu("选项(Y)");
n1 = new MenuItem("编辑(E)");
n2 = new MenuItem("全选(E)");
n3 = new MenuItem("复制(C)");
n4 = new MenuItem("剪切(T)");
n5 = new MenuItem("粘贴(P)");
helpmenu = new Menu("帮助(H)");
h1 = new MenuItem("关于");
// 设置默认文件名
fileName = "666.txt";
// 获取默认工具包
Toolkit toolkit = Toolkit.getDefaultToolkit();
// 获取剪切板对象
clipboard = toolkit.getSystemClipboard();
// 创建,并初始化打开文件对话框和保存文件对话框
openfliedialog = new FileDialog(this, "Open File", FileDialog.LOAD);
savefliedialog = new FileDialog(this, "Save File As", FileDialog.SAVE);
}
public void init() {
filemenu.add(m1);
filemenu.add(m2);
filemenu.addSeparator();
filemenu.add(m3);
filemenu.add(m4);
filemenu.addSeparator();
filemenu.add(m5);
editmenu.add(n1);
editmenu.addSeparator();
editmenu.add(n2);
editmenu.add(n3);
editmenu.add(n4);
editmenu.add(n5);
menubar.add(filemenu);
menubar.add(editmenu);
setMenuBar(menubar);
add(textarea);
helpmenu.add(h1);
menubar.setHelpMenu(helpmenu);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
setVisible(true);
setTitle("新建文本文档.txt");
setFont(new Font("Time New Roman", Font.PLAIN, 15));
setBackground(Color.white);
setSize(500, 400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
});
// 为个菜单选项添加监听器
m1.addActionListener(this);
m2.addActionListener(this);
m3.addActionListener(this);
m4.addActionListener(this);
m5.addActionListener(this);
n2.addActionListener(this);
n3.addActionListener(this);
n4.addActionListener(this);
n5.addActionListener(this);
h1.addActionListener(this);
}
// 事件处理
@Override
public void actionPerformed(ActionEvent e) {
Object eventSource = e.getSource();
if (eventSource.equals(m1)) {
textarea.setText("已新建页面");
} else if (eventSource.equals(m2)) {
openfliedialog.setVisible(true);
fileName = openfliedialog.getDirectory() + openfliedialog.getFile();
if (fileName != null)
readFile(fileName);
} else if (eventSource.equals(m3)) {
if (fileName != null)
System.out.println("保存成功!");
writeFile(fileName);
} else if (eventSource.equals(m4)) {
savefliedialog.setVisible(true);
fileName = savefliedialog.getDirectory() + savefliedialog.getFile();
if (fileName != null)
writeFile(fileName);
} else if (eventSource.equals(n2)) {
textarea.selectAll();
} else if (eventSource.equals(n3)) {
String text = textarea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
} else if (eventSource.equals(n4)) {
String text = textarea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
textarea.replaceRange("", textarea.getSelectionStart(), textarea.getSelectionEnd());
} else if (eventSource.equals(n5)) {
Transferable contents = clipboard.getContents(this);
if (contents == null)
return;
String text;
text = "";
try {
text = (String) contents.getTransferData(DataFlavor.stringFlavor);
} catch (Exception exception) {
}
textarea.replaceRange(text, textarea.getSelectionStart(), textarea.getSelectionEnd());
} else if (eventSource.equals(h1)) {
JOptionPane.showMessageDialog(null, "暂时无版本更新信息");
} else if (eventSource.equals(m5)) {
setVisible(false);
dispose();
System.exit(0);
}
}
// 写文件
private void writeFile(String fileName) {
try {
File file = new File(fileName);
FileWriter filewriter = new FileWriter(file);
filewriter.write(textarea.getText());
filewriter.close();
} catch (IOException ee) {
System.out.println("Error Writer File");
}
}
// 读文件
private void readFile(String fileName) {
try {
File file = new File(fileName);
FileReader filerader = new FileReader(file);
int size = (int) file.length();
int charsRead = 0;
char[] content = new char[size];
while (filerader.ready())
charsRead += filerader.read(content, charsRead, size - charsRead);
filerader.close();
textarea.setText(new String(content, 0, charsRead));
} catch (IOException ee) {
System.out.println("Error opening File ");
}
}
}