以前寫的java簡易記事本

來源:互聯網
上載者:User

雖然寫的很差,不過當時還是寫了很久的。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();}}

當時也沒有注釋,不過應該可以看懂的

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.