import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;public class CountCodes extends JFrame{
private long codeLines = 0;
private long commentLines = 0;
private long blankLines = 0;
private File fileName;
private String output = "";

private JLabel pathLabel;
private JTextField pathField;
private JButton openButton;
private JTextArea outputArea;
private JPanel panelNorth, panelSouth;
private JScrollPane scrollPane; private ArrayList<File> list = new ArrayList<File> ();

public CountCodes(){
super("Count Codes");

pathLabel = new JLabel("File Path: ");
pathField = new JTextField(100);
pathField.setEditable(false);
openButton = new JButton("Open");

openButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
openFile();

if (fileName == null)
return;

analysisFileOrDir(fileName);

for(File file : list){
display(file);
}
}
});
panelNorth = new JPanel();

panelNorth.setLayout(new BorderLayout());
panelNorth.add(pathLabel, BorderLayout.WEST);
panelNorth.add(pathField, BorderLayout.CENTER);
panelNorth.add(openButton, BorderLayout.EAST);

outputArea = new JTextArea();
outputArea.setEditable(false);
scrollPane = new JScrollPane(outputArea);

panelSouth = new JPanel();
panelSouth.setLayout(new BorderLayout());
panelSouth.add(scrollPane, BorderLayout.CENTER);

setLayout(new BorderLayout());
add(panelNorth, BorderLayout.NORTH);
add(panelSouth, BorderLayout.CENTER);

validate();
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 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 display(File fileName){
String[] strArray = fileName.toString().split("\\\\");

output += "\tcodeLines\tcommentLines\tblankLines\n"+ strArray[strArray.length-1] +"\t";

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++;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                    br = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        output += codeLines +"\t"+ commentLines +"\t"+ blankLines +"\n";
outputArea.append(output);
}
}
做出来后 有个显著的缺点 就是当.java文件名太长时,会造成界面的不一致,大家给点意见。除此之外,还有没有什么好的建议?

解决方案 »

  1.   

    把每一行分成char[]然后填充....固定从某个位置结束文件名
      

  2.   

    codeLines,commentLines,blankLines那些不用每行都寫一次吧
      

  3.   

    Of course you can...totally ok...
      

  4.   

    Tonight I will update my codes...
      

  5.   

    Yesterday, our team went to KTV... I went home so late... So the new version with .exe file will be realsed in this week...
      

  6.   

    Yes, I have not conside this situation...So here is my step:
    1. Use JTable to improve my display.
    2. Make a .exe file.
    3. Add your advice to codesThanks a lot!