2017年11月22日 星期三

文字編輯器























  1. /* 
  2.  * To change this license header, choose License Headers in Project Properties. 
  3.  * To change this template file, choose Tools | Templates 
  4.  * and open the template in the editor. 
  5.  */  
  6. package browse;  
  7.   
  8. import java.awt.Color;  
  9. import java.io.*;  
  10. import java.util.logging.Level;  
  11. import java.util.logging.Logger;  
  12. import javax.swing.ImageIcon;  
  13. import javax.swing.*;  
  14. import javax.swing.JOptionPane;  
  15. import javax.swing.text.BadLocationException;  
  16.   
  17. /** 
  18.  * 
  19.  * @author user 
  20.  */  
  21. public class JTextOpenSaveEx extends javax.swing.JFrame {  
  22.   
  23.     /** 
  24.      * Creates new form JTextOpenSaveEx 
  25.      */  
  26.     public JTextOpenSaveEx() {  
  27.         initComponents();  
  28.         jSlider1.setValue(14);  
  29.         this.setTitle("文字編輯器");  
  30.     }  
  31.   
  32.     /** 
  33.      * This method is called from within the constructor to initialize the form. 
  34.      * WARNING: Do NOT modify this code. The content of this method is always 
  35.      * regenerated by the Form Editor. 
  36.      */  
  37.     @SuppressWarnings("unchecked")  
  38.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                            
  39.     private void initComponents() {  
  40.   
  41.         jMenu2 = new javax.swing.JMenu();  
  42.         jLabel1 = new javax.swing.JLabel();  
  43.         jScrollPane1 = new javax.swing.JScrollPane();  
  44.         jTextArea1 = new javax.swing.JTextArea();  
  45.         jSlider1 = new javax.swing.JSlider();  
  46.         jMenuBar1 = new javax.swing.JMenuBar();  
  47.         jMenu1 = new javax.swing.JMenu();  
  48.         jMenuItem1 = new javax.swing.JMenuItem();  
  49.         jMenuItem2 = new javax.swing.JMenuItem();  
  50.         jMenuItem3 = new javax.swing.JMenuItem();  
  51.         EDIT = new javax.swing.JMenu();  
  52.         jMenu3 = new javax.swing.JMenu();  
  53.         jMenuItem4 = new javax.swing.JMenuItem();  
  54.         jMenuItem5 = new javax.swing.JMenuItem();  
  55.   
  56.         jMenu2.setText("jMenu2");  
  57.   
  58.         setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);  
  59.         setMaximumSize(new java.awt.Dimension(698525));  
  60.         setMinimumSize(new java.awt.Dimension(698525));  
  61.         setPreferredSize(new java.awt.Dimension(698525));  
  62.         setResizable(false);  
  63.         addWindowListener(new java.awt.event.WindowAdapter() {  
  64.             public void windowClosing(java.awt.event.WindowEvent evt) {  
  65.                 formWindowClosing(evt);  
  66.             }  
  67.         });  
  68.   
  69.         jLabel1.setToolTipText("path");  
  70.         jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000)));  
  71.   
  72.         jTextArea1.setColumns(20);  
  73.         jTextArea1.setFont(new java.awt.Font("微軟正黑體 Light"014)); // NOI18N  
  74.         jTextArea1.setRows(5);  
  75.         jTextArea1.setTabSize(25);  
  76.         jTextArea1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 2));  
  77.         jTextArea1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));  
  78.         jScrollPane1.setViewportView(jTextArea1);  
  79.   
  80.         jSlider1.setToolTipText("textSize");  
  81.         jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {  
  82.             public void stateChanged(javax.swing.event.ChangeEvent evt) {  
  83.                 jSlider1StateChanged(evt);  
  84.             }  
  85.         });  
  86.         jSlider1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {  
  87.             public void mouseMoved(java.awt.event.MouseEvent evt) {  
  88.                 jSlider1MouseMoved(evt);  
  89.             }  
  90.         });  
  91.   
  92.         jMenuBar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 2));  
  93.   
  94.         jMenu1.setText("檔案");  
  95.   
  96.         jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.SHIFT_MASK));  
  97.         jMenuItem1.setText("開啟");  
  98.         jMenuItem1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));  
  99.         jMenuItem1.addActionListener(new java.awt.event.ActionListener() {  
  100.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  101.                 jMenuItem1ActionPerformed(evt);  
  102.             }  
  103.         });  
  104.         jMenu1.add(jMenuItem1);  
  105.   
  106.         jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK));  
  107.         jMenuItem2.setText("儲存");  
  108.         jMenuItem2.addActionListener(new java.awt.event.ActionListener() {  
  109.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  110.                 jMenuItem2ActionPerformed(evt);  
  111.             }  
  112.         });  
  113.         jMenu1.add(jMenuItem2);  
  114.   
  115.         jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));  
  116.         jMenuItem3.setText("另存新檔");  
  117.         jMenuItem3.addActionListener(new java.awt.event.ActionListener() {  
  118.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  119.                 jMenuItem3ActionPerformed(evt);  
  120.             }  
  121.         });  
  122.         jMenu1.add(jMenuItem3);  
  123.   
  124.         jMenuBar1.add(jMenu1);  
  125.   
  126.         EDIT.setText("Edit");  
  127.         EDIT.addMouseListener(new java.awt.event.MouseAdapter() {  
  128.             public void mouseClicked(java.awt.event.MouseEvent evt) {  
  129.                 EDITMouseClicked(evt);  
  130.             }  
  131.         });  
  132.         jMenuBar1.add(EDIT);  
  133.   
  134.         jMenu3.setText("檢視");  
  135.   
  136.         jMenuItem4.setText("前景");  
  137.         jMenuItem4.addActionListener(new java.awt.event.ActionListener() {  
  138.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  139.                 jMenuItem4ActionPerformed(evt);  
  140.             }  
  141.         });  
  142.         jMenu3.add(jMenuItem4);  
  143.   
  144.         jMenuItem5.setText("背景");  
  145.         jMenuItem5.addActionListener(new java.awt.event.ActionListener() {  
  146.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  147.                 jMenuItem5ActionPerformed(evt);  
  148.             }  
  149.         });  
  150.         jMenu3.add(jMenuItem5);  
  151.   
  152.         jMenuBar1.add(jMenu3);  
  153.   
  154.         setJMenuBar(jMenuBar1);  
  155.   
  156.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
  157.         getContentPane().setLayout(layout);  
  158.         layout.setHorizontalGroup(  
  159.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  160.             .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 690, javax.swing.GroupLayout.PREFERRED_SIZE)  
  161.             .addGroup(layout.createSequentialGroup()  
  162.                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)  
  163.                 .addGap(333)  
  164.                 .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))  
  165.         );  
  166.         layout.setVerticalGroup(  
  167.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  168.             .addGroup(layout.createSequentialGroup()  
  169.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)  
  170.                 .addGap(888)  
  171.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  172.                     .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)  
  173.                     .addGroup(layout.createSequentialGroup()  
  174.                         .addGap(222)  
  175.                         .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))))  
  176.         );  
  177.   
  178.         pack();  
  179.     }// </editor-fold>                          
  180.   
  181.     private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                             
  182.         JFileChooser chooser2 = new JFileChooser(new File("C:\\"));  
  183.         chooser2.setDialogTitle("圖片開啟 : ");  
  184.         //  
  185.         chooser2.setFileFilter(new FileTypeFilter(".txt""Text File"));//設置文件過濾器   
  186.   
  187.         //  
  188.         int result = chooser2.showSaveDialog(null);  
  189.         if (result == JFileChooser.APPROVE_OPTION) {              
  190.             File file = chooser2.getSelectedFile();  
  191.             AbsolutePath = file.getAbsolutePath();  
  192.             jLabel1.setText(AbsolutePath);  
  193.             try {  
  194.                 String txt = "";  
  195.                 String txts = "";  
  196.                 BufferedReader n = new BufferedReader(new InputStreamReader(new FileInputStream(file), "big5"));  
  197.                 while ((txt = n.readLine()) != null) {  
  198.                     txts += txt + "\n";  
  199.                 }  
  200.                 jTextArea1.setText(txts);  
  201.                 n.close();  
  202.             } catch (IOException e) {  
  203.                 e.printStackTrace();  
  204.             }              
  205.         }  
  206.     }                                            
  207.     String AbsolutePath;  
  208.     private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                             
  209.         try {  
  210.             BufferedWriter n = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(AbsolutePath), "big5"));  
  211.             String fileName = jTextArea1.getText();  
  212.             String[] fileNames = fileName.split("\n");  
  213.             for (String N : fileNames) {  
  214.                 n.write(N + "\r\n");  
  215.             }  
  216.             n.close();  
  217.         } catch (IOException e) {  
  218.             e.printStackTrace();  
  219.         } catch (NullPointerException e) {  
  220.             JOptionPane.showMessageDialog(null"沒開啟文件");  
  221.         }  
  222.     }                                            
  223.   
  224.     private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                             
  225.         JFileChooser chooser2 = new JFileChooser(new File("C:\\"));  
  226.          chooser2.setDialogTitle("另存新檔 : ");  
  227.         //  
  228.         chooser2.setFileFilter(new FileTypeFilter(".txt""Text File"));//設置文件過濾器   
  229.         //  
  230.         int result = chooser2.showSaveDialog(null);  
  231.         if (result == JFileChooser.APPROVE_OPTION) {              
  232.             File file = chooser2.getSelectedFile();  
  233.             try {  
  234.                 BufferedWriter n = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file.getAbsolutePath()+".txt"), "big5"));  
  235.                 String fileName = jTextArea1.getText();  
  236.                 String[] fileNames = fileName.split("\n");  
  237.                 for (String N : fileNames) {  
  238.                     n.write(N + "\r\n");  
  239.                 }  
  240.                 n.close();  
  241.             } catch (FileNotFoundException e) {  
  242.                 JOptionPane.showMessageDialog(null"存取被拒""ERROR", JOptionPane.ERROR_MESSAGE);                  
  243.             } catch (IOException e) {  
  244.                 e.printStackTrace();  
  245.             }  
  246.               
  247.         }  
  248.     }                                            
  249.       
  250.     private void EDITMouseClicked(java.awt.event.MouseEvent evt) {                                    
  251.         // TODO add your handling code here:  
  252.         int is = JOptionPane.showOptionDialog(null"確定離開 ?""提醒", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, nullnullnull);  
  253.           
  254.         if (is == JOptionPane.YES_OPTION) {  
  255.             System.exit(0);  
  256.         }  
  257.     }                                   
  258.   
  259.     private void formWindowClosing(java.awt.event.WindowEvent evt) {                                     
  260.         int is = JOptionPane.showOptionDialog(null"確定離開 ?""提醒", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, nullnullnull);  
  261.         if (is == JOptionPane.YES_OPTION) {  
  262.             System.exit(0);  
  263.         }  
  264.     }                                    
  265.   
  266.     private void jSlider1MouseMoved(java.awt.event.MouseEvent evt) {                                      
  267.   
  268.     }                                     
  269.   
  270.     private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {                                        
  271.         size = jSlider1.getValue();  
  272.         jTextArea1.setFont(new java.awt.Font("微軟正黑體 Light"0, size));          
  273.     }                                       
  274.     int size;  
  275.     private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                             
  276.           
  277.         Color c = JColorChooser.showDialog(null"請選擇顏色", Color.yellow);  
  278.         jTextArea1.setForeground(c);  
  279.     }                                            
  280.   
  281.     private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {                                             
  282.         Color c = JColorChooser.showDialog(null"請選擇顏色", Color.yellow);  
  283.         jTextArea1.setBackground(c);  
  284.     }                                            
  285.   
  286.     /** 
  287.      * @param args the command line arguments 
  288.      */  
  289.     public static void main(String args[]) {  
  290.         /* Set the Nimbus look and feel */  
  291.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">  
  292.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
  293.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html  
  294.          */  
  295.         try {  
  296.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {  
  297.                 if ("Nimbus".equals(info.getName())) {  
  298.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());  
  299.                     break;  
  300.                 }  
  301.             }  
  302.         } catch (ClassNotFoundException ex) {  
  303.             java.util.logging.Logger.getLogger(JTextOpenSaveEx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  304.         } catch (InstantiationException ex) {  
  305.             java.util.logging.Logger.getLogger(JTextOpenSaveEx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  306.         } catch (IllegalAccessException ex) {  
  307.             java.util.logging.Logger.getLogger(JTextOpenSaveEx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  308.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {  
  309.             java.util.logging.Logger.getLogger(JTextOpenSaveEx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  310.         }  
  311.         //</editor-fold>  
  312.   
  313.         /* Create and display the form */  
  314.         java.awt.EventQueue.invokeLater(new Runnable() {  
  315.             public void run() {  
  316.                 new JTextOpenSaveEx().setVisible(true);  
  317.             }  
  318.         });  
  319.     }  
  320.   
  321.     // Variables declaration - do not modify                       
  322.     private javax.swing.JMenu EDIT;  
  323.     private javax.swing.JLabel jLabel1;  
  324.     private javax.swing.JMenu jMenu1;  
  325.     private javax.swing.JMenu jMenu2;  
  326.     private javax.swing.JMenu jMenu3;  
  327.     private javax.swing.JMenuBar jMenuBar1;  
  328.     private javax.swing.JMenuItem jMenuItem1;  
  329.     private javax.swing.JMenuItem jMenuItem2;  
  330.     private javax.swing.JMenuItem jMenuItem3;  
  331.     private javax.swing.JMenuItem jMenuItem4;  
  332.     private javax.swing.JMenuItem jMenuItem5;  
  333.     private javax.swing.JScrollPane jScrollPane1;  
  334.     private javax.swing.JSlider jSlider1;  
  335.     private javax.swing.JTextArea jTextArea1;  
  336.     // End of variables declaration                     
  337. }  

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。