黑马程序员技术交流社区

标题: 哪里写错了 [打印本页]

作者: 陌路行者    时间: 2013-7-13 09:43
标题: 哪里写错了
本帖最后由 陌路行者 于 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. }
复制代码
找的眼都花了,


作者: 王靖远    时间: 2013-7-13 10:19
openDia = new FileDialog(f,"open",FileDialog.LOAD);
                openDia = new FileDialog(f,"closed",FileDialog.SAVE);
是不是这里错了
作者: 陌路行者    时间: 2013-7-13 10:23
王靖远 发表于 2013-7-13 10:19
openDia = new FileDialog(f,"open",FileDialog.LOAD);
                openDia = new FileDialog(f,"clos ...


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

谢啦!真的是有写错
作者: 下雨天    时间: 2013-7-13 10:36
有几处毛病:
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中比较好


作者: denghui1010    时间: 2013-7-13 10:54
private Dialog openDia,saveDia;这里改为private FileDialog openDia,saveDia;
openDia = new FileDialog(f,"open",FileDialog.LOAD);
openDia = new FileDialog(f,"closed",FileDialog.SAVE);
这里第二个改为saveDia,要不然覆盖了
这样就可以运行了,但是你这程序打开文件的时候报错,你还得仔细查查




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2