1. 程式人生 > >編寫一個簡單的NotePad(開啟儲存退出)

編寫一個簡單的NotePad(開啟儲存退出)

參考:韓順平 循序漸進學Java ,從入門到精通

 一、需要用到的知識:

1.Java IO 流:

IO 流主要分為兩類:位元組流字元流

從檔案讀入記憶體為輸入流,從記憶體寫到檔案中為輸出流

需要了解更深入請移步到該部落格 點選開啟連結

2.Java圖形介面

    1.選單

        JMenuBar、 JMenu 、JMenuItem

    2.文字框:

        JTextArea

    3.監聽器

        ActionListener

package Test;

/**
 * 編寫一個具有 開啟儲存退出從NotePad
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

public class NotePad extends JFrame implements ActionListener{
	JTextArea jta=null;
	JMenuBar jmb=null;
	JMenu jm1=null;
	JMenuItem jmi_open=null;
	JMenuItem jmi_save=null;
	JMenuItem jmi_exit=null;
	public static void main(String[] args) {
		NotePad notePad=new NotePad();
	}
	
	
	public  NotePad() {
		jmb=new JMenuBar();
		jm1=new JMenu("檔案");
		jmi_open=new JMenuItem("開啟");
		jmi_save=new JMenuItem("儲存");
		jmi_exit=new JMenuItem("退出");
		this.setJMenuBar(jmb);
		jmb.add(jm1);
		jm1.add(jmi_open);
		jm1.add(jmi_save);
		jm1.add(jmi_exit);
		
		//設定監聽
		jmi_open.addActionListener(this);
		jmi_open.setActionCommand("open");
		jmi_save.addActionListener(this);
		jmi_save.setActionCommand("save");
		jmi_exit.addActionListener(this);
		jmi_exit.setActionCommand("exit");
		
		
		//文字框
		jta=new JTextArea();
		this.add(jta);
		this.setSize(400,300);
		this.setVisible(true);
		this.setTitle("NotePad");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser jfc=null;
		// TODO Auto-generated method stub
		switch(e.getActionCommand()) {
		   case "open":
			   jfc=new JFileChooser();
			   jfc.setDialogTitle("請選擇開啟的檔案");
			   jfc.showOpenDialog(null);
			   jfc.setVisible(true);
			   
			   FileReader fr=null;
			   BufferedReader br=null;
			   
			   try {
				   String fileName=jfc.getSelectedFile().getAbsolutePath();
				   fr=new FileReader(fileName);
				   br=new BufferedReader(fr);
				   
				   String str="";
				   String res="";
				   
				   while((str=br.readLine())!=null) {
					   res+=str+"\r\n"; 
				   }
				   
				   jta.setText(res);
			   } catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				   e1.printStackTrace();
			   } catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}finally {
				try {
					fr.close();
					br.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			   break; 
		   case "save":
			   System.out.println("save");
			   jfc=new JFileChooser();
			   jfc.setDialogTitle("請選擇儲存的路徑");
			   jfc.setVisible(true);
			   jfc.showSaveDialog(null);
			   
			   FileWriter fw=null;
			   BufferedWriter bw=null;
			   
			   
			   try {
				   String fileName=jfc.getSelectedFile().getAbsolutePath();
				   
				   fw=new FileWriter(fileName);
				   bw=new BufferedWriter(fw);
				   bw.write(jta.getText());
			} catch (Exception e2) {
				// TODO: handle exception
			}finally {
				try {
					bw.close();
					fw.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			   break;
			   
		   case "exit":
			   System.exit(1);
		}
		
	}
}