A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wangkai 中级黑马   /  2015-6-1 21:42  /  291 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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

class MMDemo

{private Frame f;
private MenuBar mb;
private Menu m;
private MenuItem mi,openItem,saveItem;
private FileDialog load,save;
private TextArea ta;
private File file;
MMDemo(){

init();
}
public void init(){
f=new Frame("my window");
f.setBounds(300,100,600,500);

mb=new MenuBar();
m=new Menu("文件");
mi=new MenuItem("退出");
openItem=new MenuItem("打开");
saveItem=new MenuItem("保存");
f.setMenuBar(mb);
mb.add(m);

m.add(openItem);
m.add(saveItem);
m.add(mi);
load=new FileDialog(f,"我要打开",FileDialog.LOAD);
save=new FileDialog(f,"我要保存",FileDialog.SAVE);
ta=new TextArea();
f.add(ta);
MyEvent();
f.setVisible(true);

}
public void MyEvent(){
saveItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
if(file==null)
{
save.setVisible(true);
String dirPath=load.getDirectory();
String fileNames=load.getFile();
if(dirPath==null || fileNames==null);
         return;
  file=new File(dirPath,fileNames);
}
try
{BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
        String text=ta.getText();
bufw.write(text);
//bufw.flush();
bufw.close();
       
}
catch (IOException ex)
{
        throw new RuntimeException("写入失败");
}
}
});
openItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
        load.setVisible(true);
        String dirPath=load.getDirectory();
        String fileNames=load.getFile();
        if(dirPath==null ||fileNames==null)
                return;
        ta.setText("");
         file=new File(dirPath,fileNames);
        try
        {
                BufferedReader bufr=new BufferedReader(new FileReader(file));
                String line=null;
                while((line=bufr.readLine())!=null)
                {
                ta.append(line+"\r\n");
                }
                bufr.close();
        }
        catch (IOException ex)
        {
                throw new RuntimeException("读取失败");
        }
        }
});
f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);
}

});
mi.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
        System.exit(0);
        }
});
}
public static void main(String[] args)
        {
                new MMDemo();
        }
}
52 无法访问的语句file=new File(dirPath,fileNames);

3 个回复

倒序浏览
不懂写的什么
回复 使用道具 举报
只能看懂一点点,把52行的return去掉就好了
回复 使用道具 举报
哥们,你的return 上面那条if语句后边有一个 ; 去掉就行了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马