计算小工具

package mymenu;
import java.awt.event.*;  
import java.io.*;  
import javax.swing.*; 
 
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); //Tax里面的内容不能被编辑  
    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));  
                          
                        //将统计的结果在tax里面显示  
                        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);  
       }  
}  
posted @ 2012-05-10 08:56  流-星-追-月  阅读(195)  评论(0编辑  收藏  举报