第十四周课程总结&实验报告(简单记事本的实现)

package test;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

public class Jsb implements ActionListener{

	JFrame frame;    //窗体
	JMenuBar bar;   //菜单栏
	
	//菜单
	JMenu menuFile,edit,format,see,help;

	//文本框
	JTextArea area;
	
	
	//菜单项
	JMenuItem newItem,openItem,savaItem,closeItem,cutItem,copyItem,pasteItem,typefaceItem;
	
	JFileChooser chooser;
	File file;

	
	public Jsb(){
		
		frame = new JFrame("记事本");
		bar = new JMenuBar();
		menuFile = new JMenu("文件");
		edit = new JMenu("编辑");
		format = new JMenu("格式");
		see = new JMenu("查看");
		help = new JMenu("帮助");
		
		area = new JTextArea();
		//文件菜单项
		newItem = new JMenuItem("新建(N)");
		openItem = new JMenuItem("打开(O)");
		savaItem = new JMenuItem("另存为(A)");
		closeItem = new JMenuItem("退出(X)");
		//编辑菜单项
		cutItem = new JMenuItem("剪切(T)");
		copyItem = new JMenuItem("复制(C)");
		pasteItem = new JMenuItem("粘贴(P)");
		//格式菜单项
		typefaceItem = new JMenuItem("字体(F)");
		
		frame.getContentPane().add(new JScrollPane(area)); //文本框加滚动条
		

		
		//助记符
		newItem.setMnemonic(KeyEvent.VK_N);
		openItem.setMnemonic(KeyEvent.VK_O);
		savaItem.setMnemonic(KeyEvent.VK_A);
		closeItem.setMnemonic(KeyEvent.VK_X);
		
		cutItem.setMnemonic(KeyEvent.VK_T);
		copyItem.setMnemonic(KeyEvent.VK_C);
		pasteItem.setMnemonic(KeyEvent.VK_P);
		
		
		typefaceItem.setMnemonic(KeyEvent.VK_F);
		
		
		
		//快捷键
		newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,java.awt.Event.CTRL_MASK));
		openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,java.awt.Event.CTRL_MASK));
		savaItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,java.awt.Event.CTRL_MASK));
		closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,java.awt.Event.CTRL_MASK));
		
		cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,java.awt.Event.CTRL_MASK));
		copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,java.awt.Event.CTRL_MASK));
		pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,java.awt.Event.CTRL_MASK));
		
		
		menuFile.add(newItem);
		menuFile.add(openItem);
		menuFile.add(savaItem);
		menuFile.addSeparator();
		menuFile.add(closeItem);
		
		edit.add(cutItem);
		edit.add(copyItem);
		edit.add(pasteItem);
		
		format.add(typefaceItem);
		
		bar.add(menuFile);
		bar.add(edit);
		bar.add(format);
		
		newItem.addActionListener(this);
		openItem.addActionListener(this);
		savaItem.addActionListener(this);
		closeItem.addActionListener(this);
		
		frame.setJMenuBar(bar);
		frame.setLocation(500,250);
		frame.setVisible(true);
		frame.setSize(800,600);
	}

		public void actionPerformed(ActionEvent event) {
			Object obj = event.getSource();
			if(obj instanceof JMenuItem) {
				 JMenuItem item = (JMenuItem)obj;
				 
				 if(item == newItem) {                        //新建
					 new Jsb();
				 }else if(item == openItem) {			      //打开
					 chooser = new JFileChooser();
					 chooser.showOpenDialog(null);
					 file = chooser.getSelectedFile();
					 try {
						FileInputStream fis = new FileInputStream(file);
						byte b[] = new byte[fis.available()];
						fis.read(b);
						area.append(new String(b));
						fis.close();
					} 
					catch(FileNotFoundException e) {
							e.printStackTrace();
					}catch (IOException e) {
						e.printStackTrace();
					} 
				 }else if(item == savaItem) {                  //另存为
					 chooser = new JFileChooser();
					 chooser.showSaveDialog(null);
					 file = chooser.getSelectedFile();
					 try {
							 if(!file.exists())
							   file.createNewFile();
							 
							 FileOutputStream fos = new FileOutputStream(file);
							 byte[] b = area.getText().getBytes();
							 fos.write(b);
							 fos.close();
						 } catch (IOException e) {						
							e.printStackTrace();
						}
				 }else if(item == closeItem) {               //关闭
					 System.exit(1);
				 }
			}

	}
		

}
			

posted @ 2019-11-29 16:08  嘞嘞滋  阅读(130)  评论(0编辑  收藏  举报