Example #1
0
 @Override
 public void actionPerformed(ActionEvent e) {
   JFileChooser f = new JFileChooser();
   f.setFileFilter(new MyFileFilter()); // 設定檔案選擇器
   int choose = f.showOpenDialog(getContentPane()); // 顯示檔案選取
   if (choose == JFileChooser.OPEN_DIALOG) { // 有開啟檔案的話,開始讀檔
     BufferedReader br = null;
     try {
       File file = f.getSelectedFile();
       br = new BufferedReader(new FileReader(file));
       TextDocument ta = new TextDocument(file.getName(), file);
       ta.addKeyListener(new SystemTrackSave());
       ta.read(br, null);
       td.add(ta);
       td.setTitleAt(docCount++, file.getName());
     } catch (Exception exc) {
       exc.printStackTrace();
     } finally {
       try {
         br.close();
       } catch (Exception ecx) {
         ecx.printStackTrace();
       }
     }
   }
 }
Example #2
0
 @Override
 public void actionPerformed(ActionEvent a) {
   String str = "NewDoc_" + (NewDocCount++) + ".txt";
   TextDocument text = new TextDocument(str);
   text.addKeyListener(new SystemTrackSave()); // 登記文字編譯區動作監聽,用於提示是否存檔
   td.add(text); // 將新黨案放入td,並設定其標題。
   td.setTitleAt(docCount++, str);
 }