2025-05-24

 

統計代碼的小工具,程序功能不是很完善,歡迎大傢多給意見

import java.awt.event.ActionEvent; 

import java.awt.event.ActionListener; 

import java.io.BufferedReader; 

import java.io.File; 

import java.io.FileNotFoundException; 

import java.io.FileReader; 

import java.io.IOException; 

import javax.swing.JFileChooser; 

import javax.swing.JFrame; 

import javax.swing.JMenu; 

import javax.swing.JMenuBar; 

import javax.swing.JMenuItem; 

import javax.swing.JScrollPane; 

import javax.swing.JTextArea; 

import javax.swing.UIManager; 

 

public class CodeCounterFrame extends JFrame 

    JMenuBar menuBar = new JMenuBar(); 

    JMenu fileMenu = new JMenu("文件"); 

    JMenuItem openMenuItem = new JMenuItem("打開"); 

    JTextArea txa = new JTextArea(); 

    JScrollPane jsp =new JScrollPane(txa); 

    String output = ""; 

    static long codeLines = 0; 

    static long commentLines = 0; 

    static long blankLines = 0; 

    public CodeCounterFrame() 

    { 

        this.setJMenuBar(menuBar); 

        menuBar.add(fileMenu); 

        fileMenu.add(openMenuItem); 

        txa.setEditable(false); //txa裡面的內容不能被編輯 

        add(jsp); 

         

        //內部匿名類監聽器 

        openMenuItem.addActionListener(new ActionListener() 

        { 

            public void actionPerformed(ActionEvent e) 

            { 

                if(e.getSource() == openMenuItem) 

                { 

                    //點擊瞭打開菜單 

                    //彈出文件選擇器 

                    JFileChooser chooser = new JFileChooser(); 

                    chooser.showOpenDialog(null); 

                     

                    //獲取選中的文件 

                    File selectedFile = chooser.getSelectedFile(); 

                    BufferedReader br = null; 

                    boolean flag = false; 

                    try { 

                         FileReader reader = new FileReader(selectedFile);//讀取文檔的內容 

                         br = new BufferedReader(reader); 

                         String line = ""; 

                          

                        //2讀文件 

                        while ((line = br.readLine()) != null) 

                        { 

                            line = line.trim(); // 除去註釋前的空格 

                            if (line.matches("^[ ]*$"))  

                            {  

                                // 匹配空行 

                                blankLines++; 

                            }  

                            else if (line.startsWith("//"))  

                            { 

                                commentLines++; 

                            }  

                              else if (line.startsWith("/*") && !line.endsWith("*/")) 

                             { 

                                commentLines++; 

                                flag = true; 

                            }  

                            else if (line.startsWith("/*") && line.endsWith("*/"))  

                            { 

                                commentLines++; 

                            }  

                            else if (flag == true)  

                            { 

                                commentLines++; 

                                if (line.endsWith("*/"))  

                                { 

                                    flag = false; 

                                } 

                            }  

                            else  

                            { 

                                codeLines++; 

                            } 

                        } 

 

                        //將結果保存到output 

                        output+=("代碼行數:" + codeLines+"\n"+"註釋行數:" + commentLines+"\n"

                                  +"空白行數:" + blankLines+"\n"+"總行數:" + (codeLines+commentLines+blankLines)); 

                         

                        //將統計的結果在txa裡面顯示 

                        txa.setText(output); 

                    }  

                    catch (FileNotFoundException ex)  

                    { 

                        ex.printStackTrace(); 

                    }  

                    catch (IOException ex)  

                    { 

                        ex.printStackTrace(); 

                    }  

                    finally  

                    { 

                        if (br != null)  

                        { 

                            try { 

                                br.close(); 

                                br = null; 

                            }  

                            catch (IOException ex)  

                            { 

                                ex.printStackTrace(); 

                            } 

                        } 

                    } 

                } 

            } 

        }); 

    } 

     

    //Frame主程序 

       public static void main(String[] args)  

       { 

           try { 

                 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows界面風格 

            } catch (Exception e) { 

                e.printStackTrace(); 

            } 

            CodeCounterFrame frame = new CodeCounterFrame(); 

            frame.setTitle("統計代碼的小工具"); 

            frame.setSize(300,200); 

            frame.setVisible(true); 

            frame.setLocationRelativeTo(null);//窗體居中顯示 

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       } 

運行的窗口:

 

輸出的結果:

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *