返回列表 發帖
  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.JScrollPane;
  11. import javax.swing.JTextArea;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.UIManager;
  14. import javax.swing.border.BevelBorder;
  15. import javax.swing.filechooser.FileNameExtensionFilter;
  16. import java.awt.event.ActionListener;
  17. import java.awt.event.ActionEvent;
  18. import java.io.BufferedReader;
  19. import java.io.BufferedWriter;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.FileWriter;

  23. public class Ch100 implements ActionListener{
  24.         
  25.         String title="My Editor";
  26.         JFrame fm,fm2;
  27.         JScrollPane sp;
  28.         JTextArea ta;
  29.         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;
  30.         JMenuBar mb;
  31.         JMenu mn_file,mn_edit,mn_option,mn_help,mn_size,mn_type,mn_style;
  32.         JMenuItem mi_new,mi_open,mi_save,mi_exit,mi_copy,mi_paste,mi_cut,mi_selectall,mi_about;
  33.         JRadioButtonMenuItem rbmi_1[]=new JRadioButtonMenuItem[3];
  34.         JRadioButtonMenuItem rbmi_2[]=new JRadioButtonMenuItem[4];
  35.         JRadioButtonMenuItem rbmi_3[]=new JRadioButtonMenuItem[2];
  36.         ButtonGroup bg_1,bg_2,bg_3;
  37.         JFileChooser fc;
  38.         FileNameExtensionFilter filter1;
  39.         
  40.         void initialize()
  41.         {
  42.                 try {
  43.                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  44.                 } catch (Exception e){}

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

  104.             
複製代碼

TOP

  1.     mn_file=new JMenu(" 檔案(F) ");
  2.                 mn_file.setMnemonic('F');     
  3.                 mn_file.add(mi_new);
  4.                 mn_file.add(mi_open);
  5.                 mn_file.add(mi_save);
  6.                 mn_file.addSeparator();   
  7.                 mn_file.add(mi_exit);
  8.                
  9.                 mn_edit=new JMenu(" 編輯(E) ");
  10.                 mn_edit.setMnemonic('E');   
  11.                 mn_edit.add(mi_copy);
  12.                 mn_edit.add(mi_paste);
  13.                 mn_edit.add(mi_cut);
  14.                 mn_edit.addSeparator();
  15.                 mn_edit.add(mi_selectall);
  16.                
  17.                 mn_size=new JMenu("文字大小");
  18.                 mn_size.setIcon(ic_size);
  19.                 for(int i=0; i<rbmi_1.length; i++)
  20.                         mn_size.add(rbmi_1[i]);
  21.                 mn_type=new JMenu("文字樣式");
  22.                 mn_type.setIcon(ic_type);
  23.                 for(int i=0; i<rbmi_2.length; i++)
  24.                         mn_type.add(rbmi_2[i]);
  25.                 mn_style=new JMenu("風格");
  26.                 mn_style.setIcon(ic_style);
  27.                 for(int i=0; i<rbmi_3.length; i++)
  28.                         mn_style.add(rbmi_3[i]);
  29.                
  30.                 mn_option=new JMenu(" 設定(O) ");
  31.                 mn_option.setMnemonic('O');
  32.                 mn_option.add(mn_size);
  33.                 mn_option.add(mn_type);
  34.                 mn_option.addSeparator();
  35.                 mn_option.add(mn_style);
  36.                
  37.                 mn_help=new JMenu(" 說明(H) ");
  38.                 mn_help.setMnemonic('H');   
  39.                 mn_help.add(mi_about);
  40.         
  41.                 mb=new JMenuBar();
  42.                 mb.setBorder(new BevelBorder(BevelBorder.RAISED));   //設定具陰影效果的邊框
  43.                 mb.add(mn_file);
  44.                 mb.add(mn_edit);
  45.                 mb.add(mn_option);
  46.                 mb.add(mn_help);
  47.                
  48.                 ta=new JTextArea();
  49.                 ta.setFont(new Font("新細明體", Font.PLAIN, 18));
  50.                 ta.setLineWrap(true);   
  51.                
  52.                 sp=new JScrollPane(ta);
  53.                
  54.                 fm=new JFrame(title+" - 未命名");
  55.                 fm.setBounds(100, 100, 500, 350);
  56.                 fm.setIconImage(ic.getImage());
  57.                 fm.setVisible(true);
  58.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59.                 fm.add(sp);
  60.                 fm.setJMenuBar(mb);
  61.         }
  62.         
  63.         Ch100()
  64.         {
  65.                 initialize();
  66.         }
  67.         
  68.         public void actionPerformed(ActionEvent e)
  69.         {
  70.                 if(e.getSource()==mi_new)
  71.                 {
  72.                     ta.setText("");
  73.                     fm.setTitle(title+" - 未命名");
  74.                 }
  75.                 else if(e.getSource()==mi_open)
  76.                 {
  77.                         int ret=fc.showOpenDialog(null);
  78.                         if(ret==JFileChooser.APPROVE_OPTION)
  79.                         {
  80.                                 try
  81.                                 {
  82.                                         String str;
  83.                                         File fi=fc.getSelectedFile();
  84.                                         BufferedReader br=new BufferedReader(new FileReader(fi.getAbsolutePath()));
  85.                                         ta.setText(br.readLine());
  86.                                         do
  87.                                         {
  88.                                                 str=br.readLine();
  89.                                                 if(str==null)
  90.                                                         break;
  91.                                                 ta.append("\n"+str);
  92.                                         }while(true);
  93.                                         br.close();
  94.                                         fm.setTitle(title+" - "+fc.getName(fi));
  95.                                 }catch(Exception ex){}
  96.                                 
  97.                         }        
  98.                 }
  99.                 else if(e.getSource()==mi_save)
  100.                 {
  101.                         int ret=fc.showSaveDialog(null);
  102.                         if(ret==JFileChooser.APPROVE_OPTION)
  103.                         {
  104.                                 try
  105.                                 {
  106.                                         File fi=fc.getSelectedFile();
  107.                                         BufferedWriter bw;
  108.                                         String ext=fi.getAbsolutePath().substring(fi.getAbsolutePath().length()-4);
  109.                                         System.out.println(ext);
  110.                                         String fiPath="";
  111.                                         if(fc.getFileFilter()==filter1)
  112.                                         {
  113.                                                 if(ext.equals(".txt"))
  114.                                                         fiPath=fi.getAbsolutePath();
  115.                                             else
  116.                                                     fiPath=fi.getAbsolutePath()+".txt";        
  117.                                         }
  118.                                         else
  119.                                                 fiPath=fi.getAbsolutePath();
  120.                                         bw=new BufferedWriter(new FileWriter(fiPath));
  121.                                        
  122.                                         bw.write(ta.getText().replaceAll("\n", "\r\n"));
  123.                                        
  124.                                         bw.flush();
  125.                                         bw.close();
  126.                                         fm.setTitle(title+" - "+fc.getName(fi));
  127.                                 }catch(Exception ex){}
  128.                         }        
  129.                 }
  130.                 else if(e.getSource()==mi_exit)
  131.                         System.exit(0);
  132.                 else if(e.getSource()==mi_copy)
  133.                     ta.copy();
  134.                 else if(e.getSource()==mi_paste)
  135.                         ta.paste();
  136.                 else if(e.getSource()==mi_cut)
  137.             ta.cut();
  138.                 else if(e.getSource()==mi_selectall)
  139.                         ta.selectAll();
  140.                 else if(e.getSource()==mi_about)
  141.                 {
  142.                         String msg="本軟體由社團法人高雄市資訊培育協會青少年程式設計班學員\n林宇翔所開發,感謝您的使用!";
  143.                         JOptionPane.showMessageDialog(fm,msg,"關於 My Editor",JOptionPane.INFORMATION_MESSAGE,ic_developer);
  144.                 }
  145.         }
  146.         
  147.         public static void main(String[] args)
  148.         {
  149.                 new Ch100();
  150.         }
  151. }
複製代碼

TOP

返回列表