您的位置:首页 > 产品设计 > UI/UE

GUI学习笔记MyMenuTest.java(2)

2011-12-03 18:51 260 查看
 
import java.awt.*;

import java.awt.event.*;

import java.io.*;//未学不懂。。

//保存的功能有bug

//存储时要保存两次才有。

//关闭时优化了确认对话框

//关闭跳出对话框(无操作)

//退出界面跳出文件框。。

 //对话框关闭无效

  //保存异常

  //取消异常(取消成关闭程序)

//文件名不变

class MyMenuTest 

{

 private Frame f;

 private FileDialog fd,saveFD;

 private TextArea ta;

 private MenuBar mb;

 private Menu m;

 private MenuItem closeItem,openFileItem,saveFileItem;

 private File file;

 private Label b;

 private Dialog d;

 private Button saveBut,unSaveBut,quitBut;

 

 MyMenuTest()

 {

  init();

 }

 public void init()

 {

  f=new Frame("我的打开保存文件测试");

  f.setBounds(300,200,600,500);

  

  

  fd=new FileDialog(f,"打开",FileDialog.LOAD);

  saveFD=new FileDialog(f,"保存",FileDialog.SAVE);

  

  mb=new MenuBar();//只是一个Bar 参数为空

  

  m=new Menu("文件");

  

  openFileItem=new MenuItem("打开");

  saveFileItem=new MenuItem("保存");

  closeItem=new MenuItem("退出");

  

  d=new  Dialog (f,"退出",true);

  b=new Label("文件是否保存修改",Label.CENTER);

  saveBut=new Button("保存");

  unSaveBut=new Button("不保存");

  quitBut=new Button("取消");

  d.setBounds(300,200,300,250);

  d.setLayout(new FlowLayout());

  

  ta=new TextArea();

  

  

  

  f.setMenuBar(mb);

  mb.add(m);

  m.add(openFileItem);

  m.add(saveFileItem);

  m.add(closeItem);

  f.add(ta);

  myEvent();

  d.add(b);

  d.add(saveBut);

  d.add(unSaveBut);

  d.add(quitBut);

  f.setVisible(true);

 }

 public void myEvent()

 {

  openFileItem.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

    

     fd.setVisible(true);

     String dirPath=fd.getDirectory();

     String fileName=fd.getFile();

     //System.out.println(dirPath+"...."+fileName);

     if(dirPath==null||fileName==null)

      return;

     ta.setText("");

     file=new File(dirPath,fileName);

     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("读取失败");

     }

     

   }

  }

  );

  saveFileItem.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

     if(file==null)

     {

     saveFD.setVisible(true);

     String dirPath=saveFD.getDirectory();

     String fileName=saveFD.getFile();

     if(dirPath==null||fileName==null)

      return;

     file=new File(dirPath,fileName); 

     }

    else{

     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("存储失败");

      }

     }

    

     

     

   }

  }

  );

  f.addWindowListener(new WindowAdapter()

  {

   public void windowClosing(WindowEvent e)

   {

    

     if(file==null)

      d.setVisible(true);

     System.exit(0);

    

    

   } 

     

  }

  );

  ta.addKeyListener(new KeyAdapter()

  {

   

   public void keyPressed(KeyEvent e)

   {

    if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_S)

    {

       if(file==null)

      {

      saveFD.setVisible(true);

      String dirPath=saveFD.getDirectory();

      String fileName=saveFD.getFile();

      if(dirPath==null||fileName==null)

       return;

      file=new File(dirPath,fileName); 

      }

    

      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("存储失败");

     }

     

    

     

     

    }

   }

  }

  );

  saveBut.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

     if(file==null)

     {

     saveFD.setVisible(true);

     String dirPath=saveFD.getDirectory();

     String fileName=saveFD.getFile();

     if(dirPath==null||fileName==null)

      return;

     file=new File(dirPath,fileName); 

     }

    else{

     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("存储失败");

      }

     }

    

     

     

   }

  }

  );

  unSaveBut.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

    System.exit(0);

   }

  }

  );

  quitBut.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

    d.setVisible(false);

   }

  }

  );

  

  

  closeItem.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

    d.setVisible(true);

   }

  }

  );

 }

 public static void main(String[] args)

 {

  new MyMenuTest();

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息