雖然寫的很差,不過當時還是寫了很久的。eclipse要清理了,拿來備份下吧:
package com.test.notepad;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;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.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JViewport;public class Main {JFrame frame = null;JMenuBar jmb = null;JMenu[] jm = null;JMenuItem[][] jmi = null;JScrollPane jsp = null;JViewport port = null;JTextArea content = null;JFileChooser fileChooser = null;File file = null;boolean isSaved;public Main() {isSaved = true;frame = new JFrame("記事本");frame.setLayout(new BorderLayout());frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {confirmExit();}});jmb = new JMenuBar();jm = new JMenu[2];jm[0] = new JMenu("檔案");jm[1] = new JMenu("協助");jmb.add(jm[0]);jmb.add(jm[1]);jmi = new JMenuItem[2][];jmi[0] = new JMenuItem[5];jmi[1] = new JMenuItem[2];// 檔案jmi[0][0] = new JMenuItem("建立");jmi[0][0].addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {jmi[0][3].setEnabled(true);content.setEnabled(true);isSaved = false;}});jmi[0][1] = new JMenuItem("開啟");jmi[0][1].addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {int ret = fileChooser.showOpenDialog(frame);if (ret != JFileChooser.APPROVE_OPTION) {return;}file = fileChooser.getSelectedFile();if (file == null) {return;}content.setEnabled(true);FileReader fr = null;try {fr = new FileReader(file);int i;StringBuffer sb = new StringBuffer("");while ((i = fr.read()) != -1) {sb.append((char) i);}content.setText(sb.toString());jmi[0][2].setEnabled(true);jmi[0][3].setEnabled(true);fr.close();isSaved = false;} catch (FileNotFoundException e1) {// TODO Auto-generated catch block//e1.printStackTrace();} catch (IOException e2) {// TODO Auto-generated catch block//e2.printStackTrace();}}});jmi[0][2] = new JMenuItem("儲存");jmi[0][2].setEnabled(false);jmi[0][2].addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {save();}});jmi[0][3] = new JMenuItem("另存新檔");jmi[0][3].setEnabled(false);jmi[0][3].addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {saveAs();}});jmi[0][4] = new JMenuItem("退出");jmi[0][4].addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {confirmExit();}});jm[0].add(jmi[0][0]);jm[0].add(jmi[0][1]);jm[0].add(jmi[0][2]);jm[0].add(jmi[0][3]);jm[0].add(jmi[0][4]);// 協助jmi[1][0] = new JMenuItem("關於");jm[1].add(jmi[1][0]);jsp = new JScrollPane();port = jsp.getViewport();content = new JTextArea();content.setEnabled(false);content.addKeyListener(new KeyAdapter() {public void keyReleased(KeyEvent e) {checkSave();}});port.add(content);fileChooser = new JFileChooser("e:/");frame.add(jmb, BorderLayout.NORTH);frame.add(jsp, BorderLayout.CENTER);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setBounds(400, 100, 600, 500);frame.setVisible(true);}protected void confirmExit() {if (isSaved == false) {if (JOptionPane.showConfirmDialog(null, "確定不儲存退出?", "提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {} else{if(file == null){saveAs();}else{save();}}}System.exit(0);}protected void save() {try {FileWriter fw = new FileWriter(file);String str = content.getText();System.out.println(str);fw.write(str);fw.close();isSaved = true;checkSave();} catch (IOException e1) {//e1.printStackTrace();}}protected void saveAs() {int ret = fileChooser.showSaveDialog(frame);if (ret != JFileChooser.APPROVE_OPTION) {return;}file = fileChooser.getSelectedFile();try {FileWriter fw = new FileWriter(file);fw.write(content.getText());fw.close();isSaved = true;checkSave();} catch (IOException e1) {//e1.printStackTrace();}}protected void checkSave() {//System.out.println("in func checkSave():" + file);String title = null;if(file == null){title = "untitled";}else{title = file.getName();}frame.setTitle(title);if(isSaved != true){title += " *";} else{jmi[0][2].setEnabled(true);}}public static void main(String[] args) {new Main();}}
當時也沒有注釋,不過應該可以看懂的