返回列表 發帖

文字編輯器 (八)

本帖最後由 tonyh 於 2020-3-7 15:15 編輯





使用 JOptionPane 類別下的 showMessageDialog() 方法完成 "關於 MyEditor" 的對應動作。
  1. import java.awt.Font;
  2. import javax.swing.JFileChooser;
  3. import javax.swing.JFrame;
  4. import javax.swing.JMenu;
  5. import javax.swing.JMenuBar;
  6. import javax.swing.JMenuItem;
  7. import javax.swing.JOptionPane;
  8. //import javax.swing.JDialog;
  9. import javax.swing.JScrollPane;
  10. import javax.swing.JTextArea;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.UIManager;
  13. import javax.swing.border.BevelBorder;
  14. import javax.swing.filechooser.FileNameExtensionFilter;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.ActionEvent;
  17. import java.io.BufferedReader;
  18. import java.io.BufferedWriter;
  19. import java.io.File;
  20. import java.io.FileReader;
  21. import java.io.FileWriter;

  22. public class Ch08 implements ActionListener{
  23.        
  24.         String title="My Editor";
  25.         JFrame fm;
  26.         JScrollPane sp;
  27.         JTextArea ta;
  28.         ImageIcon ic,ic_cut,ic_exit,ic_new,ic_open,ic_paste,ic_save,ic_copy,ic_selectall,ic_about,ic_developer;
  29.         JMenuBar mb;
  30.         JMenu mn_file,mn_edit,mn_help;
  31.         JMenuItem mi_new,mi_open,mi_save,mi_exit,mi_copy,mi_paste,mi_cut,mi_selectall,mi_about;
  32.         JFileChooser fc;
  33.         FileNameExtensionFilter filter1;
  34.        
  35.         void initialize()
  36.         {
  37.                 try {
  38.                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  39.                 } catch (Exception e){}

  40.                 filter1=new FileNameExtensionFilter("純文字文件","txt");
  41.                
  42.                 fc=new JFileChooser();
  43.                 //fc.addChoosableFileFilter(filter1);
  44.                 fc.setFileFilter(filter1);
  45.                
  46.                 ic=new ImageIcon(Ch08.class.getResource("pic/editor.png"));
  47.                 ic_cut=new ImageIcon(Ch08.class.getResource("pic/cut.png"));
  48.                 ic_exit=new ImageIcon(Ch08.class.getResource("pic/exit.png"));
  49.                 ic_new=new ImageIcon(Ch08.class.getResource("pic/new.png"));
  50.                 ic_open=new ImageIcon(Ch08.class.getResource("pic/open.png"));
  51.                 ic_paste=new ImageIcon(Ch08.class.getResource("pic/paste.png"));
  52.                 ic_save=new ImageIcon(Ch08.class.getResource("pic/save.png"));
  53.                 ic_copy=new ImageIcon(Ch08.class.getResource("pic/copy.png"));
  54.                 ic_selectall=new ImageIcon(Ch08.class.getResource("pic/selectall.png"));
  55.                 ic_about=new ImageIcon(Ch08.class.getResource("pic/about.png"));
  56.                 ic_developer=new ImageIcon(Ch08.class.getResource("pic/developer.png"));
  57.                
  58.                 mi_new=new JMenuItem("開新檔案",ic_new);
  59.                 mi_new.addActionListener(this);
  60.                 mi_open=new JMenuItem("開啟舊檔",ic_open);
  61.                 mi_open.addActionListener(this);
  62.                 mi_save=new JMenuItem("儲存檔案",ic_save);
  63.                 mi_save.addActionListener(this);
  64.                 mi_exit=new JMenuItem("結束",ic_exit);
  65.                 mi_exit.addActionListener(this);
  66.                 mi_copy=new JMenuItem("複製",ic_copy);
  67.                 mi_copy.addActionListener(this);
  68.                 mi_paste=new JMenuItem("貼上",ic_paste);
  69.                 mi_paste.addActionListener(this);
  70.                 mi_cut=new JMenuItem("剪下",ic_cut);
  71.                 mi_cut.addActionListener(this);
  72.                 mi_selectall=new JMenuItem("全選",ic_selectall);
  73.                 mi_selectall.addActionListener(this);
  74.                 mi_about=new JMenuItem("關於 My Editor",ic_about);
  75.                 mi_about.addActionListener(this);
  76.                
  77.                 mn_file=new JMenu(" 檔案(F) ");
  78.                 mn_file.setMnemonic('F');     //設定快速鍵
  79.                 mn_file.add(mi_new);
  80.                 mn_file.add(mi_open);
  81.                 mn_file.add(mi_save);
  82.                 mn_file.addSeparator();    //分隔線
  83.                 mn_file.add(mi_exit);
  84.                
  85.                 mn_edit=new JMenu(" 編輯(E) ");
  86.                 mn_edit.setMnemonic('E');     //設定快速鍵
  87.                 mn_edit.add(mi_copy);
  88.                 mn_edit.add(mi_paste);
  89.                 mn_edit.add(mi_cut);
  90.                 mn_edit.addSeparator();
  91.                 mn_edit.add(mi_selectall);
  92.                
  93.                 mn_help=new JMenu(" 說明(H) ");
  94.                 mn_help.setMnemonic('H');     //設定快速鍵
  95.                 mn_help.add(mi_about);

  96.                
  97.                 mb=new JMenuBar();
  98.                 mb.setBorder(new BevelBorder(BevelBorder.RAISED));   //設定具陰影效果的邊框
  99.                 mb.add(mn_file);
  100.                 mb.add(mn_edit);
  101.                 mb.add(mn_help);
  102.                
  103.                 ta=new JTextArea();
  104.                 ta.setFont(new Font("新細明體", Font.PLAIN, 18));
  105.                 ta.setLineWrap(true);     //自動換行
  106.                
  107.                 sp=new JScrollPane(ta);
  108.                
  109.                 fm=new JFrame(title+" - 未命名");
  110.                 fm.setBounds(100, 100, 500, 350);
  111.                 fm.setIconImage(ic.getImage());
  112.                 fm.setVisible(true);
  113.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  114.                 fm.add(sp);
  115.                 fm.setJMenuBar(mb);
  116.         }
  117.        
  118.         Ch08()
  119.         {
  120.                 initialize();
  121.         }
  122.        
  123.         public void actionPerformed(ActionEvent e)
  124.         {
  125.                 if(e.getSource()==mi_new)
  126.                 {
  127.                     ta.setText("");
  128.                     fm.setTitle(title+" - 未命名");
  129.                 }
  130.                 else if(e.getSource()==mi_open)
  131.                 {
  132.                         int ret=fc.showOpenDialog(null);
  133.                         if(ret==JFileChooser.APPROVE_OPTION)
  134.                         {
  135.                                 try
  136.                                 {
  137.                                         String str;
  138.                                         File fi=fc.getSelectedFile();
  139.                                         BufferedReader br=new BufferedReader(new FileReader(fi.getAbsolutePath()));
  140.                                         ta.setText(br.readLine());
  141.                                         do
  142.                                         {
  143.                                                 str=br.readLine();
  144.                                                 if(str==null)
  145.                                                         break;
  146.                                                 ta.append("\n"+str);
  147.                                         }while(true);
  148.                                         br.close();
  149.                                         fm.setTitle(title+" - "+fc.getName(fi));
  150.                                 }catch(Exception ex){}
  151.                                
  152.                         }       
  153.                 }
  154.                 else if(e.getSource()==mi_save)
  155.                 {
  156.                         int ret=fc.showSaveDialog(null);
  157.                         if(ret==JFileChooser.APPROVE_OPTION)
  158.                         {
  159.                                 try
  160.                                 {
  161.                                         File fi=fc.getSelectedFile();
  162.                                         BufferedWriter bw;
  163.                                         String ext=fi.getAbsolutePath().substring(fi.getAbsolutePath().length()-4);   //file extension 副檔名
  164.                                         System.out.println(ext);
  165.                                         String fiPath="";
  166.                                         if(fc.getFileFilter()==filter1)
  167.                                         {
  168.                                                 if(ext.equals(".txt"))
  169.                                                         fiPath=fi.getAbsolutePath();
  170.                                             else
  171.                                                     fiPath=fi.getAbsolutePath()+".txt";       
  172.                                         }
  173.                                         else
  174.                                                 fiPath=fi.getAbsolutePath();
  175.                                         bw=new BufferedWriter(new FileWriter(fiPath));
  176.                                        
  177.                                         bw.write(ta.getText().replaceAll("\n", "\r\n"));
  178.                                         //windows下的文字檔分行符號:\r\n  linux/unix下的文字檔分行符號:\r  Mac下的文字檔分行符號:\n
  179.                                         bw.flush();
  180.                                         bw.close();
  181.                                         fm.setTitle(title+" - "+fc.getName(fi));
  182.                                 }catch(Exception ex){}
  183.                         }       
  184.                 }
  185.                 else if(e.getSource()==mi_exit)
  186.                         System.exit(0);
  187.                 else if(e.getSource()==mi_copy)
  188.                     ta.copy();
  189.                 else if(e.getSource()==mi_paste)
  190.                         ta.paste();
  191.                 else if(e.getSource()==mi_cut)
  192.             ta.cut();
  193.                 else if(e.getSource()==mi_selectall)
  194.                         ta.selectAll();
  195.                 else if(e.getSource()==mi_about)
  196.                 {
  197.                         String msg="本軟體由社團法人高雄市資訊培育協會青少年程式設計班學員\n林宇翔所開發,感謝您的使用!";
  198.                         JOptionPane.showMessageDialog(fm,msg,"關於 My Editor",JOptionPane.INFORMATION_MESSAGE,ic_developer);
  199.                 }
  200.         }
  201.        
  202.         public static void main(String[] args) {
  203.                 new Ch08();
  204.         }
  205. }
複製代碼


此帖僅作者可見
EEEEEEEEEEEEELLLLLLLLLLLLLIIIIIIIIIIIII

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

返回列表