用java写了一个类似于winddows的记事本,主要实现新建,打开,保存,另存为,关闭,退出等功能,
这是源代码:
/**
* @time 2011-10-11 下午05:03:27
* @author Rookie
* @version 1.0
* @function 记事本:实现新建,打开,保存,另存为,关闭,退出等功能
*
*/
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;
public class MyNotepad
{
static JFrame frame;
static JScrollPane textScroll = null;
static JTextArea textArea = null;
public static final int FRAME_WIDTH = 800;
public static final int FRAME_HEIGHT = 600;
public static final void init()
{
frame = new JFrame("记事本");
// ImageIcon image = new ImageIcon("src/myPicture.jpg");
// frame.setIconImage(image.getImage());
// 1、菜单条
MenuBar menuBar = new MenuBar();
frame.setMenuBar(menuBar);
// 2、创建菜单
Menu fileMenu = new Menu("文件(F)");
Menu editMenu = new Menu("编辑(E)");
Menu formatMenu = new Menu("格式(O)");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(formatMenu);
// 3、文件子菜单项
MenuItem openFile = new MenuItem("打开文件", new MenuShortcut(0x4F, true));
MenuItem newFile = new MenuItem("新建", new MenuShortcut(0x4E, true));
MenuItem save = new MenuItem("保存", new MenuShortcut(0x53, false));
MenuItem saveAs = new MenuItem("另存为", new MenuShortcut(0x53, true));
MenuItem close = new MenuItem("关闭", new MenuShortcut(0x57, false));
MenuItem exit = new MenuItem("退出");
fileMenu.add(newFile);
fileMenu.add(openFile);
fileMenu.insertSeparator(2);
fileMenu.add(close);
fileMenu.insertSeparator(4);
fileMenu.add(save);
fileMenu.add(saveAs);
fileMenu.add(exit);
// 创建文本域
textArea = new JTextArea();
// 创建滚动条
textScroll = new JScrollPane(textArea);
frame.add(textScroll, BorderLayout.CENTER);
// 1、打开文件处理事件
openFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FileDialog openFileDialog = new FileDialog(frame, "打开",
FileDialog.LOAD);
openFileDialog.setVisible(true);
textArea.setVisible(true);
// 得到文件名
String openedFileName = openFileDialog.getFile();
String openedFileDirectory = openFileDialog.getDirectory();
String openedFilePath = openedFileDirectory + openedFileName;
if (openedFileName.equals("") || openedFileName == null)
{
frame.setTitle("无标题 - 记事本");
}
else
{
frame.setTitle(openedFileName + " - 记事本");
}
try
{
writerBuffertoJTextArea(openedFilePath, textArea);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
});
// 2、 新建事件处理
newFile.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
textArea.setVisible(true);
frame.setTitle("无标题 - 记事本");
textArea.setText("");
}
});
// 3、 关闭文件事件
close.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textArea.setVisible(false);
// textArea = null;
}
});
// 4、保存文件事件
save.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
FileDialog saveFileDialog = new FileDialog(frame, "保存",
FileDialog.SAVE);
saveFileDialog.setVisible(true);
// 得到文件名
String saveFileName = saveFileDialog.getFile();
String saveFileDirectory = saveFileDialog.getDirectory();
String saveFilePath = saveFileDirectory + saveFileName;
if (saveFileName.equals("") || saveFileName == null)
{
frame.setTitle("无标题 - 记事本");
}
else
{
frame.setTitle(saveFileName + " - 记事本");
}
try
{
textBufferToFile(textArea, saveFilePath);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
});
// 5、另存为事件处理
saveAs.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
FileDialog saveAsFileDialog = new FileDialog(frame, "另存为",
FileDialog.SAVE);
saveAsFileDialog.setVisible(true);
// 得到文件名
String saveAsFileName = saveAsFileDialog.getFile();
String saveAsFileDirectory = saveAsFileDialog.getDirectory();
String svaeAsFilePath = saveAsFileDirectory + saveAsFileName;
if (saveAsFileName.equals("") || saveAsFileName == null)
{
frame.setTitle("无标题 - 记事本");
}
else
{
frame.setTitle(saveAsFileName + " - 记事本");
}
try
{
textBufferToFile(textArea, svaeAsFilePath);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
});
// 6、退出事件处理
exit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
// 3.2、编辑子菜单项
MenuItem copy = new MenuItem("复制", new MenuShortcut(0x43, false));
MenuItem cut = new MenuItem("剪切", new MenuShortcut(0x58, false));
MenuItem paste = new MenuItem("粘贴", new MenuShortcut(0x56, false));
MenuItem edition = new MenuItem("版本");
editMenu.add(copy);
editMenu.add(cut);
editMenu.add(paste);
editMenu.add(edition);
editMenu.insertSeparator(3);
// 3.3 格式字菜单项
MenuItem autoLine = new MenuItem("自动换行");
MenuItem font = new MenuItem("字体");
formatMenu.add(autoLine);
formatMenu.add(font);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setLocation(100, 20);
frame.setVisible(true);
}
public static void main(String[] args)
{
init();
}
// 把一个文件里面的内容写入文本框
public static final void writerBuffertoJTextArea(String fileFrom,
JTextComponent text) throws IOException
{
InputStream inBuffer = null;
OutputStream outBuffer = null;
InputStream in = null;
OutputStream out = null;
try
{
in = new FileInputStream(fileFrom);
inBuffer = new BufferedInputStream(in);
StringBuffer sb = new StringBuffer();
while (true)
{
byte[] b = new byte[1024];
int bytedata = inBuffer.read(b);
sb = sb.append(new String(b));
if (bytedata == -1)
{
break;
}
}
text.setText(sb.toString());
}
finally
{
if (inBuffer != null)
{
inBuffer.close();
}
if (outBuffer != null)
{
outBuffer.close();
}
}
}
// 把文本框里面的内容写入一个文件中
// 写文件
public static final void textBufferToFile(JTextComponent text, String fileTo)
throws IOException
{
FileOutputStream out = null;
OutputStreamWriter outWriter = null;
BufferedWriter bufferedWriter = null;
try
{
out = new FileOutputStream(fileTo, false);
outWriter = new OutputStreamWriter(out);
bufferedWriter = new BufferedWriter(outWriter);
String content = new String(text.getText());
bufferedWriter.write(content);
}
finally
{
if (bufferedWriter != null)
{
bufferedWriter.close();
}
if (outWriter != null)
{
outWriter.close();
}
if (out != null)
{
out.close();
}
}
}
}