import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*;
import javax.swing.*; import javax.swing.table.DefaultTableModel;
public class CountCodes extends JFrame{ //新增 private long allCode = 0; private long allComment = 0; private long allBlank = 0; private long codeLines = 0; private long commentLines = 0; private long blankLines = 0; private File fileName; private JLabel pathLabel; private JTextField pathField; private JButton openButton; private JPanel panelNorth, panelSouth;
private ArrayList<File> list = new ArrayList<File> (); private String[] tableHeader = {"文件名", "JAVA代码行数", "注释行数", "空行"}; private Vector<String> vectorHeader = new Vector<String> (); private Vector<Vector> values = new Vector<Vector> (); private JTable table; private DefaultTableModel tableModel; private JScrollPane scrollPane; public CountCodes(){ super("Count Codes"); pathLabel = new JLabel("文件路径: "); pathField = new JTextField(100); pathField.setEditable(false); openButton = new JButton("打开"); openButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ openFile(); if (fileName == null) return; analysisFileOrDir(fileName); for(File file : list){ analyze(file); } //新增 System.out.println(allCode); System.out.println(allComment); System.out.println(allBlank); System.out.println("-------------------"); Vector<String> value = new Vector<String> (); value.add("总计"); value.add(String.valueOf(allCode)); value.add(String.valueOf(allComment)); value.add(String.valueOf(allBlank)); values.add(value); } }); panelNorth = new JPanel(); panelNorth.setLayout(new BorderLayout()); panelNorth.add(pathLabel, BorderLayout.WEST); panelNorth.add(pathField, BorderLayout.CENTER); panelNorth.add(openButton, BorderLayout.EAST); for (int i=0; i<tableHeader.length; i++) vectorHeader.add(tableHeader[i]); tableModel = new DefaultTableModel(values, vectorHeader);
table = new JTable(tableModel); scrollPane = new JScrollPane(table); panelSouth = new JPanel(); panelSouth.setLayout(new BorderLayout()); panelSouth.add(scrollPane, BorderLayout.CENTER); setLayout(new BorderLayout()); add(panelNorth, BorderLayout.NORTH); add(panelSouth, BorderLayout.CENTER); setSize(450, 400); setVisible(true); } public static void main(String args[]){ CountCodes countCodes = new CountCodes(); countCodes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void openFile(){ JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.CANCEL_OPTION) return; fileName = fileChooser.getSelectedFile(); if (fileName == null || fileName.getName().equals("")) JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE); pathField.setText(fileName.toString()); } private void zeroVariables(){ this.codeLines = 0; this.commentLines = 0; this.blankLines = 0; } private void analysisFileOrDir(File fileName){ if(fileName.isFile()){ list.add(fileName); return; } else{ File[] fileArray = fileName.listFiles(); for(File inArray : fileArray){ if(inArray.isFile() && inArray.getName().matches(".*\\.java$")){ list.add(inArray); } else if(inArray.isDirectory()){ analysisFileOrDir(inArray); } } } } private void analyze(File fileName){ Vector<String> value = new Vector<String> (); String[] strArray = fileName.toString().split("\\\\"); // value.add(strArray[strArray.length-1]); value.add(fileName.toString()); BufferedReader br = null; boolean comment = false; try { br = new BufferedReader(new FileReader(fileName)); String line = ""; while ((line = br.readLine()) != null) { line = line.trim(); if (line.matches("^[\\s&&[^\\n]]*$")) { blankLines++; } else if (line.startsWith("/*") && !line.endsWith("*/")) { commentLines++; comment = true; } else if (line.startsWith("/*") && line.endsWith("*/")) { commentLines++; } else if (true == comment) { commentLines++; if (line.endsWith("*/")) { comment = false; } } else if (line.startsWith("//")) { commentLines++; } else { codeLines++; } } value.add(String.valueOf(codeLines)); value.add(String.valueOf(commentLines)); value.add(String.valueOf(blankLines)); values.add(value); this.table.revalidate(); allCode+=codeLines; allComment+=commentLines; allBlank+=blankLines; // System.out.println(all1);// System.out.println(all2);// System.out.println(all3);// System.out.println("-------------------"); zeroVariables(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } } }