Chinaunix首页 | 论坛 | 博客
  • 博客访问: 604860
  • 博文数量: 30
  • 博客积分: 8020
  • 博客等级: 中将
  • 技术积分: 1125
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-31 16:14
文章分类

全部博文(30)

文章存档

2011年(1)

2010年(3)

2009年(5)

2008年(21)

分类: Java

2008-08-25 10:44:27

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class SimpleEditor
{
     static MouseListener m1=new MouseListener() {
         public void mouseClicked(MouseEvent e) {
             
         }
         
         public void mouseEntered(MouseEvent e) {
             
         }
         public void mouseExited(MouseEvent e) {
             
         }
         public void mousePressed(MouseEvent e) {
             if(e.getSource()==jok) {
                 s=jta.getText();
                 writeFile();
             }
             else {
                 System.out.println("Cancel");
                 System.exit(1);
             }
         }
         public void mouseReleased(MouseEvent e) {
             
         }
         
     };
     static String s;
     static JTextArea jta=new JTextArea("abc",17,20);
     static JButton jok=new JButton("OK");
     static JButton jcnl=new JButton("Cancal");
     
     public static void main(String args[]) {
         readFile();
         int v=Scrollbar.VERTICAL;
         int h=Scrollbar.HORIZONTAL;
         
         JPanel jp1=new JPanel();
        JPanel jp2=new JPanel();
        JFrame jf=new JFrame("The simplest editor");
        JScrollPane jsp=new JScrollPane(jta);
        jta.setLineWrap(true);
        jta.setEditable(true);
        jta.setText(s);
        jok.addMouseListener(m1);
        jcnl.addMouseListener(m1);
        jp1.add(jsp);
        jp2.add(jok);
        jp2.add(jcnl);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.getContentPane().add(jp1,BorderLayout.NORTH);
        jf.getContentPane().add(jp2,BorderLayout.SOUTH);
        jf.setBounds(100,200,300,400);
        jf.setResizable(false);
        jf.setVisible(true);
        
         
    }
    
    public static void readFile() {
        try {
            File f=new File("save.txt");
            if(f.exists())
                System.out.println("exist!");
            if(f.canRead()) {
                FileReader fr=new FileReader(f);
                int len=(int)f.length();
                char []buff=new char[len];
                fr.read(buff,0,len);
                s=new String(buff);
                fr.close();
            }
            else {
                s=new String("");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    public static void writeFile() {
        try {
            File f=new File("save.txt");
            if(f.exists())
                System.out.println("exist!");
            
            FileWriter fw=new FileWriter(f);
            fw.write(s);
            fw.close();
            System.out.println(s);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

阅读(1094) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~