当前位置: 代码迷 >> J2SE >> 初学者~JAVA记事本
  详细解决方案

初学者~JAVA记事本

热度:256   发布时间:2016-04-24 15:35:19.0
菜鸟求助~JAVA记事本
我做的记事本怎么一直打开不了文件,也刷新不了

很是郁闷,代码如下,希望得到高手指点

import   java.awt.*;
import   java.awt.event.*;
import   java.io.*;
import   java.awt.datatransfer.*;

public   class   MyMemo   extends   Frame   implements   ActionListener{
private   TextArea   editor=new   TextArea();
private   TextArea   editor1=new   TextArea();//another   textarea
private   MyFile   mf=new   MyFile(this);

public   MyMemo(String   title){
super(title);
MyMenuBar   mb=new   MyMenuBar(this);

mb.addMenus(new   String[]{ "File ", "Do "});
mb.addMenuItem(0,new   String[]{ "New ", "Open ", "Save ", "Exit "});
mb.addMenuItem(1,new   String[]{ "Point ", "Chart "});

add(editor);
mb.addActionListener(this);

add(editor1);
mb.addActionListener(this);//add   a   new   textarea

addWindowListener(new   WindowAdapter(){
public   void   windowClosing(WindowEvent   e){
MyMemo.this.dispose();
}
});

}

public   void   actionPerformed(ActionEvent   e){
String   selected=e.getActionCommand();

if(selected.equals( "New "))
    editor.setText( " ");
   
else   if(selected.equals( "Open ")){
    try{editor.setText(mf.getData());
    }catch(IOException   ie){}
}

else   if(selected.equals( "Save ")){
try{
mf.setData(editor.getText());
}catch(IOException   ie){}
}

else   if(selected.equals( "Exit ")){
dispose();
}
}

public   static   void   main(String[]   args){
MyMemo   memo=new   MyMemo( "Compiler ");
memo.setSize(600,600);
memo.setVisible(true);
}

}


class   MyMenuBar   extends   MenuBar{
public   MyMenuBar(Frame   parent){
parent.setMenuBar(this);
}
public   void   addMenus(String[]   menus){
for(int   i=0;i <menus.length;i++)
add(new   Menu(menus[i]));
}
public   void   addMenuItem(int   menuNumber,String[]   items){
for(int   i=0;i <items.length;i++){
if(items[i]!=null)
    getMenu(menuNumber).add(new   MenuItem(items[i]));
else   getMenu(menuNumber).addSeparator();
}
}
public   void   addActionListener(ActionListener   al){
for(int   i=0;i <getMenuCount();i++)
    for(int   j=0;j <getMenu(i).getItemCount();j++)
        getMenu(i).getItem(j).addActionListener(al);
}
}


class   MyFile   {
private   FileDialog   fDlg;  

public   MyFile(Frame   parent){
fDlg=new   FileDialog(parent, " ",FileDialog.LOAD);
}
private   String   getPath(){
return   fDlg.getDirectory()+ "\\ "+fDlg.getFile();
}
public   String   getData()   throws   IOException{
fDlg.setTitle( "Open ");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader   br=new   BufferedReader(new   FileReader(getPath()));
  相关解决方案