本帖最后由 smile_joe 于 2013-4-28 17:50 编辑
package cn.joe.Gui;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Notepad extends Frame {
private static final long serialVersionUID = 1L;
private MenuItem mi3;
private MenuItem mi1;
private TextArea ta;
private File file;
private String encoding = "GBK";
private MenuItem mi4;
private MenuItem mi5;
private MenuItem mi2;
public Notepad() {
// super("记事本");
initNotepad();
// 创建菜单栏
getMenuBar1();
// 添加文本域
addArea();
// 添加监听器
listenerAll();
setVisible(true);
}
private void addArea() {
ta = new TextArea();
ta.setFont(new Font("Courier New", Font.PLAIN, 15));
add(ta);
}
private void getMenuBar1() {
MenuBar menu = new MenuBar();
// 创建菜单,并添加到菜单栏
Menu m1 = new Menu("文件(F)");
Menu m2 = new Menu("编辑(E)");
Menu m3 = new Menu("查看(S)");
// 创建菜单项并给菜单上添加功能
mi1 = new MenuItem("打开");
mi2 = new MenuItem("保存");
mi3 = new MenuItem("退出");
mi4 = new MenuItem("GBK");
mi5 = new MenuItem("UTF-8");
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m3.add(mi4);
m3.add(mi5);
menu.add(m1);
menu.add(m2);
menu.add(m3);
// 添加菜单栏到窗体
setMenuBar(menu);
}
// 创建构造Notepad函数,实现Notepad实例化样式
private void initNotepad() {
setTitle("记事本");
setSize(666, 666);
setLocation(111, 99);
setLayout(new BorderLayout());
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void listenerAll() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open(e);
}
});
mi3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
private void open(ActionEvent e) {
MenuItem mi1 = (MenuItem) e.getSource();
Menu menu = (Menu) mi1.getParent();
MenuBar mb = (MenuBar) menu.getParent();
Notepad frame = (Notepad) mb.getParent();
FileDialog fd = new FileDialog(frame, "打开", FileDialog.LOAD);// 创建对话框
// FileDialog fd = new FileDialog(this, "打开", FileDialog.LOAD);//
// 创建对话框,成员方法就可以用这种
fd.setVisible(true); // 显示
String loc = fd.getDirectory();// 获取文件路径
String nam = fd.getFile();// 获取文件名字
if (loc != null && nam != null) {
file = new File(loc, nam);
loadFile(file);
}
}
private void loadFile(File file) {
try (FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) = -1) {// 读出
baos.write(b, 0, len);// 写进内存中
}
String content = new String(baos.toByteArray(), encoding);// 内存中的数据转换成字符串
// 带格式
ta.setText(content);
} catch (IOException e) {
e.getStackTrace();
}
}
}
//倒数11行-while ((len = fis.read(b)) = -1) {// 读出 一直报错
//错在哪里,头大,帮忙看看啊.. |