码迷,mamicode.com
首页 > 其他好文 > 详细

记事本

时间:2019-11-23 00:03:02      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:set   inf   bsp   tcl   listener   读取   load   dir   mic   

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();
    }
 
}

运行截图

 

 

 

技术图片

记事本

标签:set   inf   bsp   tcl   listener   读取   load   dir   mic   

原文地址:https://www.cnblogs.com/chenguohhw/p/11914748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!