一個簡單的記事本。版本V1.0
支援:檔案的建立,開啟,儲存,唯讀,退出(含快速鍵)
Code:
- package myclass.notepad;
-
- import java.awt.Frame;
- import java.awt.HeadlessException;
- import java.awt.event.*;
- import java.awt.MenuBar;
- import java.awt.Menu;
- import java.awt.MenuItem;
- import java.awt.MenuShortcut;
- import java.awt.CheckboxMenuItem;
- import java.awt.FileDialog;
- import java.io.FileInputStream;
- import java.io.*;
- import java.awt.TextArea;
-
- /**
- * <p>Title:記事本 </p>
- * <p>Description:簡單的記事本程式 </p>
- * <p>Copyright: Copyright (c) 2010</p>
- * <p>Company:new core </p>
- * @author not attributable
- * @version 1.0
- */
-
- public class Notepad {
- public static String path=null;
- public static void main(String[] args) throws HeadlessException {
- final Frame frame=new Frame("簡單的記事本");
- frame.setSize(800,600);
- frame.setLocation(200,200);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- MenuBar bar=new MenuBar();
- Menu menu=new Menu("檔案",true);
- menu.addSeparator();
- Menu menu1=new Menu("編輯",true);
- menu1.addSeparator();
- Menu more=new Menu("更多");
-
- final TextArea ta=new TextArea();
-
- MenuShortcut ms = new MenuShortcut(KeyEvent.VK_S, false);
- MenuShortcut ms1 = new MenuShortcut(KeyEvent.VK_O, false);
- MenuShortcut ms2 = new MenuShortcut(KeyEvent.VK_F4, false);
- MenuItem mi=new MenuItem("建立");
- mi.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- ta.setText("");
- }
- });
- MenuItem mi1=new MenuItem("開啟",ms1);
- final CheckboxMenuItem cm=new CheckboxMenuItem("唯讀",false);
- cm.addItemListener(new ItemListener() {
- public void itemStateChanged(ItemEvent e) {
- if(cm.getState()){
- ta.setEditable(false);
- }else{
- ta.setEditable(true);
- }
-
- }
- });
- mi1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- FileDialog fd=new FileDialog(frame,"開啟檔案",FileDialog.LOAD);
- fd.show();
- //System.out.println(fd.getFile());
- path=fd.getDirectory()+fd.getFile();
- //System.out.println(path);
- if(path!=null){
- try {
- FileInputStream is = new FileInputStream(path);
- byte b[]=new byte[10*1024];
- int len = is.read(b);
- if(cm.getState()){
- ta.setEditable(false);
- }
- //清空先前的內容
- ta.setText("");
- ta.append(new String(b,0,len));
- }
- catch (FileNotFoundException ex) {
- System.out.println("找不到該檔案");
- }
- catch (IOException ex1) {
- }
- }
- }
- });
-
- MenuItem separa=new MenuItem("-");
- MenuItem mi2=new MenuItem("儲存",ms);
-
- mi2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a=ta.getText();
- if(path!=null){
- BufferedWriter wri = null;
- try {
- wri = new BufferedWriter(new FileWriter(path));
- wri.write(a);
- wri.close();
- }
- catch (IOException ex1) {
- }
-
- }else{
-
- FileDialog fd=new FileDialog(frame,"儲存檔案",FileDialog.SAVE);
- fd.show();
-
- String path1=fd.getDirectory()+fd.getFile();
- try {
- BufferedWriter wri = new BufferedWriter(new FileWriter(path1+".txt"));
- wri.write(a);
- wri.close();
- }
- catch (IOException ex) {
- }
-
- }
-
- }
- });
-
- MenuItem mi3=new MenuItem("複製");
- MenuItem mi4=new MenuItem("粘貼");
- MenuItem mi5=new MenuItem("剪下");
-
- MenuItem mi7=new MenuItem("另存新檔");
- MenuItem mi8=new MenuItem("版面設定");
- MenuItem mi9=new MenuItem("列印");
- MenuItem mi10=new MenuItem("退出",ms2);
- mi10.addActionListener((new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- })
- );
-
- menu.add(mi);
- menu.add(mi1);
- menu.add(separa);
- menu.add(mi2);
- menu.add(cm);
- menu.add(more);
- menu.add(mi10);
- menu1.add(mi3);
- menu1.add(mi4);
- menu1.add(mi5);
-
- more.add(mi7);
- more.add(mi8);
- more.add(mi9);
-
- bar.add(menu);
- bar.add(menu1);
- frame.setMenuBar(bar);
- frame.add(ta);
- frame.show();
- }
-
- }