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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 高廷平 中级黑马   /  2012-9-1 11:18  /  1588 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//记事本的练习
import java.awt.*;
import java.awt.event.*;
import  javax.swing.*;
import java.io.*;
class  Text  extends  Frame
{
boolean eventchange=false;//属性
MenuBar mb;
Menu file;
Menu edit;

MenuItem  open;
MenuItem  save;
MenuItem  exit;

MenuItem   copy;
MenuItem   paste;

TextArea ta;

JDialog dl;
Button b1;
Button b2;

Text(String str)//构造函数
  {
   super(str);
   setSize(500,600);
   setLocation(200,100);
   
   mb=new MenuBar();
   file=new Menu("文件");
   
   open=new MenuItem("打开");
   save=new MenuItem("保存");
   exit=new MenuItem("退出");
   
   file.add(open);
   file.add(save);
   file.add(exit);
   
   edit=new Menu("编辑");
   copy=new MenuItem("复制");
   paste=new MenuItem("粘贴");
   edit.add(copy);
   edit.add(paste);
   
   
   mb.add(file);
   mb.add(edit);
   
   ta=new TextArea();
   setMenuBar(mb);
   add(ta,BorderLayout.CENTER);
   
   //一下语句设置退出的对话框
   dl=new JDialog(Text.this,"保存?");
   dl.setSize(250, 120);
         dl.setLocation(400, 200);
         dl.setResizable(false);
   dl.setLayout(new FlowLayout(FlowLayout.CENTER));
   b1=new Button("是");
   b1.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent e)
               {
                System.exit(0);
               }
        });
   Label  tf=new Label("文件内容未保存,你确定要关闭文件吗?");
   Label  tf1=new Label("不保存请单击:是,否则关闭对话框      ");
         dl.add(tf);
         dl.add(tf1);
         dl.add(b1);
   
   setVisible(true);
   
   
   //以下语句为组件添加事件监听器
   // open打开一个对话框,将选定的文件内容写入到text中
   // save打开一个对话框,将text的内容写入到指定文件中
   //exit退出窗口,如果当前内容没有保存,则提示保存内容
   //copy实现复制功能
   //paste实现粘贴功能
  open.addActionListener(new ActionListener()
   {//打开事件监听器
    public void actionPerformed(ActionEvent e)
       {
        FileDialog fd=new FileDialog(Text.this,"打开文件对话框",FileDialog.LOAD);
        fd.setVisible(true);
        String filename=fd.getDirectory()+fd.getFile();
        if(filename!=null)
        {
         try
            {//两种实现 方式
          /*FileInputStream fis=new FileInputStream(filename);
          byte []bf=new byte[10000];
          int len=fis.read(bf);
          String readset=new String(bf,0,len);
          ta.append(readset);
          fis.close();
          */
          FileReader fr=new FileReader (filename);
           //创建FileWriter对象
           char [] str=new char[10000];
           int len=fr.read(str);
           ta.setText(new String(str,0,len));
           fr.close();//关闭
         
         }
           catch(Exception ee)
           {
             ee.printStackTrace();
           }
        }
       }
  });
    save.addActionListener(new ActionListener()
   {//保存事件监听器
    public void actionPerformed(ActionEvent e)
       {
        eventchange=true;//已经保存过了
        FileDialog fd=new FileDialog(Text.this,"保存文件对话框",FileDialog.SAVE);
        fd.setVisible(true);
           String filename=fd.getDirectory()+fd.getFile();
        if(filename!=null)
        {
         try
            {
           FileWriter fw=new FileWriter(filename);
           //创建FileWriter对象
           String str=ta.getText();//声明字符串
           //getText()返回此文本组件表示的文本。默认情况下,此文本是一个空字符串。
           fw.write(str);//将字符串写入到文件中去
           fw.close();//关闭
         }
           catch(Exception ee)
           {
             ee.printStackTrace();
           }
        }
       }
     });
   
     exit.addActionListener( new ActionListener()
       //为退出添加事件监听器,响应方式:退出窗口
      {
       public void actionPerformed(ActionEvent e)
       {
        if(eventchange==true)
           //如果是真则退出窗口
           System.exit(0);
        else
         //提示保存内容
           dl.setVisible(true);//使窗体可见
         
         
       }
      });
      
    /*  copy.addActionListener(new ActionListener()
   {
    public void actionPerformed(ActionEvent e)
       {
        
       }
   });
      paste.addActionListener(new ActionListener()
   {
    public void actionPerformed(ActionEvent e)
       {
        
       }
   });
   */
      
      addWindowListener(new WindowAdapter()
   //添加窗口监听器(Adapter是适配器),为使正常退出窗口
          {
             public void windowClosing(WindowEvent e)
                 {
                       System.exit(0);
                 }
           });
  }
}
public class Mytext
{
   public static void main(String[] args)
{
     new Text("记事本!");
    }
}

1 个回复

倒序浏览
沙发,学习中...
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马