Java課程設計- 記事本程式碼
Java課程設計
【題目】:使用已經學習的java知識編寫一個簡單的記事本
【實現功能】:
【1】檔案: 新建 開啟 儲存 另存為 退出
【2】編輯: 剪下 複製 貼上 刪除 撤銷
【3】格式: 自動換行 字型格式 字型顏色 查詢||替換
【4】檢視: 關於記事本
【5】幫助 :幫助選項
【6】顯示當前時間
【使用的類】:
import.java.io; import java.swt; import javax.swing; import javax.swing.undo.UndoManager
程式碼:
package qqq; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; 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; import java.util.Calendar; import java.util.GregorianCalendar; import javax.swing.JButton; import javax.swing.JCheckBoxMenuItem; import javax.swing.JColorChooser; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JToolBar; import javax.swing.undo.UndoManager; // 對選單活動事件撤銷的實現 public class NotePad extends JFrame /// extends繼承JFrame類 { int start = 0, end = 0; private UndoManager manager = new UndoManager();// 添加布局管理器 private JTextArea text = new JTextArea(); private JFileChooser jfc = new JFileChooser(); private String jsb = "新建記事本"; private File file; private JMenuBar menu; // ---------------選單欄 private JMenu File_bar, Edit_bar, Format_bar, View_bar, Help_bar; // ---------------檔案選單 private JMenuItem File_bar_creat, File_bar_open, File_bar_save, File_bar_othersave, File_bar_exit; // ---------------編輯選單 private JMenuItem Edit_bar_Revoke, Edit_bar_shear, Edit_bar_copy, Edit_bar_paste, Edit_bar_delete; // ---------------格式選單 private JMenuItem Format_bar_hl, m30, Format_bar_ztxz, Format_bar_ztsz, View_bar_about, Help_bar_help; // ---------------查詢替換選單 private JMenuItem Format_bar_find_replace; private JLabel statusLabel1; private JToolBar statusBar; GregorianCalendar time = new GregorianCalendar(); int hour = time.get(Calendar.HOUR_OF_DAY); int min = time.get(Calendar.MINUTE); int second = time.get(Calendar.SECOND); /* 檔案格式過濾器 */ public class filter extends javax.swing.filechooser.FileFilter { public boolean accept(File file) { String name = file.getName(); name.toString(); // 該字串中的數字被轉換為字元 /* 檔案字尾是.txt且是個目錄 */ if (name.endsWith(".txt") || file.isDirectory()) { return true; } else return 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; } class Clock extends Thread { // 模擬時鐘 public void run() { while (true) { GregorianCalendar time = new GregorianCalendar(); int hour = time.get(Calendar.HOUR_OF_DAY); int min = time.get(Calendar.MINUTE); int second = time.get(Calendar.SECOND); statusLabel1.setText(" 當前時間:" + hour + ":" + min + ":" + second); try { Thread.sleep(950); } catch (InterruptedException exception) { } } } } NotePad note; // public void NotePad() { Container container = getContentPane(); setTitle(jsb); // 設定視窗標題 setBounds(250, 250, 500, 500);// 設定邊界 JMenuBar menu = new JMenuBar(); // 新增選單 JMenuBar this.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)); /* 建立一個 JScrollPane,它將檢視元件顯示在一個視口中,檢視位置可使用一對滾動條控制 */ add(new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); File_bar = this.AddBar("檔案(F)", menu); // File_bar.setMnemonic('F'); 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() { // @Override public void actionPerformed(ActionEvent arg0) { // 設定新檔案內容 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()); // 過濾檔案 /* * 確定是否將AcceptAll FileFilter用作可選擇過濾器列表中一個可用選項。如果為假, * 則AcceptAll檔案過濾器從可用的檔案過濾列表中刪除。 * 如果為true,則AcceptAll檔案過濾器將成為可用的檔案過濾器。 */ jfc.setAcceptAllFileFilterUsed(false); // 全選檔案 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"; } else jsb = 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(note, "您確定要退出?退出前請確定您的檔案已儲存"); 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); else text.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.getSelectedValue(); 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); } }); Format_bar_find_replace = this.AddItem("替換(R)||查詢(F)", Format_bar); Format_bar_find_replace.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JDialog search = new JDialog(); search.setSize(300, 100); search.setLocation(450, 350); JLabel label_1 = new JLabel("查詢的內容"); JLabel label_2 = new JLabel("替換的內容"); JTextField textField_1 = new JTextField(5); JTextField textField_2 = new JTextField(5); JButton buttonFind = new JButton("查詢下一個"); JButton buttonChange = new JButton("替換"); JPanel panel = new JPanel(new GridLayout(2, 3)); panel.add(label_1); panel.add(textField_1); panel.add(buttonFind); panel.add(label_2); panel.add(textField_2); panel.add(buttonChange); search.add(panel); search.setVisible(true); // 為查詢下一個 按鈕繫結監聽事件 buttonFind.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String findText = textField_1.getText();// 查詢的字元 String textArea = text.getText();// 當前文字框的內容 start = textArea.indexOf(findText, end); end = start + findText.length(); if (start == -1) // 沒有找到 { JOptionPane.showMessageDialog(null, "沒找到" + findText, "記事本", JOptionPane.WARNING_MESSAGE); text.select(start, end); } else { text.select(start, end); } } }); // 為替換按鈕繫結監聽時間 buttonChange.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String changeText = textField_2.getText();// 替換的字串 /* 如果選定檔案為真 */ if (text.getSelectionStart() != text.getSelectionEnd()) text.replaceRange(changeText, text.getSelectionStart(), text.getSelectionEnd()); } }); } }); 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開發者:【herongwei&&limiao】\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); } }); JPanel toolBar = new JPanel(); toolBar.setLayout(new FlowLayout(FlowLayout.LEFT)); // --------------------------------------向容器新增工具欄 container.add(toolBar, BorderLayout.NORTH); // -----------------------------------建立和新增狀態列 // toolBar.add(File_bar); // toolBar.add(Edit_bar); // toolBar.add(Format_bar); // toolBar.add(Help_bar); // toolBar.add(View_bar); statusBar = new JToolBar(); statusBar.setLayout(new FlowLayout(FlowLayout.LEFT)); statusLabel1 = new JLabel(" 當前時間:" + hour + ":" + min + ":" + second); statusBar.add(statusLabel1); statusBar.addSeparator(); container.add(statusBar, BorderLayout.SOUTH); statusBar.setVisible(true); Clock clock = new Clock(); clock.start(); this.setResizable(true); // 窗體是否可變 this.setVisible(true); // 窗體是否可見 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String args[]) { NotePad example = new NotePad(); } /* * public class jjishiben{ public static void main(Strin args[]) { public * void Run() { NotePad note = new NotePad(); note.setTitle("記事本"); * note.setVisible(true); note.setBounds(250, 250, 500, 500); * note.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } } */ }
2015/12/15 新增獲取本機時間功能,顯示在一個StausBar裡面。
2015/12/15 新增查詢和替換功能,當用戶沒有選擇任何字元時,不會預設在最後面新增字元。
相關推薦
Java課程設計- 記事本程式碼
Java課程設計 【題目】:使用已經學習的java知識編寫一個簡單的記事本 【實現功能】: 【1】檔案: 新建 開啟 儲存 另存為 退出 【2】編輯: 剪下 複製 貼上 刪除 撤銷 【3】格式: 自動換行 字型格式 字型顏色 查詢||替換 【4】檢視: 關於記事本
java課程設計團隊(搜索引擎)
plc 簡介 mpi hasclass findall image doc style agen JAVA課程設計 基於學院網站的搜索引擎 對學院網站進行抓取、建索(需要中文分詞)、排序(可選)、搜索、摘要顯示。可以是GUI界面,也可以是Web界面。 一、團隊介紹 學
201621123063《JAVA課程設計報告》
元素 markdown 程序 pan 針對 任務 通過 remove 情況 一、需求分析 完成一個單機版的購物車 (1)先建立一個文本文件,定義出自己想要的商品。以商品編號;商品名稱;商品品牌;價格作為文件的內容,中間要求用分號或者分割 (2)編寫程序,定義一個商品類,文件
Java課程設計
產品 復制粘貼 window als 得到 -m bubuko 題目 研究 Java課程設計 一、題目介紹 計算器模擬程序(2人)得分系數:D 功能要求:仿照Windows計算器,編寫一具有GUI的計算器,能實現整數的加、減、乘、除四則運算,並且需要支持鍵盤快捷鍵,基本可取
java課程設計(學生信息管理系統)
gen 圖片 false 出生日期 resizable cnblogs orm size gin 1.需求分析 功能要求: 1)需要管理的學生信息有:學號、姓名、性別、出生日期、政治面貌、家庭住址、電話、宿舍號。 2)實現查詢、增、刪、改等功能。 數據存儲:數據庫或文件。
Java課程設計---安裝Mysql及管理工具
需要 編碼 tps ref target 管理工具 技術分享 span blank 1、安裝mysql 沒有安裝包的可以在這個地址下載:https://dev.mysql.com/downloads/mysql/5.5.html 雙擊提供的安裝包
Java課程設計---Eclipse基本環境配置
bubuko splay height margin width src nbsp eclips 技術 1、設置eclipse編碼 2、設置提示 Java課程設計---Eclipse基本環境配置
Java課程設計---新建項目
添加 mar 技術分享 新建 技術 src img AR wid 1、新建 2、添加lib包 3、新建各種包 為了將項目劃分清楚,下面將新建如下幾個包 Java課程設計---新建項目
Java課程設計---實現登錄(2)
mage dax stat 2018年 const SQ getc RF ktr 前面已經完成了登錄界面的設計,單擊確認按鈕可以獲取輸入的用戶名和密碼,下面來將演示實現真實的登錄(輸入值和數據庫的比對) 1、設計“登錄服務” 通常我們根據業務關系,對相應的操作人員提
數據庫+Java課程設計 人事管理系統 (一)
clear 插入 ID spc AR util color job AI 一、JAVA與數據庫的合作 此次開發語言為Java,所用的數據庫驅動
代寫CSSE2002/7023作業、代做Java程序作業、代寫Java編程作業、幫寫Java實驗作業、Java課程設計作業代做
sheet log contain star Once pro click ble explain The University of Queenland, School of ITEECSSE2002/7023 — 2018Assignment 3 (15%)Due: 1
代寫Inf2A作業、代做Java程式語言作業、代做Micro-Haskell留學生作業、代寫Java課程設計作業
代寫Inf2A作業、代做Java程式語言作業、代做Micro-Haskell留學生作業、代寫Java課程設計作業Inf2A 2018–19: Assignment 1The Language Processing Pipeline for Micro-HaskellIssued 12 October 2018
Comp 2013作業代寫、代做Java程式設計作業、代做Software Maintenance作業0、代寫Java課程設計作業
Comp 2013作業代寫、代做Java程式設計作業、代做Software Maintenance作業0、代寫Java課程設計作業Comp 2013 Software Maintenance Coursework 1 for 2018Maintainable Code Reflection Portfolio
COMP201作業代寫、代做Java or Python程式設計作業、代做Java課程設計作業
COMP201作業代寫、代做Java or Python程式設計作業、代寫Software Engineering作業、代做Java課程設計作業COMP201: Software Engineering IAssignment 1.2 (2018/2019)(100% mark for Assignment
COMP201作業代寫、代做Java or Python編程作業、代做Java課程設計作業
mount pow any ica electron int iss sting within COMP201作業代寫、代做Java or Python編程作業、代寫Software Engineering作業、代做Java課程設計作業COMP201: Software E
萬年曆-Java課程設計題(eclipse編輯器)
專案目錄結構: `CalendarMainClass.java` package Calendar; /** * CalendarMainClass主類 * @author Administrator * @version 版本1.0 */ public
【JAVA 課程設計 之 萬年曆】
距離2017年還有30多個小時~轉眼間2016只剩一個尾巴了,大學生活也過了快一半了,自己卻依舊那麼笨手笨腳,不會的知識永遠那麼多,該看的書永遠沒機會去看,2017願一切如昨天抽的籤: 遠方不一定有詩,但有更好的自己~明天你好,請多關照~ 2017希望我的家人
JAVA課程設計——華容道小遊戲
華容道遊戲簡介: 華容道,古老的中國遊戲,以其變化多端、百玩不厭的特點與魔方、獨立鑽石棋一起被國外智力專家並稱為“智力遊戲界的三個不可思議”。它與七巧板、九連環等中國傳統益智玩具還有個代名詞叫作“中國的難題”。華容道遊戲取自著名的三國故事,曹操在赤壁大戰中被劉備和孫權的“苦
Java課程設計:捕獲圖片以及識別圖中的文字
lean 方式 最簡 文件名 println otf jpeg robot nap 本人承接各種高校C語言、C++、Java語言等課程設計以及ppt等制造,有需要的私信我或者微信18476275715 屏幕捕捉工具 要求: 用圖形用戶界面實現。 能實現捕捉屏幕上的圖片、文字