java小程式之簡單記事本
阿新 • • 發佈:2019-01-02
import java.awt.FileDialog; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Notepad extends JFrame { // 定義元件 MenuBar menubar; // 選單欄 Menu menu; // 選單 JTextArea edit_Area; MenuItem open_menuItem, save_menuItem, exit_menuItem; // 子選單 FileDialog open_fileDialog, save_fileDialog; // 彈出的儲存開啟框 JScrollPane jspane; // 讓文字域可以滾動的元件 File file; public Notepad() { init(); event(); } // 設定監聽 private void event() { open_menuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { openfile(); } }); save_menuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { savefile(); } }); exit_menuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); } // 開啟檔案方法 private void openfile() { open_fileDialog.setVisible(true); // 設定其顯示出來 // 獲取路徑和檔名 String dirPath = open_fileDialog.getDirectory(); String fileName = open_fileDialog.getFile(); // 防止點選取消報錯 if (dirPath == null || fileName == null) return; edit_Area.setText(""); // 將文字區域清空 file = new File(dirPath, fileName); // 建立檔案物件 // 按照行來讀取資料,顯示在文字區域 try { BufferedReader br = new BufferedReader(new FileReader(file)); String line = null; while ((line = br.readLine()) != null) { edit_Area.append(line + "\r\n"); } br.close(); } catch (IOException ex) { throw new RuntimeException("讀取失敗"); } } // 儲存檔案方法 private void savefile() { // 先判斷檔案是否存在 if (file == null) { save_fileDialog.setVisible(true); String dirPath = save_fileDialog.getDirectory(); String fileName = save_fileDialog.getFile(); // 防止點選取消報錯 if (dirPath == null || fileName == null) return; // 因為檔案不存在。所以需要建立file物件 file = new File(dirPath, fileName); } // 將資料寫入檔案 try { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); String info = edit_Area.getText(); // 得到文字區域的資訊 bw.write(info); // 寫入操作 bw.flush(); bw.close(); } catch (IOException e1) { throw new RuntimeException(); } } // 初始化 private void init() { this.setTitle("記事本"); menubar = new MenuBar(); menu = new Menu("選單"); open_menuItem = new MenuItem("開啟"); save_menuItem = new MenuItem("儲存"); exit_menuItem = new MenuItem("退出"); edit_Area = new JTextArea(); jspane = new JScrollPane(edit_Area); // 分別設定水平和垂直滾動條自動出現 jspane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); jspane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); // //分別設定水平和垂直滾動條總是出現 // jspane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); // jspane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // // //分別設定水平和垂直滾動條自動出現總是隱藏 // jspane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // jspane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); // 新增對應元件 this.add(jspane); menu.add(open_menuItem); menu.add(save_menuItem); menu.add(exit_menuItem); menubar.add(menu); // 設定窗體選單欄 如果要新增其他選單直接menbar.add()即可 this.setMenuBar(menubar); // 設定檔案可開啟跟儲存視窗 open_fileDialog = new FileDialog(this, "開啟", FileDialog.LOAD); save_fileDialog = new FileDialog(this, "儲存", FileDialog.SAVE); this.setBounds(200, 300, 500, 400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); // 設定顯示在中央 this.setVisible(true); } public static void main(String[] args) { new Notepad(); } }