java 记事本本源代码.

  1. //handel.java
  2. package abc.jdm;
  3. import abc.jdm.*;
  4. import java.awt.Color;
  5. import java.awt.PrintJob;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import javax.swing.*;
  9. import java.io.*;
  10. import javax.print.*;
  11. import sun.rmi.runtime.GetThreadPoolAction;
  12. import java.awt.datatransfer.*; 
  13. public class handel {
  14.     public String test(String txt){
  15.         return txt.toString();
  16.     }
  17. }
  18. class newfile extends Thread implements ActionListener{
  19.     public void actionPerformed(ActionEvent e){
  20.         new newfile().start();
  21.         }
  22.     public void run(){
  23.         new MyFrame();
  24.     }
  25. }
  26. class savefile implements ActionListener{
  27.     public void actionPerformed(ActionEvent e){
  28.         JFileChooser jfile=new JFileChooser();
  29.         jfile.setFileFilter(new javax.swing.filechooser.FileFilter() {
  30.                public boolean accept(File f) { //设定可用的文件的后缀名
  31.                  if(f.getName().endsWith(".java")||f.isDirectory()){
  32.                    return true;
  33.                  }
  34.                  return false;
  35.                }
  36.                public String getDescription() {
  37.                  return "文本文件(*.txt)";
  38.                }
  39.              });
  40.         int result=jfile.showSaveDialog(null);
  41.         if(result==JFileChooser.APPROVE_OPTION){
  42.             
  43.             
  44.             File dir=jfile.getCurrentDirectory();
  45.             File file=jfile.getSelectedFile();
  46.             write(new File(dir,file.getName()),MyFrame.mytext.getText());
  47.         }
  48.         if(result==JFileChooser.CANCEL_OPTION){
  49.             
  50.         }
  51.     }
  52.     public void write(File file,String str){
  53.         try{
  54.             PrintWriter write=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),"GBK"));
  55.             write.println(str);
  56.             write.close();
  57.         }
  58.         catch(IOException e){
  59.             throw new RuntimeException(e);
  60.         }
  61.     }
  62.     
  63. }
  64. //
  65. class quitframe implements ActionListener{
  66.     public void actionPerformed(ActionEvent e){
  67.         
  68.         String a=MyFrame.mytext.getText();
  69.         if(a==""){
  70.         System.exit(0);}
  71.         else{
  72.             int result=JOptionPane.showConfirmDialog(null,"文件尚未保存,你决定要退出吗?");
  73.         if(result==JOptionPane.OK_OPTION)
  74.             System.exit(0);
  75.         else
  76.             MyFrame.mytext.setText(a);
  77.         }
  78.         
  79.     }
  80.     
  81. }
  82. //
  83. class about implements ActionListener{
  84.     public void actionPerformed(ActionEvent e){
  85.         JOptionPane.showMessageDialog(null,"该软件由java编写,开发平台为myeclipse /n 希望大家多多支持!/t 孟德军","关于",JOptionPane.INFORMATION_MESSAGE);
  86.     }
  87. }
  88. //
  89. class openfile implements ActionListener{
  90.     public void actionPerformed(ActionEvent e){
  91.         
  92.         JFileChooser jfile=new JFileChooser();
  93.         jfile.setFileFilter(new javax.swing.filechooser.FileFilter() {
  94.                public boolean accept(File f) { //设定可用的文件的后缀名
  95.                  if(f.getName().endsWith(".java")||f.isDirectory()){
  96.                    return true;
  97.                  }
  98.                  return false;
  99.                }
  100.                public String getDescription() {
  101.                  return "文本文件(*.txt)";
  102.                }
  103.              });
  104.         int result=jfile.showOpenDialog(null);
  105.         if(result==JFileChooser.APPROVE_OPTION){
  106.             File dir=jfile.getCurrentDirectory();
  107.             File file=jfile.getSelectedFile();
  108.             MyFrame.mytext.setText(read(new File(dir,file.getName())));
  109.             //new MyFrame().mytext.setText(t)
  110.         }
  111.         if(result==JFileChooser.CANCEL_OPTION){
  112.             
  113.         }
  114.     }
  115.     //
  116.     public String read(File file){
  117.         try{
  118.             BufferedReader read=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
  119.             String data=null;
  120.             StringBuffer buffer=new StringBuffer();
  121.             while((data==read.readLine())){
  122.                 buffer.append(data+"/n");
  123.                 read.close();
  124.             }
  125.             return buffer.toString();
  126.         }
  127.         catch(IOException e){
  128.             throw new RuntimeException(e);
  129.         }
  130.         
  131.     }
  132. }//
  133. //color set
  134. class colorset implements ActionListener{
  135.     public void actionPerformed(ActionEvent e)
  136.       {
  137.      //显示颜色对话框
  138.         
  139.      Color newcolor=JColorChooser.showDialog(null"颜色设置",null);
  140.         MyFrame.mytext.setBackground(newcolor);
  141.      
  142.       }
  143.     
  144.     }
  145. class printframe implements ActionListener{
  146.     public void actionPerformed(ActionEvent e){
  147.         /*PrintService service;
  148.         java.awt.PrintJob b;
  149.         */
  150.         
  151.         
  152.     }
  153.     
  154. }
  155. class all extends Frame implements ActionListener{
  156.     public void actionPerformed(ActionEvent e){
  157.         
  158.         
  159.         Clipboard clipboard=null
  160.         clipboard=getToolkit().getSystemClipboard();//获取系统剪贴板。
  161.         if(e.getActionCommand()=="复制"){
  162.         String temp=MyFrame.mytext.getSelectedText(); //拖动鼠标选取文本。 
  163.         StringSelection text=new StringSelection(temp); 
  164.         clipboard.setContents(text,null); 
  165.         }
  166.         else if(e.getActionCommand()=="粘贴"){
  167.             Transferable contents=clipboard.getContents(this); 
  168.             DataFlavor flavor= DataFlavor.stringFlavor; 
  169.             if( contents.isDataFlavorSupported(flavor)) 
  170.             try{ String str; 
  171.             str=(String)contents.getTransferData(flavor); 
  172.             MyFrame.mytext.append(str); 
  173.             }
  174.             catch(Exception x){
  175.             
  176.             }
  177.             //
  178.             else if(e.getActionCommand()=="剪切"){
  179.                  String temp=MyFrame.mytext.getSelectedText(); //拖动鼠标选取文本。 
  180.                 StringSelection text=new StringSelection(temp); 
  181.                 clipboard.setContents(text,null); 
  182.                 int start=MyFrame.mytext.getSelectionStart(); 
  183.                 int end =MyFrame.mytext.getSelectionEnd(); 
  184.                 MyFrame.mytext.replaceRange("",start,end) ; //从Text1中删除被选取的文本。 
  185.                  
  186.             }
  187.             else if(e.getActionCommand()=="全选"){
  188.                 String alltemp=MyFrame.mytext.toString();
  189.                 StringSelection alltext=new StringSelection(alltemp);
  190.                 clipboard.getContents(alltext);
  191.             }
  192.             }
  193.     } 
  194.         
  195. }
  196. class display implements ActionListener{
  197.     public void actionPerformed(ActionEvent e){
  198.         new search();
  199.     }
  200. }
  201. class osinfo implements ActionListener{
  202.     public void actionPerformed(ActionEvent e){
  203.         Runtime rt = Runtime.getRuntime();
  204.         JOptionPane.showConfirmDialog(null,"剩余内存:  "+rt.freeMemory()+" KB"+"/n"+"总内存:  "+rt.totalMemory()+" KB","系统信息",
  205.                 JOptionPane.OK_OPTION,
  206.                 JOptionPane.WARNING_MESSAGE);
  207.     }
  208.     
  209. }
 

你可能感兴趣的:(java,String,File,null,平台)