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

© 梦里听传说 中级黑马   /  2014-9-9 11:58  /  1227 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 付江涛 于 2014-9-9 12:06 编辑
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. class TextDemo1
  5. {
  6.         public static void main(String[] args)
  7.         {
  8.                 new text();
  9.         }
  10. }
  11. class text
  12. {
  13.         private Frame ck;
  14.         private TextArea wbk;
  15.         private MenuBar cdlan;
  16.         private File fi;
  17.         private Menu cd;
  18.         private MenuItem zcd1;
  19.         private MenuItem zcd2;
  20.         private MenuItem zcd3;
  21.         private MenuItem zcd4;
  22.         private FileDialog dk;
  23.         private FileDialog bc;
  24.         text()
  25.         {
  26.                 textck();
  27.         }
  28.         private void textck()
  29.         {
  30.                 ck=new Frame("我的记事本");
  31.                 wbk=new TextArea(40,50);
  32.                 cdlan=new MenuBar();
  33.                 cd=new Menu("文件");
  34.                 zcd1=new MenuItem("打开");
  35.                 zcd2=new MenuItem("保存");
  36.                 zcd3=new MenuItem("另存为");
  37.                 zcd4=new MenuItem("关闭");
  38.                 bc=new FileDialog(ck,"保0存",FileDialog.SAVE);
  39.                 dk=new FileDialog(ck,"打0开",FileDialog.LOAD);
  40.                 ck.setMenuBar(cdlan);
  41.                 ck.add(wbk);
  42.                 cdlan.add(cd);
  43.                 cd.add(zcd1);
  44.                 cd.add(zcd2);
  45.                 cd.add(zcd3);
  46.                 cd.add(zcd4);
  47.                 ck.setBounds(200,100,500,600);
  48.                 event();
  49.                 ck.setVisible(true);

  50.         }
  51.         private void event()
  52.         {
  53.                 ck.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){
  54.                 System.exit(0);}});
  55.                 zcd1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
  56.                         dk.setVisible(true);
  57.                         String s1=dk.getFile();
  58.                         String s2 =dk.getDirectory();
  59.                         //System.out.println(s1+":::"+s2);
  60.                         if (s2==null || s1==null)
  61.                         {
  62.                                 return;
  63.                         }
  64.                         BufferedReader buf=null;
  65.                         File f=new File(s2+s1);
  66.                         try
  67.                         {
  68.                                 buf=new BufferedReader(new FileReader(f));
  69.                               
  70.                                 String str=null;
  71.                                 wbk.setText("");
  72.                                 while ((str=buf.readLine())!=null)
  73.                                 {
  74.                                         wbk.append(str+"\r\n");
  75.                                 }
  76.                         }
  77.                         catch (IOException a)
  78.                         {
  79.                                 throw new RuntimeException("读取失败");
  80.                         }
  81.                         finally
  82.                         {
  83.                                 try
  84.                                 {
  85.                                         buf.close();
  86.                                 }
  87.                                 catch (IOException c)
  88.                                 {
  89.                                         throw new RuntimeException("关闭失败");
  90.                                 }
  91.                         }
  92.                 }});
  93.                 zcd3.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
  94.                                 bc.setVisible(true);
  95.                                 String s1=bc.getFile();
  96.                                 String s2=bc.getDirectory();
  97.                                 if (s2==null||s1==null)
  98.                                 {
  99.                                         return;
  100.                                 }
  101.                                 fi=new File(s2+s1);
  102.                                 BufferedWriter buf=null;
  103.                                 try
  104.                                 {
  105.                                         buf=new BufferedWriter(new FileWriter(fi));
  106.                                         String s=wbk.getText();
  107.                                         buf.write(s);
  108.                                        
  109.                                 }
  110.                                 catch (IOException c)
  111.                                 {
  112.                                         throw new RuntimeException("写入失败");
  113.                                 }
  114.                                 finally
  115.                                 {
  116.                                         try
  117.                                         {
  118.                                                 buf.close();
  119.                                         }
  120.                                         catch (IOException d)
  121.                                         {
  122.                                                 throw new RuntimeException("关闭失败");
  123.                                         }
  124.                                 }
  125.                               
  126.                        
  127.                 }});
  128.                 zcd2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
  129.                         BufferedWriter buf=null;
  130.                                 if (fi==null)
  131.                                 {
  132.                                         bc.setVisible(true);
  133.                                         String s1=bc.getFile();
  134.                                         String s2=bc.getDirectory();
  135.                                         if (s2==null||s1==null)
  136.                                         {
  137.                                                 return;
  138.                                         }
  139.                                         fi=new File(s2+s1);
  140.                                 }
  141.                                 try
  142.                                 {
  143.                                         buf=new BufferedWriter(new FileWriter(fi));
  144.                                         String s=wbk.getText();
  145.                                         buf.write(s);
  146.                                        
  147.                                 }
  148.                                 catch (IOException c)
  149.                                 {
  150.                                         throw new RuntimeException("写入失败");
  151.                                 }
  152.                                 finally
  153.                                 {
  154.                                         try
  155.                                         {
  156.                                                 buf.close();
  157.                                         }
  158.                                         catch (IOException d)
  159.                                         {
  160.                                                 throw new RuntimeException("关闭失败");
  161.                                         }
  162.                                 }
  163.                        
  164.                 }});

  165.         }
  166. }


复制代码


1 个回复

倒序浏览
V字仇杀队
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马