JAVA编写的简易记事本




  用JAVA编写了一个简易的记事本,其实,不过是JAVA课后的一个小题。

  没想到JAVA都结束,学的不好,以后有时间,还是会弄弄JAVA的

 

  1import java.lang.*;
  2import java.awt.*;
  3import java.io.*;
  4import java.awt.datatransfer.*;
  5import java.awt.event.*;
  6public class Note extends Frame implements ActionListener
  7{
  8    private MenuItem filenew=new MenuItem("新建");
  9    private MenuItem fileopen=new MenuItem("打开");
 10    private MenuItem filesave=new MenuItem("保存");
 11    private MenuItem filesaveas=new MenuItem("存为");
 12    private MenuItem fileexit=new MenuItem("退出");
 13    private MenuItem editselectall=new MenuItem("全选");
 14    private MenuItem editcopy=new MenuItem("复制");
 15    private MenuItem editcut=new MenuItem("剪切");
 16    private MenuItem editpaste=new MenuItem("粘贴");
 17    private TextArea area=new TextArea();
 18
 19    String fileName = null;
 20    Toolkit toolKit=Toolkit.getDefaultToolkit();
 21    Clipboard clipBoard=toolKit.getSystemClipboard();
 22    private FileDialog openFileDialog= new FileDialog(this,"打开文件",FileDialog.LOAD);
 23    private FileDialog saveAsFileDialog=new FileDialog(this,"文件存为",FileDialog.SAVE);
 24
 25    private class WindowCloser extends WindowAdapter
 26    {
 27        public void windowClosing(WindowEvent we)
 28        {  System.exit(0);}
 29    }

 30    private void setup()
 31    {
 32        Menu file=new Menu("文件");
 33        file.add(filenew);          filenew.addActionListener(this); 
 34        file.add(fileopen);         fileopen.addActionListener(this); 
 35        file.add(filesave);         filesave.addActionListener(this);
 36        file.add(filesaveas);       filesaveas.addActionListener(this);
 37        file.addSeparator();
 38        file.add(fileexit);         fileexit.addActionListener(this);
 39        Menu edit=new Menu("编辑");
 40        edit.add(editselectall);    editselectall.addActionListener(this);
 41        edit.addSeparator();        
 42        edit.add(editcopy);         editcopy.addActionListener(this); 
 43        edit.add(editcut);          editcut.addActionListener(this);
 44        edit.add(editpaste);        editpaste.addActionListener(this);
 45        MenuBar bar=new MenuBar();
 46        bar.add(file);
 47        bar.add(edit);
 48        setMenuBar(bar);
 49        setLayout(new BorderLayout());
 50        add("Center",area);
 51    }

 52    public Note()
 53    {
 54        super("记事本");
 55        setup();
 56        addWindowListener(new WindowCloser());
 57        setSize(400,500);
 58        show();
 59    }

 60    public void actionPerformed(ActionEvent e)
 61    {
 62        if(e.getSource()==filenew)
 63            area.setText("");
 64        if(e.getSource()==fileopen)
 65        {
 66            openFileDialog.show();
 67            fileName=openFileDialog.getDirectory()+openFileDialog.getFile();
 68            if(fileName != null)
 69                readFile(fileName);
 70        }

 71        if(e.getSource()==filesave)
 72        {
 73            if(fileName != null)
 74                writeFile(fileName);
 75        }

 76        if(e.getSource()==filesaveas)
 77        {
 78            saveAsFileDialog.show();
 79            fileName=saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
 80            if(fileName != null)
 81                readFile(fileName);
 82        }

 83        if(e.getSource()==fileexit)
 84            System.exit(0);
 85        if(e.getSource()==editselectall)
 86            area.selectAll();
 87        if(e.getSource()==editcopy)
 88        {
 89            String text=area.getSelectedText();
 90            StringSelection selection=new StringSelection(text);
 91            clipBoard.setContents(selection,null);
 92        }

 93        if(e.getSource()==editcut)
 94        {
 95            String text=area.getSelectedText();
 96            StringSelection selection=new StringSelection(text);
 97            clipBoard.setContents(selection,null);
 98            area.replaceRange("",area.getSelectionStart(),area.getSelectionEnd());
 99        }

100        if(e.getSource()==editpaste)
101        {
102            Transferable contents=clipBoard.getContents(this);
103            if(contents==nullreturn;
104            String text;
105            text="";
106            try{
107                text=(String)contents.getTransferData(DataFlavor.stringFlavor);
108               }
catch(Exception exception){
109               }

110            area.replaceRange(text,area.getSelectionStart(),area.getSelectionEnd());
111        }

112    }

113    public void readFile(String fileName)
114    {
115        area.setText("");
116        try{
117            BufferedReader in=new BufferedReader(new FileReader(fileName));
118            String line;
119            while((line=in.readLine())!=null)
120                area.append(line+"\n");
121            in.close();
122            area.setCaretPosition(0);
123           }
catch(IOException e){
124              System.out.println("打开文件错误");
125         }

126    }

127    public void writeFile(String fileName)
128    {
129        try{
130           File file = new File (fileName);
131           FileWriter writeOut = new FileWriter(file);
132           writeOut.write(area.getText());
133           writeOut.close();
134           }
catch(IOException e){
135              System.out.println("写文件错误");
136           }

137    }

138    public static void main(String srgs[])
139    {
140        Note f=new Note();
141    }

142}

143        
144

 



posted on 2005-11-05 23:08  newzpflying  阅读(4943)  评论(1)    收藏  举报