创建记事本(JTextArea)

/**
 * 我的记事本(界面+功能)
 */
package com.io5;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NotePad extends JFrame implements ActionListener{
	//定义需要的组件
	JTextArea jta = null;
	//菜单条
	JMenuBar jmb = null;
	//第一JMenu
	JMenu jm1 = null;
	//定义JMenuItem
	JMenuItem jmi1 = null;
	JMenuItem jmi2 = null;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NotePad np = new NotePad();
	}
	public NotePad(){
		//创建jta
		jta = new JTextArea();
		jmb = new JMenuBar();
		jm1 = new JMenu("文件"); 
		//设置助记符
		jm1.setMnemonic('F');
		jmi1 = new JMenuItem("打开",new ImageIcon("new.gif"));
		jmi2 = new JMenuItem("保存",new ImageIcon("new.gif"));
		//加入
		this.setJMenuBar(jmb);
		//把jm1放入jmb
		jmb.add(jm1);
		//把item放入到menu
		jm1.add(jmi1);
		jm1.add(jmi2);
		jmi1.addActionListener(this);
		jmi1.setActionCommand("open");
		//对保存菜单处理
		jmi2.addActionListener(this);
		jmi2.setActionCommand("save");
		//放入JFrame,默认是Border布局
		this.add(jta);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400,300);
		this.setVisible(true);
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//判断哪个菜单被选中
		if (e.getActionCommand().equals("open")) {
//			System.out.println("open");
			//隆重给大家推荐JFileChooser
			//文件选择组件
			JFileChooser jfc1 = new JFileChooser();
			//设置名字
			jfc1.setDialogTitle("请选择文件....");
			jfc1.showOpenDialog(null);
			//显示
			jfc1.setVisible(true);
			//得到用户选择的文件的全路径
			String fileName = jfc1.getSelectedFile().getAbsolutePath();
			System.out.println(fileName);
			FileReader fr = null;
			BufferedReader br = null;
			try {
				fr = new FileReader(fileName);
//				File fileIn= new File(fileName);
//				InputStreamReader inStream = new InputStreamReader(new FileInputStream(fileIn), "GBK"); 
				br = new BufferedReader(fr);
//				br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName)), "UTF-8"));
				//从文件中读取信息并显示到jta上
				String s = "";
				String allCon = "";
				while ((s = br.readLine())!= null) {
					//显示到JTextArea上
//					System.out.println(s);
					allCon += s+"\r\n";
				}
//				allCon = new String(allCon.getBytes("Unicode"),"UTF-8");
						
				System.out.println(allCon);
				jta.setText(allCon);
			} catch (Exception e2) {
				// TODO: handle exception
			}finally {
				try {
					//一定要先关闭br,不然会报错(从大到小关闭)
					br.close();
					fr.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		}else if(e.getActionCommand().equals("save")) {
			//出现保存对话框
			JFileChooser jfc = new JFileChooser();
			jfc.setDialogTitle("另存为......");
			//按照默认的方式显示
			jfc.showSaveDialog(null);
			jfc.setVisible(true);
			//得到用户希望把文件保存到何处,文件的全路径
			String file = jfc.getSelectedFile().getAbsolutePath();
			//准备写入到指定文件
			FileWriter fw = null;
			BufferedWriter bw = null;
			try {
				fw = new FileWriter(file);
				bw = new BufferedWriter(fw);
				//自己优化
//				System.out.println(this.jta.getText());
//				bw.write(this.jta.getText());
				bw.write(this.jta.getText());
			} catch (Exception e2) {
				// TODO: handle exception
			}finally {
				try {
					bw.close();
					fw.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		}
		
	}

}

你可能感兴趣的:(java)