控制台的显示
Exception in thread "main" java.lang.NullPointerException
at com.zyn_1.WriteNote.myEvent(WriteNote.java:121)
at com.zyn_1.WriteNote.init(WriteNote.java:49)
at com.zyn_1.WriteNote.<init>(WriteNote.java:19)
at com.zyn_1.WriteNote.main(WriteNote.java:147)
代码如下:
package com.zyn_1;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.management.RuntimeErrorException;
public class WriteNote {
private Frame f;
private MenuBar mb;
private Menu m1,m2;
private MenuItem mi1,mi2,mi3,mi4,mi5,mi6;
private FileDialog open,save;
private TextArea ta;
private File file;
private Dialog d;
private Label l;
WriteNote(){
init();
}
public void init(){
f = new Frame("记事本");//创建frame窗体
//对窗体进行基本设置
//f.setLayout(new FlowLayout());//设置布局
f.setLocation(300, 70);//位置
f.setSize(600,640); //大小
mb = new MenuBar();
m1 = new Menu("文件");
m2 = new Menu("帮助");
mi1 = new MenuItem("新建");
mi2 = new MenuItem("打开");
mi3 = new MenuItem("保存");
mi4 = new MenuItem("另存为");
mi5 = new MenuItem("退出");
mi6 = new MenuItem("版本");
mb.add(m1);
mb.add(m2);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m1.add(mi5);
m2.add(mi6);
open = new FileDialog(f,"打开",FileDialog.LOAD);
save = new FileDialog(f,"保存",FileDialog.SAVE);
myEvent();
ta = new TextArea();
d = new Dialog(f,"版本",false);
d.setLocation(400, 250);//位置
d.setSize(300,170); //大小
d.setLayout(new FlowLayout());
l = new Label();
l.setText("制作的一个简单记事本");
d.add(l);
f.add(ta);
f.setMenuBar(mb);
f.setVisible(true);//通过setvisiable(true)显示窗体
}
public void myEvent(){
mi2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
open.setVisible(true);
String dirpath = open.getDirectory();
String FileName = open.getFile();
if(dirpath == null || FileName == null)
return;
ta.setText("");
file = new File(dirpath,FileName);
try{
BufferedReader bu = new BufferedReader(new FileReader(file));
String line = null;
while((line=bu.readLine())!=null){
ta.append(line+"\r\n");
}
bu.close();
}
catch(IOException x){
throw new RuntimeErrorException( null, "读取失败");
}
}
});
mi3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(file==null){
save.setVisible(true);
String dirpath = save.getDirectory();
String FileName = save.getFile();
if(dirpath == null || FileName == null)
return;
file = new File(dirpath,FileName);
}
try{
BufferedWriter bu = new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bu.write(text);
bu.close();
}
catch(IOException x){
throw new RuntimeErrorException( null, "读取失败");
}
}
});
mi5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
});
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
mi6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
d.setVisible(true);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new WriteNote();
}
}
yanrun
hulukid
相关分类