package javanotpad;
public class MainFrame
{
/**
* @param args
* author javaAlpha
* 时间:2009年5月8日20:00:50
* java记事本
*/ public static void main(String[] args)
{
MenuFrame menu = new MenuFrame();
menu.initialize();
}
}
package javanotpad;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
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.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.Properties;import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;public class MenuFrame extends JFrame implements ActionListener
{
private static final long serialVersionUID = 201269579650631599L;
private String filePath;// 文件路径
private JTextArea text;// 文本区
private JScrollPane scrollPane; // 滚动条
private JMenuBar menuBar;// 菜单栏
private JMenu file, edit, view, search, document, tool, window, help;// 主菜单
private JMenuItem fileItem1, fileItem2, fileItem3, fileItem4, fileItem5,
fileItem6, editItem1, editItem2, editItem3, editItem4, editItem5,
helpItem1;// 子菜单
private Toolkit toolkit = Toolkit.getDefaultToolkit();// 获取默认工具包
private Clipboard clipboard = toolkit.getSystemClipboard();// 该剪贴板使数据能够在Java应用程序和使用本机剪贴板工具的本机应用程序之间传输
private JFileChooser fileChoose = new JFileChooser();// 文件选择对话框
private FileDialog openFileDialog = new FileDialog(this, "打开文件",
FileDialog.LOAD);// 设置打开文件对话框
private FileDialog saveAsFileDialog = new FileDialog(this, "文件另存为",
FileDialog.SAVE);// 设置另存为文件对话框 @SuppressWarnings("static-access")
// 初始化窗口
public void initialize()
{
text = new JTextArea();
scrollPane = new JScrollPane(text);
text.setBackground(Color.white);// 设置文本框的背景颜色
text.setForeground(Color.black);// 设置文本框的前景颜色
scrollPane.setViewportView(text);// 创建一个视口(如果有必要)并设置其视图 initMenuBar();// 加载菜单栏
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);// 设置关闭窗口事件
this.setTitle("Java ");// 设置窗口标题
this.setSize(600, 700);// 设置窗口大小
this.setLocationRelativeTo(null);// 设置窗口位置
this.setVisible(true);// 设置窗口可见性
// this.add(text);// 文本编辑区域
// this.add(scrollPane);
this.getContentPane().add(scrollPane);// 设置滚动条
} // 初始化菜单栏
public void initMenuBar()
{
menuBar = new JMenuBar();
file = new JMenu("文件(F)");// 文件菜单
edit = new JMenu("编辑(E)");// 编辑菜单
view = new JMenu("视图(V)");// 视图菜单
search = new JMenu("搜索(S)");// 上搜菜单
document = new JMenu("文档(D)");// 文档菜单
tool = new JMenu("工具(T)");// 工具菜单
window = new JMenu("窗口(W)");// 窗口菜单
help = new JMenu("帮助(H)");// 帮助菜单 // 添加快捷键按键响应
file.setMnemonic(KeyEvent.VK_F);// ==file.setMnemonic('F');
edit.setMnemonic(KeyEvent.VK_E);
view.setMnemonic(KeyEvent.VK_V);
search.setMnemonic(KeyEvent.VK_S);
document.setMnemonic(KeyEvent.VK_D);
tool.setMnemonic(KeyEvent.VK_T);
window.setMnemonic(KeyEvent.VK_W);
help.setMnemonic(KeyEvent.VK_H); // 文件菜单项
fileItem1 = new JMenuItem("新建(N)", 'N');
fileItem2 = new JMenuItem("打开(O)", 'O');
fileItem3 = new JMenuItem("保存(S)", 'S');
fileItem4 = new JMenuItem("另存为(A)", 'A');
fileItem5 = new JMenuItem("退出(Q)", 'Q');
fileItem6 = new JMenuItem("打印(P)", 'P'); // 添加菜单子项的CTRL按键响应
fileItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
InputEvent.CTRL_MASK));
fileItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
fileItem3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK));
fileItem4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
InputEvent.CTRL_MASK));
fileItem5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
InputEvent.CTRL_MASK));
fileItem6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,
InputEvent.CTRL_MASK)); // 添加文件子菜单项
file.add(fileItem1);
file.addSeparator();// 添加分割线
file.add(fileItem2);
file.add(fileItem3);
file.add(fileItem4);
file.addSeparator();// 添加分割线
file.add(fileItem6);
file.addSeparator();// 添加分割线
file.add(fileItem5); // 添加编辑子菜单项
editItem1 = new JMenuItem("剪切");
editItem2 = new JMenuItem("粘贴");
editItem3 = new JMenuItem("复制");
editItem4 = new JMenuItem("查找");
editItem5 = new JMenuItem("全选");
edit.add(editItem1);
edit.add(editItem2);
edit.add(editItem3);
edit.addSeparator();// 添加分割线
edit.add(editItem4);
edit.addSeparator();// 添加分割线
edit.add(editItem5); // 添加帮助子菜单项
helpItem1 = new JMenuItem("关于");
help.add(helpItem1); // 往菜单栏添加菜单项
menuBar.add(file);
menuBar.add(edit);
menuBar.add(view);
menuBar.add(search);
menuBar.add(document);
menuBar.add(tool);
menuBar.add(window);
menuBar.add(help);
this.setJMenuBar(menuBar); // 给按钮添加事件
helpItem1.addActionListener(this);
fileItem1.addActionListener(this);
fileItem2.addActionListener(this);
fileItem3.addActionListener(this);
fileItem4.addActionListener(this);
fileItem5.addActionListener(this);
} @SuppressWarnings( { "deprecation", "static-access" })
@Override
public void actionPerformed(ActionEvent e)// 关于菜单项信息
{
Object eventSource = e.getSource();// 获取当前的事件源 if (eventSource == helpItem1)// 添加帮助事件
{
JOptionPane.showMessageDialog(null,
"copyright:QQ:562265123/r/n 时间:2009年5月5日", "关于",
JOptionPane.INFORMATION_MESSAGE);
} else if (eventSource == fileItem1)// 添加新建文件事件
{
this.text.setText("");// 将内容清空
this.setTitle("新建文本");// 设置新建文本的标题
} else if (eventSource == fileItem2)// 添加打开文件对话框事件
{
openFileDialog.show();// 显示打开文件对话框
filePath = openFileDialog.getDirectory() + openFileDialog.getFile();// 获取此文件对话框的目录和选择的文件
this.setTitle(filePath);// 设置窗口的标题为打开文件的路径
if (filePath != null)
{
readFile(filePath);
} } else if (eventSource == fileItem3)// 添加保存文件事件
{ int returnVal = fileChoose.showSaveDialog(MenuFrame.this);
if (returnVal == fileChoose.APPROVE_OPTION)
{
filePath = fileChoose.getSelectedFile().getAbsolutePath();
writerFile(filePath);
} else
{
JOptionPane.showMessageDialog(null, "文件将默认保存在:/n" + filePath,
"提示", JOptionPane.INFORMATION_MESSAGE);
} } else if (eventSource == fileItem4)// 添加另存为事件
{
saveAsFileDialog.show();// 显示另存为文件对话框
filePath = saveAsFileDialog.getDirectory()
+ saveAsFileDialog.getFile();// 获取此文件对话框的目录和选择的文件 if (filePath != null)
{
writerFile(filePath);
}
} else if (eventSource == fileItem5)// 添加退出程序事件
{
System.exit(0);
}
} void readFile(String path) // 显示打开文件的内容
{
try
{
File file = new File(path);
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "gb2312"));// 编码转换 String str = "";
int size = (int) file.length();
int charRead = 0;
char[] content = new char[size];
// byte[] content = new byte[size];
while (br.ready())
{
charRead += br.read(content, charRead, size - charRead);
}
br.close();
str = new String(content, 0, charRead);
text.setText(str);
} catch (IOException e)
{
System.out.println("读取文件出错");
}
} void writerFile(String path) // 保存文件内容
{
try
{
File file = new File(path);
FileWriter fw = new FileWriter(file);
fw.write(text.getText());
fw.close();
} catch (IOException e)
{
System.out.println("保存文件出错");
}
}
}