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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© freehiker 中级黑马   /  2013-9-24 12:57  /  1092 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

在写图形化界面,打开保存小工具的时候,有些地方和windows稍有不同
主要是注意以下两个方面:
1.Ubuntu下的最近打开文件,无法直接获取目录路径
2.文件权限问题

大概问题如下:
windows下打开有直接目录路径,而Ubuntu下显示的是最近打开文件,这样就导致获取不到文件目录(即getDirectory()为null),
所以就需要根据获取的文件建立对象后,通过这个对象来获取文件的绝对路径(getAbsolutePath())来解决这个问题了
还有一个问题就是,一定要清楚是否有权限获取文件,有时候即使文件目录都正确,但是去没权限,那也一样会报空指针异常

现在贴上具体的代码:
需要修改的地方有注释
  1. /*
  2. * 练习题:
  3. * 制作一个图形画界面的编辑器,包含打开、保存功能,再通过包封装,
  4. * 并用jar打包,且jar包需要具备双击运行的功能
  5. */
  6. package runpack;
  7. import java.awt.*;
  8. import java.io.*;
  9. import java.awt.event.*;
  10. public class AwtRunJar {

  11.         private Frame f;
  12.         private MenuBar mb;
  13.         private Menu m;
  14.         private MenuItem openItem;
  15.         private MenuItem saveItem;
  16.         private FileDialog openDia;
  17.         private FileDialog saveDia;
  18.         private MenuItem exitItem;
  19.         private TextArea ta;
  20.         private File file;
  21.         public void init()
  22.         {
  23.                 f = new Frame("简易编辑器");
  24.                 f.setBounds(500,200,400,512);
  25.                 f.setLayout(new FlowLayout());
  26.                 mb = new MenuBar();
  27.                 m = new Menu("菜单");
  28.                 openItem = new MenuItem("打开");
  29.                 saveItem = new MenuItem("保存");
  30.                 exitItem = new MenuItem("退出");
  31.                 mb.add(m);
  32.                 m.add(openItem);
  33.                 m.add(saveItem);
  34.                 m.add(exitItem);
  35.                 ta = new TextArea(29,45);
  36.                 f.setMenuBar(mb);
  37.                 f.add(ta);
  38.                 MyEvent();
  39.                 f.setVisible(true);
  40.                
  41.         }
  42.         private void MyEvent()
  43.         {
  44.                 f.addWindowListener(new WindowAdapter()
  45.                 {
  46.                         public void windowClosing(WindowEvent e)
  47.                         {
  48.                                 System.exit(0);
  49.                         }
  50.                 });
  51.                 exitItem.addActionListener(new ActionListener()
  52.                 {
  53.                         public void actionPerformed(ActionEvent ae)
  54.                         {
  55.                                 System.exit(0);
  56.                         }
  57.                 });
  58.                 openItem.addActionListener(new ActionListener()
  59.                 {
  60.                         BufferedReader bfr;
  61.                         public void actionPerformed(ActionEvent ae)
  62.                         {
  63.                                
  64.                                 openDia = new FileDialog(f,"OPEN",FileDialog.LOAD);
  65.                                 openDia.setVisible(true);
  66.                                 //String dir = openDia.getDirectory();
  67.                                 String filename = openDia.getFile();
  68.                                 if(filename ==null) //if(dir == null || filename ==null) Ubuntu下有“最近使用的”dir会为null
  69.                                         return ;
  70.                                 file = new File(filename);
  71.                                 String filePath = file.getAbsolutePath(); //需要考虑该文件是否有权限读取
  72.                                 try
  73.                                 {
  74.                                         bfr = new BufferedReader(new FileReader(filePath));
  75.                                         String len = null;
  76.                                         System.out.println(filePath);
  77.                                         ta.setText("");
  78.                                         while((len=bfr.readLine())!=null)
  79.                                         {
  80.                                                 ta.append(len+"\r\n");
  81.                                         }
  82.                                 }
  83.                                 catch (IOException e)
  84.                                 {
  85.                                         throw new RuntimeException("文件读取失败");
  86.                                 }
  87.                                 finally
  88.                                 {
  89.                                         try
  90.                                         {
  91.                                                 if(bfr!=null);
  92.                                                         bfr.close();
  93.                                         }
  94.                                         catch (IOException io)
  95.                                         {
  96.                                                 throw new RuntimeException("文件关闭失败");
  97.                                         }
  98.                                 }
  99.                         }
  100.                 });
  101.                 saveItem.addActionListener(new ActionListener()
  102.                 {
  103.                         BufferedWriter bfw;
  104.                         public void actionPerformed(ActionEvent ae)
  105.                         {
  106.                                 if(file==null)
  107.                                 {
  108.                                         saveDia = new FileDialog(f,"保存",FileDialog.SAVE);
  109.                                         saveDia.setVisible(true);
  110.                                         String dir = saveDia.getDirectory();
  111.                                         String filename = saveDia.getFile();
  112.                                         if(dir==null||filename==null)
  113.                                                 return;
  114.                                         file = new File(dir,filename);
  115.                                         saveText();
  116.                                        
  117.                                 }
  118.                                 else
  119.                                 {
  120.                                         saveText();
  121.                                 }
  122.                                
  123.                         }
  124.                         public void saveText()
  125.                         {
  126.                                 try
  127.                                 {
  128.                                         bfw = new BufferedWriter(new FileWriter(file));
  129.                                         bfw.write(ta.getText());
  130.                                         bfw.flush();
  131.                                 }
  132.                                 catch (IOException e)
  133.                                 {
  134.                                         throw new RuntimeException("文件保存失败");
  135.                                 }
  136.                                 finally
  137.                                 {
  138.                                         try
  139.                                         {
  140.                                                 if(bfw!=null)
  141.                                                 bfw.close();
  142.                                         }
  143.                                         catch(IOException e)
  144.                                         {
  145.                                                 throw new RuntimeException("文件关闭失败");
  146.                                         }
  147.                                        
  148.                                 }
  149.                         }
  150.                 });
  151.         }
  152.        
  153.         public static void main(String[] args) {
  154.                 AwtRunJar awt =new AwtRunJar();
  155.                 awt.init();
  156.         }

  157. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

2 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马