package test;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class LineNum extends JFrame{
private JPanel topPanel = new JPanel();
private JPanel bottomPanel = new JPanel();
private JButton fileChoose = new JButton("选择目录");
private JTextField fileField = new JTextField(20);
private JFileChooser fc = new JFileChooser("选择目录");
private JTextArea filePathArea = new JTextArea(5 , 20);
/**普通行数*/
private long normalLines=0;
/**注释行数*/
private long commentLines=0;
/**空白行数*/
private long spaceLines=0;
/**总行数*/
private long totalLines=0;
private DecimalFormat myFormat = null;
public LineNum(String title)
{
super(title);
//设置面板
Container container = getContentPane();
container.setLayout(new BorderLayout());
topPanel.setLayout(new GridLayout(1 , 2));
bottomPanel.setLayout(new BorderLayout());
topPanel.add(fileChoose);
topPanel.add(fileField);
bottomPanel.add(new JScrollPane(filePathArea));
container.add(topPanel , BorderLayout.NORTH);
container.add(bottomPanel , BorderLayout.CENTER);
//添加选择目录监听,默认获取的是选择文件所在的父目录,程序统计对象是此父目录及其子目录下的所有java文件
fileChoose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int result = fc.showOpenDialog(LineNum.this);
if(result == JFileChooser.APPROVE_OPTION)
{
String path = fc.getSelectedFile().getAbsolutePath();
fileField.setText(path);
File file = new File(path);
CalculateLineNum(file);
}
}
});
}
/*
* 计算并显示统计信息
*/
private void CalculateLineNum(File file)
{
if(file.exists())
{
this.setNormalLines(0L);
this.setCommentLines(0L);
this.setSpaceLines(0L);
this.setTotalLines(0L);
displayLineNum(file);
myFormat = (DecimalFormat)NumberFormat.getPercentInstance();
myFormat.applyPattern("0.00%");
totalLines=normalLines+spaceLines+commentLines;
filePathArea.setText("");
filePathArea.setText(filePathArea.getText() + "\n" + " 总行数:" + totalLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 程序行数:" + normalLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 注释行数:" + commentLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 空行行数:" + spaceLines + "行");
}
}
//循环访问目录及子目录,统计代码总行数,注释行数及空行行数
public void displayLineNum(File file)
{
BufferedReader br=null;
boolean comment=false;
try {
br=new BufferedReader(new FileReader(file));
String line="";
while((line=br.readLine())!=null){
line=line.trim();//去除空格
//空白
if(line.matches("^[\\s&&[^\\n]]*$")) {
spaceLines ++;
}
// //
else if(line.startsWith("//")) {
commentLines ++;
}
// 有/*的情况
else if(line.indexOf("/*") != -1)
{
// /*开始的情况
if(line.startsWith("/*"))
{
// 同一行里有*/
if(line.indexOf("*/") != -1)
{
//以*/结尾
if(line.endsWith("*/"))
{
commentLines ++;
}
else
{
normalLines ++;
}
}
else
{
commentLines ++;
comment = true;
}
}
// /*不是开始的情况
else
{
//同一行里没有*/
if(line.indexOf("*/") == -1)
{
comment = true;
normalLines ++;
}
}
}
else if(true == comment)
{
//是否有*/
if(line.indexOf("*/") != -1)
{
if(line.endsWith("*/"))
{
commentLines ++;
}
else
{
normalLines ++;
}
comment = false;
}
else
{
commentLines ++;
}
}
else
{
normalLines ++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[])
{
LineNum lineFrame=new LineNum("java程序行数统计");
lineFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lineFrame.setBounds(212,159,600,420);
lineFrame.setVisible(true);
lineFrame.setResizable(false);
}
public long getNormalLines() {
return normalLines;
}
public void setNormalLines(long normalLines) {
this.normalLines = normalLines;
}
public long getCommentLines() {
return commentLines;
}
public void setCommentLines(long commentLines) {
this.commentLines = commentLines;
}
public long getSpaceLines() {
return spaceLines;
}
public void setSpaceLines(long spaceLines) {
this.spaceLines = spaceLines;
}
public long getTotalLines() {
return totalLines;
}
public void setTotalLines(long totalLines) {
this.totalLines = totalLines;
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class LineNum extends JFrame{
private JPanel topPanel = new JPanel();
private JPanel bottomPanel = new JPanel();
private JButton fileChoose = new JButton("选择目录");
private JTextField fileField = new JTextField(20);
private JFileChooser fc = new JFileChooser("选择目录");
private JTextArea filePathArea = new JTextArea(5 , 20);
/**普通行数*/
private long normalLines=0;
/**注释行数*/
private long commentLines=0;
/**空白行数*/
private long spaceLines=0;
/**总行数*/
private long totalLines=0;
private DecimalFormat myFormat = null;
public LineNum(String title)
{
super(title);
//设置面板
Container container = getContentPane();
container.setLayout(new BorderLayout());
topPanel.setLayout(new GridLayout(1 , 2));
bottomPanel.setLayout(new BorderLayout());
topPanel.add(fileChoose);
topPanel.add(fileField);
bottomPanel.add(new JScrollPane(filePathArea));
container.add(topPanel , BorderLayout.NORTH);
container.add(bottomPanel , BorderLayout.CENTER);
//添加选择目录监听,默认获取的是选择文件所在的父目录,程序统计对象是此父目录及其子目录下的所有java文件
fileChoose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int result = fc.showOpenDialog(LineNum.this);
if(result == JFileChooser.APPROVE_OPTION)
{
String path = fc.getSelectedFile().getAbsolutePath();
fileField.setText(path);
File file = new File(path);
CalculateLineNum(file);
}
}
});
}
/*
* 计算并显示统计信息
*/
private void CalculateLineNum(File file)
{
if(file.exists())
{
this.setNormalLines(0L);
this.setCommentLines(0L);
this.setSpaceLines(0L);
this.setTotalLines(0L);
displayLineNum(file);
myFormat = (DecimalFormat)NumberFormat.getPercentInstance();
myFormat.applyPattern("0.00%");
totalLines=normalLines+spaceLines+commentLines;
filePathArea.setText("");
filePathArea.setText(filePathArea.getText() + "\n" + " 总行数:" + totalLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 程序行数:" + normalLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 注释行数:" + commentLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 空行行数:" + spaceLines + "行");
}
}
//循环访问目录及子目录,统计代码总行数,注释行数及空行行数
public void displayLineNum(File file)
{
BufferedReader br=null;
boolean comment=false;
try {
br=new BufferedReader(new FileReader(file));
String line="";
while((line=br.readLine())!=null){
line=line.trim();//去除空格
//空白
if(line.matches("^[\\s&&[^\\n]]*$")) {
spaceLines ++;
}
// //
else if(line.startsWith("//")) {
commentLines ++;
}
// 有/*的情况
else if(line.indexOf("/*") != -1)
{
// /*开始的情况
if(line.startsWith("/*"))
{
// 同一行里有*/
if(line.indexOf("*/") != -1)
{
//以*/结尾
if(line.endsWith("*/"))
{
commentLines ++;
}
else
{
normalLines ++;
}
}
else
{
commentLines ++;
comment = true;
}
}
// /*不是开始的情况
else
{
//同一行里没有*/
if(line.indexOf("*/") == -1)
{
comment = true;
normalLines ++;
}
}
}
else if(true == comment)
{
//是否有*/
if(line.indexOf("*/") != -1)
{
if(line.endsWith("*/"))
{
commentLines ++;
}
else
{
normalLines ++;
}
comment = false;
}
else
{
commentLines ++;
}
}
else
{
normalLines ++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[])
{
LineNum lineFrame=new LineNum("java程序行数统计");
lineFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lineFrame.setBounds(212,159,600,420);
lineFrame.setVisible(true);
lineFrame.setResizable(false);
}
public long getNormalLines() {
return normalLines;
}
public void setNormalLines(long normalLines) {
this.normalLines = normalLines;
}
public long getCommentLines() {
return commentLines;
}
public void setCommentLines(long commentLines) {
this.commentLines = commentLines;
}
public long getSpaceLines() {
return spaceLines;
}
public void setSpaceLines(long spaceLines) {
this.spaceLines = spaceLines;
}
public long getTotalLines() {
return totalLines;
}
public void setTotalLines(long totalLines) {
this.totalLines = totalLines;
}
}