java 記事本代碼

來源:互聯網
上載者:User

java 記事本代碼

代碼:

 

package qqq;import java.awt.Color;import java.awt.Font;import java.awt.GraphicsEnvironment;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileReader;import java.io.FileWriter;/** * @author 賀榮偉 * Created Date: 2015/11/28 * */import javax.swing.JCheckBoxMenuItem;import javax.swing.JColorChooser;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JList;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.undo.UndoManager; // 對菜單活動事件撤銷的實現public class aaa extends JFrame{/* *  *  */UndoManager manager = new UndoManager();JTextArea text = new JTextArea();JFileChooser jfc = new JFileChooser();String jsb = "建立記事本";File file;JMenuBar menu;JMenu File_bar, Edit_bar, Format_bar, View_bar, Help_bar;JMenuItem File_bar_creat, File_bar_open, File_bar_save, File_bar_othersave, File_bar_exit;JMenuItem Edit_bar_Revoke, Edit_bar_shear, Edit_bar_copy, Edit_bar_paste, Edit_bar_delete;JMenuItem Format_bar_hl, m30, Format_bar_ztxz, Format_bar_ztsz, View_bar_about, Help_bar_help;/* 檔案格式過濾器 */public class filter extends javax.swing.filechooser.FileFilter{public boolean accept(File file){String name = file.getName();name.toString(); // 該字串中的數字被轉換為字母name.toLowerCase();// 該字串中的字母被轉換為小寫字母/* 檔案尾碼是.txt且是個目錄 */if (name.endsWith(".txt") || file.isDirectory()){return true;} elsereturn false;}/* 將引用具體子類的子類對象的方法,不可以省略類中的getDescription(),原因是編譯器只允許調用在類中聲明的方法. */public String getDescription(){return ".txt";}}/* 將功能表項目 JMenu添加菜單 JMenuBar */public JMenu AddBar(String name, JMenuBar menu){JMenu jmenu = new JMenu(name);menu.add(jmenu);return jmenu;}/* 將功能表項目JMenuItem添加到菜單JMenu */public JMenuItem AddItem(String name, JMenu menu){JMenuItem jmenu = new JMenuItem(name);menu.add(jmenu);return jmenu;}aaa aaa1;{setTitle(jsb); // 設定視窗標題setBounds(250, 250, 500, 500);// 設定邊界JMenuBar menu = new JMenuBar(); // 添加菜單 JMenuBarthis.setJMenuBar(menu);// 調用this方法text.getDocument().addUndoableEditListener(manager);// 用於獲得程式當前有效文檔/* * Font是JAVA中的字型類,PLAIN是Font類中的靜態常量( static final ) ,表示是:普通樣式常量 BOLD * :粗體樣式常量 ,ITALIC: 斜體樣式常量,14:磅 */text.setFont(new Font("宋體", Font.PLAIN, 14));/* 游標顏色 */text.setCaretColor(Color.gray);/* 選中字型顏色 */text.setSelectedTextColor(Color.blue);/* 選中背景顏色 */text.setSelectionColor(Color.green);/* 是否換行 */text.setLineWrap(true);/* 是否單詞邊界換行(即有空白) */text.setWrapStyleWord(true);/* 文本區與邊框的間距,四個參數分別為上、左、下、右 */text.setMargin(new Insets(3, 5, 3, 5));/* 開啟或關閉自動拖動處理 */text.setDragEnabled(true);/* 建立一個 JScrollPane,它將視圖組件顯示在一個視口中,視圖位置可使用一對捲軸控制 */add(new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));File_bar = this.AddBar("檔案(F)", menu);Edit_bar = this.AddBar("編輯(E)", menu);Format_bar = this.AddBar("格式(O)", menu);View_bar = this.AddBar("查看(V)", menu);Help_bar = this.AddBar("協助(H)", menu);/* 檔案選項 *//* 建立選項 */File_bar_creat = this.AddItem("建立(N)   Ctrl+N", File_bar);File_bar_creat.addActionListener(new ActionListener(){// @Overridepublic void actionPerformed(ActionEvent arg0){// TODO 自動產生的方法存根text.setText("");}});/* 開啟選項 */File_bar_open = this.AddItem("開啟(O)   Ctrl+O", File_bar);File_bar_open.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){try{jfc.setCurrentDirectory(new File("."));// 設定目前的目錄// jfc.setFileFilter(new filter()); // 過濾檔案jfc.setFileFilter(new filter()); // 過濾檔案jfc.setAcceptAllFileFilterUsed(false); // 全選檔案// jfc.addChoosableFileFilter(new// javax.swing.filechooser.FileFilter()); //不能執行個體化類型jfc.showOpenDialog(null); // 彈出一個 "Open File" 檔案選取器對話方塊。file = jfc.getSelectedFile(); // 擷取已經選擇目錄jsb = file.getName(); // 擷取目錄名setTitle(jsb); // 顯示目錄名int length = (int) (jfc.getSelectedFile()).length();char[] ch = new char[length];FileReader fr = new FileReader(file);fr.read(ch);jsb = new String(ch);text.setText(jsb.trim()); // 獲得對象的欄位的值,然後轉成string類型,並且去掉前後空白~~ToString()是轉化為字串的方法// Trim()是去兩邊空格} catch (Exception e){JOptionPane.showMessageDialog(null, e);}}});/* 儲存選項 = (1)如果檔案為空白,建立一個目錄儲存;(2)如果當前檔案存在,直接儲存 */File_bar_save = this.AddItem("儲存(S)   Ctrl+O", File_bar);File_bar_save.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){if (file == null){try{jfc = new JFileChooser();jfc.setCurrentDirectory(null);jsb = JOptionPane.showInputDialog("請輸入檔案名稱:") + ".txt";/* * setSelectedFile返回的是對話方塊中選中的檔案但如果對話方塊類型是showSaveDialog的話, * 那麼這裡返回的值是你要儲存的檔案, 這個檔案可能存在,可能不存在,就是你在對話方塊中輸入的檔案名稱了, * 既然知道了檔案,如果不存在,就建立一個,然後向檔案寫入資料,這樣就可以實現儲存了 */jfc.setSelectedFile(new File(jsb));jfc.setFileFilter(new filter());int temp = jfc.showSaveDialog(null);if (temp == jfc.APPROVE_OPTION){if (file != null)file.delete();file = new File(jfc.getCurrentDirectory(), jsb);file.createNewFile();FileWriter fw = new FileWriter(file);fw.write(text.getText());fw.close();}} catch (Exception e){JOptionPane.showMessageDialog(null, e);}} else{try{FileWriter fw = new FileWriter(file);fw.write(text.getText());fw.close();} catch (Exception e){JOptionPane.showMessageDialog(null, e);}}}});/* 另存新檔選項 */File_bar_othersave = this.AddItem("另存新檔(A)...", File_bar);File_bar_othersave.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){// file fw = new file();jfc = new JFileChooser();jfc.setCurrentDirectory(new File("."));try{if (file == null){jsb = JOptionPane.showInputDialog("請輸入檔案名稱:") + ".txt";} elsejsb = file.getName();jfc.setSelectedFile(new File(jsb));jfc.setFileFilter(new filter());int temp = jfc.showSaveDialog(null);if (temp == jfc.APPROVE_OPTION){if (file != null)file.delete();file = new File(jfc.getCurrentDirectory(), jsb);file.createNewFile();FileWriter fw = new FileWriter(file);fw.write(text.getText());fw.close();}} catch (Exception e){JOptionPane.showMessageDialog(null, e);}}});/* 將預設大小的分隔字元添加到工具列的末尾。 */File_bar.addSeparator();/* 退出選項 + 退出提示 */File_bar_exit = this.AddItem("退出(X)", File_bar);File_bar_exit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){int state = JOptionPane.showConfirmDialog(aaa1, "您確定要退出?退出前請確定您的檔案已儲存");if (state == JOptionPane.OK_OPTION)System.exit(0);}});/* 編輯選項 *//* 撤消選項 */Edit_bar_Revoke = this.AddItem("撤銷(U)   Ctrl+Z", Edit_bar);Edit_bar_Revoke.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){if (manager.canUndo())manager.undo();}});/* 剪下選項 */Edit_bar_shear = this.AddItem("剪下(T)   Ctrl+X", Edit_bar);Edit_bar_shear.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){text.cut();}});/* 複製選項 */Edit_bar_copy = this.AddItem("複製(C)   Ctrl+C", Edit_bar);Edit_bar_copy.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){text.copy();}});/* 粘貼選項 */Edit_bar_paste = this.AddItem("粘貼(P)   Ctrl+V", Edit_bar);Edit_bar_paste.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){text.paste();}});/* 刪除選項=用空格替換從當前選取的開始到結束 */Edit_bar_delete = this.AddItem("刪除(L)   Del", Edit_bar);Edit_bar_delete.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){text.replaceRange("", text.getSelectionStart(), text.getSelectionEnd());}});/* 自動換行選項 */// m26 = this.AddItem("自動換行(W)", m3);JCheckBoxMenuItem Format_bar_hl = new JCheckBoxMenuItem("自動換行", true);Format_bar.add(Format_bar_hl);Format_bar_hl.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){/* 根據檔案名稱擷取檔案資訊 */if (Format_bar_hl.getState())text.setLineWrap(true);elsetext.setLineWrap(false);}});/* 字型選項 *//* * 字型格式設定選項 GraphicsEnvironment 類描述了 Java(tm) 應用程式在特定平台上可用 *  * 的 GraphicsDevice 對象和 Font 對象的集合 */Format_bar_ztxz = this.AddItem("字型選擇(F)", Format_bar);Format_bar_ztxz.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){/* 擷取本地圖形環境 */GraphicsEnvironment gr = GraphicsEnvironment.getLocalGraphicsEnvironment();/* 字型名稱列表框 */JList fontnames = new JList(gr.getAvailableFontFamilyNames());/* JScrollPane 管理視口、可選的垂直和水平捲軸以及可選的行和欄位標題視口 */int selection = JOptionPane.showConfirmDialog(null, new JScrollPane(fontnames), "請選擇字型",JOptionPane.OK_CANCEL_OPTION);// Object selectedFont = fontnames.getObject selectedFont = fontnames.getSelectedValue();// Object selectedFont = fontnames.getSelectedIndex();if (selection == JOptionPane.OK_OPTION && selectedFont != null){text.setFont(new Font(fontnames.getSelectedValue().toString(), Font.PLAIN, 20));}}});/* 字型顏色設定選項 */Format_bar_ztsz = this.AddItem("顏色(C)", Format_bar);Format_bar_ztsz.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){Color color = JColorChooser.showDialog(null, "文字顏色選擇", Color.BLACK);text.setForeground(color);}});View_bar_about = this.AddItem("關於記事本(About)", View_bar);View_bar_about.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){JOptionPane.showMessageDialog(null, "記事本\n開發語言:JAVA\n開發人員:【賀榮偉&&李苗】\n連絡方式:[email protected]", "關於",JOptionPane.PLAIN_MESSAGE);}});Help_bar_help = this.AddItem("協助選項(H)", Help_bar);Help_bar_help.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){JOptionPane.showMessageDialog(null, "詳細代碼請移步\n部落格:www.acmerbar.com", "協助", JOptionPane.PLAIN_MESSAGE);}});setResizable(true); // 表單是否可變setVisible(true); // 表單是否可見setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String args[]){aaa example = new aaa();}}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.