返回列表 發帖

文字編輯器 (九)

本帖最後由 tonyh 於 2017-2-11 17:09 編輯

使用 JRadioButtonMenuItem 類別產生帶有 radio button 的 menu item,再將同一區塊的選項加入由 ButtonGroup 類別所產生的群組物件中,使於同一區塊只有一個選項可被選取。





  1. import java.awt.Font;
  2. import javax.swing.ButtonGroup;
  3. import javax.swing.JFileChooser;
  4. import javax.swing.JFrame;
  5. import javax.swing.JMenu;
  6. import javax.swing.JMenuBar;
  7. import javax.swing.JMenuItem;
  8. import javax.swing.JRadioButtonMenuItem;
  9. import javax.swing.JOptionPane;
  10. //import javax.swing.JDialog;
  11. import javax.swing.JScrollPane;
  12. import javax.swing.JTextArea;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.UIManager;
  15. import javax.swing.border.BevelBorder;
  16. import javax.swing.filechooser.FileNameExtensionFilter;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.ActionEvent;
  19. import java.io.BufferedReader;
  20. import java.io.BufferedWriter;
  21. import java.io.File;
  22. import java.io.FileReader;
  23. import java.io.FileWriter;

  24. public class Ch09 implements ActionListener{
  25.        
  26.         String title="My Editor";
  27.         JFrame fm,fm2;
  28.         JScrollPane sp;
  29.         JTextArea ta;
  30.         ImageIcon ic,ic_cut,ic_exit,ic_new,ic_open,ic_paste,ic_save,ic_copy,ic_selectall,ic_about,ic_developer,ic_size,ic_style,ic_type;
  31.         JMenuBar mb;
  32.         JMenu mn_file,mn_edit,mn_option,mn_help,mn_size,mn_type,mn_style;
  33.         JMenuItem mi_new,mi_open,mi_save,mi_exit,mi_copy,mi_paste,mi_cut,mi_selectall,mi_about;
  34.         JRadioButtonMenuItem rbmi_1[]=new JRadioButtonMenuItem[3];
  35.         JRadioButtonMenuItem rbmi_2[]=new JRadioButtonMenuItem[4];
  36.         JRadioButtonMenuItem rbmi_3[]=new JRadioButtonMenuItem[2];
  37.         ButtonGroup bg_1,bg_2,bg_3;
  38.         JFileChooser fc;
  39.         FileNameExtensionFilter filter1;
  40.        
  41.         void initialize()
  42.         {
  43.                 try {
  44.                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  45.                 } catch (Exception e){}

  46.                 filter1 = new FileNameExtensionFilter("文字文件(*.txt)", "txt");
  47.                
  48.                 fc=new JFileChooser();
  49.                 fc.setFileFilter(filter1);
  50.                
  51.                 ic=new ImageIcon(Ch09.class.getResource("pic/editor.png"));
  52.                 ic_cut=new ImageIcon(Ch09.class.getResource("pic/cut.png"));
  53.                 ic_exit=new ImageIcon(Ch09.class.getResource("pic/exit.png"));
  54.                 ic_new=new ImageIcon(Ch09.class.getResource("pic/new.png"));
  55.                 ic_open=new ImageIcon(Ch09.class.getResource("pic/open.png"));
  56.                 ic_paste=new ImageIcon(Ch09.class.getResource("pic/paste.png"));
  57.                 ic_save=new ImageIcon(Ch09.class.getResource("pic/save.png"));
  58.                 ic_copy=new ImageIcon(Ch09.class.getResource("pic/copy.png"));
  59.                 ic_selectall=new ImageIcon(Ch09.class.getResource("pic/selectall.png"));
  60.                 ic_about=new ImageIcon(Ch09.class.getResource("pic/about.png"));
  61.                 ic_developer=new ImageIcon(Ch09.class.getResource("pic/developer.png"));
  62.                 ic_size=new ImageIcon(Ch09.class.getResource("pic/size.png"));
  63.                 ic_type=new ImageIcon(Ch09.class.getResource("pic/type.png"));
  64.                 ic_style=new ImageIcon(Ch09.class.getResource("pic/style.png"));
  65.                
  66.                 mi_new=new JMenuItem("開新檔案",ic_new);
  67.                 mi_new.addActionListener(this);
  68.                 mi_open=new JMenuItem("開啟舊檔",ic_open);
  69.                 mi_open.addActionListener(this);
  70.                 mi_save=new JMenuItem("儲存檔案",ic_save);
  71.                 mi_save.addActionListener(this);
  72.                 mi_exit=new JMenuItem("結束",ic_exit);
  73.                 mi_exit.addActionListener(this);
  74.                 mi_copy=new JMenuItem("複製",ic_copy);
  75.                 mi_copy.addActionListener(this);
  76.                 mi_paste=new JMenuItem("貼上",ic_paste);
  77.                 mi_paste.addActionListener(this);
  78.                 mi_cut=new JMenuItem("剪下",ic_cut);
  79.                 mi_cut.addActionListener(this);
  80.                 mi_selectall=new JMenuItem("全選",ic_selectall);
  81.                 mi_selectall.addActionListener(this);
  82.                 mi_about=new JMenuItem("關於 My Editor",ic_about);
  83.                 mi_about.addActionListener(this);
  84.                
  85.                 rbmi_1[0]=new JRadioButtonMenuItem("大 (22)");
  86.                 rbmi_1[1]=new JRadioButtonMenuItem("中 (18)",true);
  87.                 rbmi_1[2]=new JRadioButtonMenuItem("小 (14)");
  88.                 bg_1=new ButtonGroup();
  89.                 for(int i=0; i<rbmi_1.length; i++)
  90.                         bg_1.add(rbmi_1[i]);
  91.                
  92.                 rbmi_2[0]=new JRadioButtonMenuItem("一般",true);
  93.                 rbmi_2[1]=new JRadioButtonMenuItem("粗體");
  94.                 rbmi_2[2]=new JRadioButtonMenuItem("斜體");
  95.                 rbmi_2[3]=new JRadioButtonMenuItem("粗體+斜體");
  96.                 bg_2=new ButtonGroup();
  97.                 for(int i=0; i<rbmi_2.length; i++)
  98.                         bg_2.add(rbmi_2[i]);
  99.                        
  100.                 rbmi_3[0]=new JRadioButtonMenuItem("白底黑字",true);
  101.                 rbmi_3[1]=new JRadioButtonMenuItem("黑底白字");
  102.                 bg_3=new ButtonGroup();
  103.                 for(int i=0; i<rbmi_3.length; i++)
  104.                         bg_3.add(rbmi_3[i]);

  105.                 mn_file=new JMenu(" 檔案(F) ");
  106.                 mn_file.setMnemonic('F');     //設定快速鍵
  107.                 mn_file.add(mi_new);
  108.                 mn_file.add(mi_open);
  109.                 mn_file.add(mi_save);
  110.                 mn_file.addSeparator();    //分隔線
  111.                 mn_file.add(mi_exit);
  112.                
  113.                 mn_edit=new JMenu(" 編輯(E) ");
  114.                 mn_edit.setMnemonic('E');     //設定快速鍵
  115.                 mn_edit.add(mi_copy);
  116.                 mn_edit.add(mi_paste);
  117.                 mn_edit.add(mi_cut);
  118.                 mn_edit.addSeparator();
  119.                 mn_edit.add(mi_selectall);
  120.                
  121.                 mn_size=new JMenu("文字大小");
  122.                 mn_size.setIcon(ic_size);
  123.                 for(int i=0; i<rbmi_1.length; i++)
  124.                         mn_size.add(rbmi_1[i]);
  125.                 mn_type=new JMenu("文字樣式");
  126.                 mn_type.setIcon(ic_type);
  127.                 for(int i=0; i<rbmi_2.length; i++)
  128.                         mn_type.add(rbmi_2[i]);
  129.                 mn_style=new JMenu("風格");
  130.                 mn_style.setIcon(ic_style);
  131.                 for(int i=0; i<rbmi_3.length; i++)
  132.                         mn_style.add(rbmi_3[i]);
  133.                
  134.                 mn_option=new JMenu(" 設定(O) ");
  135.                 mn_option.setMnemonic('O');
  136.                 mn_option.add(mn_size);
  137.                 mn_option.add(mn_type);
  138.                 mn_option.addSeparator();
  139.                 mn_option.add(mn_style);
  140.                
  141.                 mn_help=new JMenu(" 說明(H) ");
  142.                 mn_help.setMnemonic('H');     //設定快速鍵
  143.                 mn_help.add(mi_about);
  144.         
  145.                 mb=new JMenuBar();
  146.                 mb.setBorder(new BevelBorder(BevelBorder.RAISED));   //設定具陰影效果的邊框
  147.                 mb.add(mn_file);
  148.                 mb.add(mn_edit);
  149.                 mb.add(mn_option);
  150.                 mb.add(mn_help);
  151.                
  152.                 ta=new JTextArea();
  153.                 ta.setFont(new Font("新細明體", Font.PLAIN, 18));
  154.                 ta.setLineWrap(true);     //自動換行
  155.                
  156.                 sp=new JScrollPane(ta);
  157.                
  158.                 fm=new JFrame(title+" - 未命名");
  159.                 fm.setBounds(100, 100, 500, 350);
  160.                 fm.setIconImage(ic.getImage());
  161.                 fm.setVisible(true);
  162.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  163.                 fm.add(sp);
  164.                 fm.setJMenuBar(mb);
  165.         }
  166.        
  167.         Ch09()
  168.         {
  169.                 initialize();
  170.         }
  171.        
  172.         public void actionPerformed(ActionEvent e)
  173.         {
  174.                 if(e.getSource()==mi_new)
  175.                 {
  176.                     ta.setText("");
  177.                     fm.setTitle(title+" - 未命名");
  178.                 }
  179.                 else if(e.getSource()==mi_open)
  180.                 {
  181.                         int ret=fc.showOpenDialog(null);
  182.                         if(ret==JFileChooser.APPROVE_OPTION)
  183.                         {
  184.                                 try
  185.                                 {
  186.                                         String str;
  187.                                         File fi=fc.getSelectedFile();
  188.                                         BufferedReader br=new BufferedReader(new FileReader(fi.getAbsolutePath()));
  189.                                         ta.setText(br.readLine());
  190.                                         do
  191.                                         {
  192.                                                 str=br.readLine();
  193.                                                 if(str==null)
  194.                                                         break;
  195.                                                 ta.append("\n"+str);
  196.                                         }while(true);
  197.                                         br.close();
  198.                                         fm.setTitle(title+" - "+fc.getName(fi));
  199.                                 }catch(Exception ex){}
  200.                                
  201.                         }       
  202.                 }
  203.                 else if(e.getSource()==mi_save)
  204.                 {
  205.                         int ret=fc.showSaveDialog(null);
  206.                         if(ret==JFileChooser.APPROVE_OPTION)
  207.                         {
  208.                                 try
  209.                                 {
  210.                                         File fi=fc.getSelectedFile();
  211.                                         BufferedWriter bw;
  212.                                         String ext=fi.getAbsolutePath().substring(fi.getAbsolutePath().length()-4);
  213.                                         System.out.println(ext);
  214.                                         String fiPath="";
  215.                                         if(fc.getFileFilter()==filter1)
  216.                                         {
  217.                                                 if(ext.equals(".txt"))
  218.                                                         fiPath=fi.getAbsolutePath();
  219.                                             else
  220.                                                     fiPath=fi.getAbsolutePath()+".txt";       
  221.                                         }
  222.                                         else
  223.                                                 fiPath=fi.getAbsolutePath();
  224.                                         bw=new BufferedWriter(new FileWriter(fiPath));
  225.                                        
  226.                                         bw.write(ta.getText().replaceAll("\n", "\r\n"));
  227.                                         //windows下的文字檔分行符號:\r\n  linux/unix下的文字檔分行符號:\r  Mac下的文字檔分行符號:\n
  228.                                         bw.flush();
  229.                                         bw.close();
  230.                                         fm.setTitle(title+" - "+fc.getName(fi));
  231.                                 }catch(Exception ex){}
  232.                         }       
  233.                 }
  234.                 else if(e.getSource()==mi_exit)
  235.                         System.exit(0);
  236.                 else if(e.getSource()==mi_copy)
  237.                     ta.copy();
  238.                 else if(e.getSource()==mi_paste)
  239.                         ta.paste();
  240.                 else if(e.getSource()==mi_cut)
  241.             ta.cut();
  242.                 else if(e.getSource()==mi_selectall)
  243.                         ta.selectAll();
  244.                 else if(e.getSource()==mi_about)
  245.                 {
  246.                         String msg="本軟體由社團法人高雄市資訊培育協會青少年程式設計班學員\n林宇翔所開發,感謝您的使用!";
  247.                         JOptionPane.showMessageDialog(fm,msg,"關於 My Editor",JOptionPane.INFORMATION_MESSAGE,ic_developer);
  248.                 }
  249.         }
  250.        
  251.         public static void main(String[] args) {
  252.                 new Ch09();
  253.         }
  254. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

返回列表