`
rainytimes
  • 浏览: 7092 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

用java写的一个小记事本

    博客分类:
  • J2SE
阅读更多
/*
 * 自己写的一个小记事本,
 * 为了方便编译,把一些用到的东西都写成了内部类了,
 * 不过感觉还有很多方面欠缺,谢谢指正!
 * 
 * 
 * 
 * **/

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;


public class EditFrame extends Frame{
 
// 添加TextArea
    TextArea ta=new TextArea();
 File currentfile=null;
 

 public void creatFrame(){
  //设置菜单栏
  MenuBar mb=new MenuBar();
  Menu menufile =new Menu("File");
  Menu menuedit =new Menu("Edit");
  Menu menuhelp=new Menu("Help");
  
  MenuItem fileitemnew=new MenuItem("New");
  MenuItem fileitemopen=new MenuItem("Open");
  MenuItem fileitemsave=new MenuItem("Save");
  MenuItem fileitemsaveas=new MenuItem("Save As");
  MenuItem fileitemexit=new MenuItem("Exit");
  
  fileitemopen.addActionListener(new OpenAction(this));
  fileitemexit.addActionListener(new ExitAction());
  fileitemsave.addActionListener(new SaveAction());
  fileitemnew.addActionListener(new NewAction(this));
  fileitemsaveas.addActionListener(new SaveAsAction(this));
  
  menufile.add(fileitemnew);
  menufile.add(fileitemopen);
  menufile.add(fileitemsave);
  menufile.add(fileitemsaveas);
  menufile.add(fileitemexit);
  
  
  
  
  MenuItem edititemcopy=new MenuItem("Copy");
  MenuItem edititemcut=new MenuItem("Cut");
  MenuItem edititempaste=new MenuItem("Paste");
  
  edititempaste.addActionListener(new PasteAction());
  edititemcut.addActionListener(new CutAction());
  edititemcopy.addActionListener(new CopyAction());
  
  menuedit.add(edititemcopy);
  menuedit.add(edititemcut);
  menuedit.add(edititempaste);
  
  
  MenuItem helpitemhelp=new MenuItem("Help");
  MenuItem helpitemabout=new MenuItem("About");
  
  helpitemhelp.addActionListener(new HelpAction());
  helpitemabout.addActionListener(new HelpAction());
  
  menuhelp.add(helpitemhelp);
  menuhelp.add(helpitemabout);
  
  
  mb.add(menufile);
  mb.add(menuedit);
  mb.add(menuhelp);
  
  
  
  //设置Frame
  this.setTitle("MenuTest");
  this.setSize(600,500);
  this.setLocation(200, 200);
  this.setResizable(false);
  this.setMenuBar(mb);
  
  this.add(ta);
  
  
  this.addWindowListener(new WindowAdapter(){
   @Override
   public void windowClosing(WindowEvent arg0) {
    System.exit(0);
   }
  });
  
  this.setVisible(true);
  
  
 }

 public static void main(String[] args){
  
  EditFrame f=new EditFrame();
     f.creatFrame(); 
     
    
  
 }
 class OpenAction implements ActionListener {
  
  Frame f;
  public OpenAction(Frame f){
   this.f=f;
   
  }

  public void actionPerformed(ActionEvent e)  {
//   JFileChooser jfc=new JFileChooser();
//   jfc.setVisible(true);
//   System.out.println("asdg");
     /*
* FileDialog(Frame parent) 
          创建一个文件对话框,用于加载文件。 
FileDialog(Frame parent, String title) 
          创建一个具有指定标题的文件对话框窗口,用于加载文件。 
FileDialog(Frame parent, String title, int mode) 
          创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。 

      * 
mode:
    static int LOAD   0
          此常量值指示文件对话框窗口的作用是查找要读取的文件。 
 static int SAVE   1
          此常量值指示文件对话框窗口的作用是查找要写入的文件。 

      * */
   FileDialog fd=new FileDialog(f,"Open",0);//0 打开对话框
   //FileDialog fd=new FileDialog(f,"Open",1);// 1 保存对话框
   fd.setVisible(true);
      if(fd.getFile()==null)return ;
      
   
   File choosefile=new File(fd.getDirectory(),fd.getFile());
   currentfile=choosefile;
      
      try {
    FileInputStream fis=new FileInputStream(choosefile);
    InputStreamReader isr=new InputStreamReader(fis);
    BufferedReader br=new BufferedReader(isr);
    
     String linestring;
     /*怎么样换行???*/
     while((linestring=br.readLine())!=null){//全部读取
      ta.append(linestring+"\n");
         
      //System.out.println(linestring);
     }
     
     //br.close();
    
    
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   
   
   
      
   
   
   
   
  
  }
  
  
 }
 
 class ExitAction implements ActionListener{

  public void actionPerformed(ActionEvent e) {
   System.exit(0);
   
  }
  
  
 }
 
 class NewAction implements ActionListener{
  
  Frame f;
  public NewAction(Frame f){
   this.f=f;
  }

  public void actionPerformed(ActionEvent arg0) {
   
   FileDialog fd=new FileDialog(f,"New",FileDialog.SAVE);
   
   fd.setVisible(true);
   
   if(fd.getFile()==null)return;
   currentfile=new File(fd.getDirectory(),fd.getFile());
   //System.out.println("asdg");
   
   
  }
 
 }

 
 class SaveAction implements ActionListener{

  public void actionPerformed(ActionEvent arg0) {
   if(currentfile!=null){
   FileOutputStream fos;
   try {
    fos = new FileOutputStream(currentfile);
    OutputStreamWriter osw=new OutputStreamWriter(fos);
    BufferedWriter bw=new BufferedWriter(osw);
    try {
     bw.write(ta.getText());
     bw.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    
    
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } 
   
   System.out.println("asdg");
   
   }else return;
   
  } 
 }


 class SaveAsAction implements ActionListener{
  
  Frame f;
  public SaveAsAction(Frame f){
   this.f=f;
   
  }

  public void actionPerformed(ActionEvent arg0) {
   
   FileDialog fd=new FileDialog(f,"Save As",FileDialog.SAVE);
   
   fd.setVisible(true);
   
   currentfile=new File(fd.getDirectory(),fd.getFile());
   SaveAction save=new SaveAction();
   save.actionPerformed(arg0);
   
  }
  
  
  
  
 }
 
// static class SetMenuState{
//  
//  MenuBar mb;
//   public  SetMenuState(MenuBar mb){
//   this.mb=mb;
//   
//  }
//  
//  public void SetMenu(){
//   
//   int i;
//   int j;
//   for(i=0;i<mb.getMenuCount();i++){
//     for(j=0;j<mb.getMenu(i).getItemCount();j++){
//    mb.getMenu(i).getItem(j).getLabel();break;
//     }
//   }
//   
//  }
//  
//  
//  
//  
// }

 class PasteAction implements ActionListener {

  public void actionPerformed(ActionEvent arg0) {
   Clipboard  clipbd=getToolkit().getSystemClipboard();//得到系统剪切板内容
    Transferable clipData =clipbd.getContents(EditFrame.this);//??????
         try {
           String clipString = 
             (String)clipData.
               getTransferData(
                 DataFlavor.stringFlavor);
           ta.replaceRange(clipString, 
             ta.getSelectionStart(), 
             ta.getSelectionEnd());
         } catch(Exception ex) {
           System.out.println("not String flavor");
         }
   
  }
  
  
  
  
 }
 
 class CutAction implements ActionListener {

  public void actionPerformed(ActionEvent arg0) {
   StringSelection clipstring=new StringSelection(ta.getSelectedText());
   Clipboard clipbd=getToolkit().getSystemClipboard();
   clipbd.setContents(clipstring,clipstring);
   ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
   
  }

 }
 class CopyAction implements ActionListener{
  
  public void actionPerformed(ActionEvent arg0){
   
   Clipboard cb=getToolkit().getSystemClipboard();
   StringSelection clipstring =new StringSelection(ta.getSelectedText());
   cb.setContents(clipstring, clipstring);
   
  }
  
 }
 
 class HelpAction implements ActionListener{

  public void actionPerformed(ActionEvent arg0) {
   final Dialog help=new Dialog(EditFrame.this,"Help");
   Label lab1=new Label("个人BLOG:http://hi.baidu.com/gosanye/blog");
   
   help.setLayout(new BorderLayout());
   
   help.add(lab1,BorderLayout.CENTER);
   
   help.setSize(300,80);
   help.setResizable(false);
   help.setLocation(400,400);
   help.addWindowListener(new WindowAdapter(){


    public void windowClosing(WindowEvent e){
     help.setVisible(false);
     
    }
    
    
   });
   help.setVisible(true);
   
  }
  
  
  
  
  
  
  
 }
 

}

 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics