package demo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
 
public class Calculator {
    
    JFrame f;
    MenuBar mb;    
    Menu mu;      
    JTextArea jta;
    MenuItem openItem, saveItem, closeItem;   
    FileDialog openDia,saveDia;   
    
    File file;
    
    public Calculator()
    {
        init();
    }
    public void init()
    {
        f=new JFrame("简易记事本");
        mb=new MenuBar();
        mu=new Menu("文件");
        openItem=new MenuItem("打开");
        saveItem=new MenuItem("保存");
        closeItem=new MenuItem("退出");
        jta=new JTextArea();
        
        f.add(jta);
        
        mu.add(openItem);
        mu.add(saveItem);
        mu.add(closeItem);
        
        mb.add(mu);
        
        f.setMenuBar(mb);
        
        openDia=new FileDialog(f, "打开", FileDialog.LOAD);
        saveDia=new FileDialog(f, "保存", FileDialog.SAVE);
        
        f.setBounds(200, 300, 500, 400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        

        event();
        
    }
    
    public void event()
    {
        openItem.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e) {
                
                openFile();
                            
            }
            
            
        });
        

        saveItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) {
             saveFile();    
                
            }
            
            
        });
    
        jta.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent e){
    
                if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_S)
                {
                     saveFile();    
                }
            }
        });
        
    
        closeItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
                
            }
            
        });
    }
    

    public void openFile()
    {
        openDia.setVisible(true); 
        String dirPath=openDia.getDirectory();
        String fileName=openDia.getFile();
        
    
        if(dirPath==null || fileName==null)
            return ;
        
        jta.setText(""); 
        file=new File(dirPath,fileName); 
        
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(file));
 
            String line = null;
 
            while((line=br.readLine())!=null)
            {
                jta.append(line+"\r\n");
            }
 
            br.close();
        }
        catch (IOException ex)
        {
            throw new RuntimeException("读取失败");
        }
    }
    public void saveFile()
    {
    
        if(file==null)
        {
            saveDia.setVisible(true);
            
            String dirPath = saveDia.getDirectory();
            String fileName = saveDia.getFile();
            
            if(dirPath==null || fileName==null)
                return ;    
            file = new File(dirPath,fileName);                
        }
        

        try {
            BufferedWriter bw=new BufferedWriter(new FileWriter(file));
            
            String info=jta.getText();  
            
            bw.write(info); 
            bw.flush();
            bw.close();
            
        } catch (IOException e1) {
            
            throw new RuntimeException();
        }        
        
    }
    
    
    public static void main(String[] args) {
        
          
        new Calculator();
    }
 
}

运行截图

 

 

 

Posted on 2019-11-22 22:56  晨晨果  阅读(129)  评论(0编辑  收藏  举报