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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陌路行者 中级黑马   /  2013-7-13 09:43  /  1617 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 陌路行者 于 2013-7-13 10:37 编辑
  1. <p>import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. class MyWinDemo
  5. {
  6.         private Frame f;
  7.         private Menu fileMenu;
  8.         private MenuBar bar;
  9.         private MenuItem openItem,saveItem,exit;
  10.         private TextArea ta;
  11.         private Dialog openDia,saveDia;
  12.         private File file;
  13.         MyWinDemo()
  14.         {
  15.                 init();
  16.                
  17.         }
  18.         public void init()
  19.         {
  20.                 f = new Frame("My window");
  21.                 f.setBounds(300,240,500,420);
  22.                 bar = new MenuBar();
  23.                 f.setMenuBar(bar);
  24.                 fileMenu = new Menu("文件");
  25.                 bar.add(fileMenu);
  26.                 openItem = new MenuItem("打开");
  27.                 saveItem = new MenuItem("保存");
  28.                 exit = new MenuItem("退出");
  29.                 openDia = new FileDialog(f,"open",FileDialog.LOAD);
  30.                 openDia = new FileDialog(f,"closed",FileDialog.SAVE);
  31.                 fileMenu.add(openItem);
  32.                 fileMenu.add(saveItem);
  33.                 fileMenu.add(exit);
  34.                 ta = new TextArea();
  35.                 f.add(ta);
  36.                 myEvent();



  37.                 f.setVisible(true);
  38.         }
  39.         public void myEvent()
  40.         {
  41.                 openItem.addActionListener(new ActionListener()
  42.                 {
  43.                         public void actionPerformed(ActionEvent e)
  44.                         {
  45.                                 openDia.setVisible(true);
  46.                                 String dirPath = openDia.getDirectory();
  47.                                 String fileName = openDia.getFile();
  48.                                 if(dirPath==null || fileName == null)
  49.                                         return;
  50.                                 file = new File(dirPath,fileName);
  51.                                 try
  52.                                 {
  53.                                         BufferedReader bufr = new BufferedReader(new FileReader(file));
  54.                                         String line = null;
  55.                                         while((bufr.readLine())!=null)
  56.                                         {
  57.                                                 ta.append(line);
  58.                                                 //bufr.flush();
  59.                                         }
  60.                                         bufr.close();
  61.                                 }
  62.                                 catch (IOException ex)
  63.                                 {
  64.                                         throw new RuntimeException("文件读取失败");
  65.                                 }
  66.                         }
  67.                 });
  68.                 f.addWindowListener(new WindowAdapter()
  69.                 {
  70.                         public void windowClosing(WindowEvent e)
  71.                         {
  72.                                 System.exit(0);
  73.                         }
  74.                 });
  75.                 exit.addActionListener(new ActionListener()
  76.                 {
  77.                         public void actionPerformed(ActionEvent e)
  78.                         {
  79.                                 System.exit(0);
  80.                         }
  81.                 });
  82.         }
  83. }
  84. class  MyWindow
  85. {
  86.         public static void main(String[] args)
  87.         {
  88.                 new MyWinDemo();
  89.         }
  90. }
复制代码
找的眼都花了,

评分

参与人数 1技术分 +1 收起 理由
杜光 + 1 呵呵 这个需要慢慢调试。。

查看全部评分

9 个回复

倒序浏览
openDia = new FileDialog(f,"open",FileDialog.LOAD);
                openDia = new FileDialog(f,"closed",FileDialog.SAVE);
是不是这里错了
回复 使用道具 举报
王靖远 发表于 2013-7-13 10:19
openDia = new FileDialog(f,"open",FileDialog.LOAD);
                openDia = new FileDialog(f,"clos ...

回复 使用道具 举报
哦我知道了。Dialog中没有你调用的两个方法。你这是多态中父类引用指向子类实例,能调用的方法要看左边的父类。你需要做一下向下转型就OK了。
回复 使用道具 举报
我在MyEclipse上看到了,可是为什么毕老师就没哟转型的动作
回复 使用道具 举报
private Dialog openDia,saveDia; 你看看毕老师的代码这里是不是用的Dialog还是FileDialog。如果没有的话不转型是肯定通不过的。
回复 使用道具 举报
哦! 我说呢,怎么就是找不到方法呢。  写错啦
回复 使用道具 举报
王靖远 发表于 2013-7-13 10:32
private Dialog openDia,saveDia; 你看看毕老师的代码这里是不是用的Dialog还是FileDialog。如果没有的话 ...

谢啦!真的是有写错
回复 使用道具 举报
有几处毛病:
1,Dialog openDia,saveDia; 改成:fileDialog openDia,saveDia;

2, openDia = new FileDialog(f,"open",FileDialog.LOAD);
    openDia = new FileDialog(f,"closed",FileDialog.SAVE);  这个自己找吧

3, while((bufr.readLine())!=null)
    {
              ta.append(line);

               //bufr.flush();
     }
你没发现少点东西吗: (line=bufr.readLine())!=null
还有要这样写:ta.append(line+"\r\n")来换行

4, bufr.close();
这句话感觉放在finally中比较好

回复 使用道具 举报
private Dialog openDia,saveDia;这里改为private FileDialog openDia,saveDia;
openDia = new FileDialog(f,"open",FileDialog.LOAD);
openDia = new FileDialog(f,"closed",FileDialog.SAVE);
这里第二个改为saveDia,要不然覆盖了
这样就可以运行了,但是你这程序打开文件的时候报错,你还得仔细查查
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马