package com.cy;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
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.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/*
* 这个程序为计算java代码中有多少个程序行normalLines,注释行commentLines,空行whiteLines
*/
public class CodeCounter extends JFrame{
static long normalLines=0;
static long commentLines=0;
static long whiteLines=0;
static String path;
static File file;
final JFileChooser chooser=new JFileChooser(); //文件选择器
JDialog dialog=new JDialog(); //对话框
public static JTextField textField;
public static JTextField textField_1;
public static JTextField textField_2;
public static JButton button;
private JTextField textField_3;
public CodeCounter() {
getContentPane().setLayout(null);
JLabel label = new JLabel("normalLines:");
label.setForeground(Color.BLUE);
label.setBounds(62, 83, 95, 21);
getContentPane().add(label);
JLabel lblCommentlines = new JLabel("commentLines:");
lblCommentlines.setForeground(Color.GREEN);
lblCommentlines.setBounds(62, 137, 95, 21);
getContentPane().add(lblCommentlines);
JLabel lblWhitelines = new JLabel("whiteLines:");
lblWhitelines.setForeground(Color.ORANGE);
lblWhitelines.setBounds(74, 196, 75, 21);
getContentPane().add(lblWhitelines);
JLabel lbljava = new JLabel("this is a java file code counter");
lbljava.setForeground(Color.RED);
lbljava.setBounds(74, 10, 234, 50);
getContentPane().add(lbljava);
textField = new JTextField();
textField.setEditable(false);
textField.setBounds(167, 83, 79, 21);
getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setEditable(false);
textField_1.setBounds(167, 137, 79, 21);
getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setEditable(false);
textField_2.setBounds(167, 196, 79, 21);
getContentPane().add(textField_2);
textField_2.setColumns(10);
textField.setText("0");
textField_1.setText("0");
textField_2.setText("0");
button = new JButton("start");
button.setEnabled(false);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(file.getName().matches(".*\\.java$")){
parse(file);
textField.setText(normalLines+"");
textField_1.setText(commentLines+"");
textField_2.setText(whiteLines+"");
normalLines=0;
commentLines=0;
whiteLines=0;
}
else{
JOptionPane.showMessageDialog(null, "您选择的不是java文件");
}
/*System.out.println("normalLines:" +normalLines);
System.out.println("commentLines:" +commentLines);
System.out.println("whiteLines:" +whiteLines);*/
}
});
button.setBounds(49, 354, 93, 23);
getContentPane().add(button);
JButton button_1 = new JButton("exit");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
button_1.setBounds(189, 354, 93, 23);
getContentPane().add(button_1);
JButton button_2 = new JButton("choose a file");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String title=chooser.getDialogTitle(); // JFileChooser 的标题栏中所显示的字符串
if(title == null)
chooser.getUI().getDialogTitle(chooser);//获得实现此组件 L&F 的 UI 对象。
dialog = new JDialog((Frame)null, title, false);
dialog.getContentPane().setLayout(new BorderLayout());
dialog.getContentPane().add(chooser, BorderLayout.CENTER);
dialog.setTitle("请选择要显示的文件");
dialog.pack();
//dialog.setLocationRelativeTo(CodeCounter.this);
dialog.setVisible(true);
button.setEnabled(true);
}
});
chooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String state = (String)e.getActionCommand();
// 确定按钮被激活
if(state.equals(JFileChooser.APPROVE_SELECTION)) {
file = chooser.getSelectedFile();
//System.out.println(file.getAbsoluteFile());
/*JOptionPane.showMessageDialog(null, file.getPath());*/
textField_3.setText(file.getPath());
}
dialog.setVisible(false);
}
});
button_2.setForeground(Color.RED);
button_2.setBounds(101, 249, 145, 23);
getContentPane().add(button_2);
textField_3 = new JTextField();
textField_3.setBounds(10, 294, 326, 21);
getContentPane().add(textField_3);
textField_3.setColumns(10);
this.setTitle("Java CodeCounter");
this.setSize(352,429);
this.setLocation(100,100);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new CodeCounter();
/*//选择文件夹
File f=new File("E:\\temp\\Snake\\src\\");
File[] codeFiles=f.listFiles();
//以.java结尾的文件
for(File child:codeFiles){
if(child.getName().matches(".*\\.java$")){
parse(child);
}
}
*/
}
private static void parse(File f) {
BufferedReader br=null;
boolean comment=false;
try {
br=new BufferedReader(new FileReader(f));
String line="";
while((line=br.readLine())!=null){
line=line.trim();
if(line.matches("^[\\s&&[^\\n]]*")){ //正则表达式
whiteLines++;
}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 {
normalLines++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(br!=null){
try{
br.close();
br=null;
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
最近下载更多
shuiqing_cjn LV1
2023年4月7日
object-oriented440 LV3
2021年5月23日
yechenming LV9
2019年8月8日
灵依ziNing LV7
2019年5月10日
65152145 LV1
2019年4月22日
wangmeicong LV12
2018年11月30日
南风入笙 LV18
2018年7月22日
须臾景稍绸缪 LV5
2018年4月4日
xuehaibo08 LV3
2017年12月13日
mzoai LV19
2017年7月25日

最近浏览