1、java 代码统计工具及完整代码大神请绕道一个 Java 图形界面小程序,可以用来统计指定目录下的 java 代码总行数,注释行数。在 win7 以及 xp 下运行通过,其他非 windows环境需要修改代码。复制以上对象(图标),粘贴到桌面即可。或者直接双击运行已打包成 jar 文件,jar 中含有代码。点击保存后可以使用,不能打开的可能是 jdk 版本不同,需要打开代码重新编译。简介:程序可以通过打开对话框选择文件或文件夹,也可以直接输入目录对 java 代码进行统计。点击确定后开始统计代码数据,统计完成后可以进行清零。程序使用截图的方式模拟打印的功能,注意,只对这个程序进行截图。(1)自
2、主打印:点击后弹出对话框,让用户选择目录,保存窗体的屏幕截图。(2)默认打印:使用默认的目录,保存这个程序的截图,快速迅速。打印后图片名称设置为“xiaobai”加上系统此时的年月日时分秒。方便知道在此时此刻的代码量。程序运行后如图所示:java 代码统计工具.jar大神请绕道代码块,分为三个类:FileRowCount.java, LinkLabel.java, LinkListener.javaFileRowCount 是主窗体以及界面,包括几个内部类,实现主要功能。ShotPrint 与 LinkListener 是打印类与监听器类。打印类根据窗体的位置以及区域,对屏幕像素进行截取,实现
3、截图功能;监听器类实现点击标签后调用默认浏览器实现跳转到指定网址,可以修改模仿跳转到Advertisement 页面的功能。下面分别是三个类的代码:FileRowCount.javaimport java.awt.Button;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Label;import java.awt.Rectangle;import java.awt.
4、TextField;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import javax.swing.Box
5、;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.SwingUtilities;import javax.swing.UIManager;public class FileRowCount extends JFrame/*大神请绕道* */ private static final long serialVersionUID = 1L; private static FileRowCount frame; private static Label infoLabelname,infoLab
6、elpath;/显示信息标签 static int spaceLine = 0; static int commentLine = 0; static int codeLine = 0; private Label inputLabel; private TextField inputTextField; private Button cleanButton; private Button yesButton; private Label code; private TextField codeTextField; private Label comment; private TextFiel
7、d commentTextField; private Label space; private TextField spaceTextField; private Label total; private TextField totalTextField; private Button openButton; private Button printButton; private Button defaultPrintButton; private Box baseBox,boxV1,boxV2,boxV3,boxV4,boxV5; public void getFileName(Strin
8、g filePath) File f = new File(filePath); if (!f.isDirectory() if (!f.getName().endsWith(“.java“) return; count(f); else String fileList = f.list(); for (int i = 0; i fileList.length; +i) File file = new File(filePath + “ + fileListi); if (!file.isDirectory() if (!file.getName().endsWith(“.java“) con
9、tinue; count(file); else大神请绕道getFileName(file.getPath();public void count(File f)String line = null;boolean comment = false;BufferedReader br = null;trybr = new BufferedReader(new FileReader(f);line = null;while (line = br.readLine() != null)line = line.trim();if (line.matches(“sn*$“)spaceLine += 1;
10、else if (line.startsWith(“/“) | (line.startsWith(“/*“) (line.endsWith(“*/“)commentLine += 1;else if (line.startsWith(“/*“) (!line.endsWith(“*/“)commentLine += 1;comment = true;else if (line.endsWith(“*/“) (comment)commentLine += 1;comment = false;else if (comment)commentLine += 1;elsecodeLine += 1;c
11、atch (Exception e)System.out.println(e.getStackTrace();finallyif (br != null)try 大神请绕道 br.close(); br = null; catch (IOException e) e.printStackTrace(); public static void main(String args) frame = new FileRowCount(); frame.start(); void start() Dimension dimension = Toolkit.getDefaultToolkit().getS
12、creenSize(); int width = (int)(dimension.getHeight()/1.8); int height = (int)(dimension.getHeight()/1.4); inputLabel = new Label(“代码位置:“); inputTextField = new TextField(20); cleanButton = new Button(“清零 “); yesButton = new Button(“确定“); code = new Label(“代码行数:“); codeTextField = new TextField(20);
13、comment = new Label(“注释行数:“); commentTextField = new TextField(20); space = new Label(“空格行数:“); spaceTextField = new TextField(20); total = new Label(“总计行数:“); totalTextField = new TextField(20); openButton = new Button(“打开“); printButton = new Button(“自主打印“); defaultPrintButton = new Button(“默认打印“)
14、; Font font = new Font(“黑体“,Font.PLAIN , 12); Label xiao = new Label(“小白菜 QQ:1379598589“); xiao.setFont(font); xiao.setForeground(Color.blue); boxV1 = Box.createVerticalBox(); boxV1.add(inputLabel); Container box1 = new Container();大神请绕道/ Box box1 = Box.createHorizontalBox(); Box box2 = Box.createHo
15、rizontalBox(); / Container box2 = new Container(); box1.setLayout(new FlowLayout(); / box2.setLayout(new GridLayout(); box1.add(openButton); box1.add(inputTextField); box2.add(yesButton); box2.add(cleanButton); / box2.add(yesButton); / box2.add(cleanButton); boxV1.add(box1); boxV1.add(Box.createVert
16、icalStrut(8); boxV1.add(box2); boxV1.add(Box.createVerticalStrut(20); boxV2 = Box.createHorizontalBox(); boxV2.add(code); boxV2.add(codeTextField); boxV3 = Box.createHorizontalBox(); boxV3.add(comment); boxV3.add(commentTextField); boxV4 = Box.createHorizontalBox(); boxV4.add(space); boxV4.add(space
17、TextField); boxV5 = Box.createHorizontalBox(); boxV5.add(total); boxV5.add(totalTextField); Container boxV6 = new Container(); / Box boxV6 = Box.createHorizontalBox(); boxV6.setLayout(new FlowLayout(FlowLayout.RIGHT); boxV6.add(printButton); boxV6.add(defaultPrintButton); Box infoBox = Box.createVer
18、ticalBox(); infoLabelname = new Label(); infoLabelpath = new Label(); infoBox.add(infoLabelname);大神请绕道infoBox.add(infoLabelpath); Container authorBox = new Container(); authorBox.setLayout(new FlowLayout(FlowLayout.CENTER); authorBox.add(xiao); baseBox = Box.createVerticalBox(); baseBox.add(boxV1);
19、baseBox.add(Box.createVerticalStrut(20); baseBox.add(boxV2); baseBox.add(Box.createVerticalStrut(20); baseBox.add(boxV3); baseBox.add(Box.createVerticalStrut(20); baseBox.add(boxV4); baseBox.add(Box.createVerticalStrut(20); baseBox.add(boxV5); baseBox.add(Box.createVerticalStrut(20); baseBox.add(box
20、V6); baseBox.add(Box.createVerticalStrut(10); baseBox.add(infoBox); baseBox.add(Box.createVerticalStrut(20); baseBox.add(authorBox); setSize(width, height); setResizable(true); / setLayout(new BorderLayout(); setLayout(new FlowLayout(FlowLayout.CENTER,30,30); setLocationRelativeTo(null); setTitle(“j
21、ava 代码统计工具“); setVisible(true); add(baseBox); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); this.yesButton.addActionListener(new FileRowCount.yesActionListener(); this.cleanButton.addActionListener(new FileRowCount.emptyActionListener(); this.openB
22、utton.addActionListener(new OpenDialog();大神请绕道 this.printButton.addActionListener(new PrintResult(); defaultPrintButton.addActionListener(new DefaultPrintResult(); xiao.addMouseListener(new LinkListener(); class OpenDialog implements ActionListener Override public void actionPerformed(ActionEvent e)
23、 try UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel“); / Object object = “选定“,“选定“; catch (Exception e1) e1.printStackTrace(); JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle(“选定“); chooser.setDialogType(JFileChooser.CUSTOM_DIALOG); chooser.setFileSele
24、ctionMode(JFileChooser.FILES_AND_DIRECTORIES);int state = chooser.showDialog(null, “选定“);SwingUtilities.updateComponentTreeUI(chooser);if(state=JFileChooser.APPROVE_OPTION)String dir = chooser.getSelectedFile().getPath();inputTextField.setText(dir);class emptyActionListenerimplements ActionListenere
25、mptyActionListener()public void actionPerformed(ActionEvent e)FileRowCount.this.codeTextField.setText(“);FileRowCmentTextField.setText(“);FileRowCount.this.spaceTextField.setText(“);FileRowCount.this.totalTextField.setText(“);FileRowCount.codeLine = FileRowCmentLine = FileRowCount.spaceLine = 0;大神请绕
26、道class yesActionListener implements ActionListeneryesActionListener()public void actionPerformed(ActionEvent e)FileRowCount.this.getFileName(FileRowCount.this.inputTextField.getText(); FileRowCount.this.codeTextField.setText(newInteger(FileRowCount.codeLine).toString();FileRowCmentTextField.setText(
27、newInteger(FileRowCmentLine).toString();FileRowCount.this.spaceTextField.setText(newInteger(FileRowCount.spaceLine).toString();FileRowCount.this.totalTextField.setText(new Integer(FileRowCount.codeLine + FileRowCmentLine + FileRowCount.spaceLine).toString();FileRowCount.codeLine = FileRowCmentLine =
28、 FileRowCount.spaceLine = 0;class PrintResult implements ActionListener Overridepublic void actionPerformed(ActionEvent e) ShotPrint cam= new ShotPrint();Rectangle rectangle = frame.getBounds();File fileLabel=cam.snapShot(rectangle);infoLabelname.setText(“文件名:“+fileLabel.getName();infoLabelpath.setT
29、ext(“目录:“+fileLabel.getParentFile();class DefaultPrintResult implements ActionListener Overridepublic void actionPerformed(ActionEvent e) ShotPrint cam= new ShotPrint();Rectangle rectangle = frame.getBounds();/获取窗体的坐标区域File fileLabel = cam.defaultShotPrint(rectangle);/传区域到默认的截图方法,并返回生成的图片的路径和名称infoL
30、abelname.setText(“文件名:“+fileLabel.getName();infoLabelpath.setText(“目录:“+fileLabel.getParentFile();大神请绕道ShotPrint.javaimport java.awt.Dimension;import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.io.File;import java.text.SimpleDateFo
31、rmat;import java.util.Date;import javax.imageio.ImageIO;import javax.swing.JFileChooser;import javax.swing.JOptionPane;import javax.swing.SwingUtilities;import javax.swing.UIManager;/* author LuTao*/public class ShotPrintprivate String fileName; /文件名private String imageFormat; /图片格式Dimension d = Too
32、lkit.getDefaultToolkit().getScreenSize();public ShotPrint() /* 默认打印,路径:d:xiaobai/*/public File defaultShotPrint(Rectangle rectangle) imageFormat= “png“;大神请绕道/fileName = “xiaobai“+getDateTime()+“.“+imageFormat; String pathname = “d:/xiaobai“; File file = new File(pathname,fileName); writeImage(file,
33、rectangle); return file; /* * 自主打印 */ public File snapShot(Rectangle rectangle) /默认 png 格式 imageFormat = “png“;/格式 fileName = “xiaobai“+getDateTime()+“.“+imageFormat; File path = shotDialog(); File file = new File(path,fileName); writeImage(file, rectangle); return file; /* * 根据传来的文件,以及窗体的坐标区域,获取屏幕像
34、素 */ public void writeImage(File file,Rectangle rectangle) try BufferedImage frameShot = (new Robot().createScreenCapture(rectangle); System.out.println(file); if(!file.exists() / file.mkdirs(); file.createNewFile(); ImageIO.write(frameShot, imageFormat, file); catch (Exception ex) System.out.printl
35、n(ex); private String getDateTime() Date time=new Date(); Calendar time = new GregorianCalendar(); SimpleDateFormat geshi=new SimpleDateFormat(“yyyyMMdd_HHmmss“); String date_time=geshi.format(time); return date_time; 大神请绕道private File shotDialog() File filedir = null; try UIManager.setLookAndFeel(“
36、com.sun.java.swing.plaf.windows.WindowsLookAndFeel“); catch (Exception e1) e1.printStackTrace(); JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle(“保存“); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);int state = chooser.showDialog(null, “保存“);SwingUtilities.updateC
37、omponentTreeUI(chooser);if(state=JFileChooser.APPROVE_OPTION)filedir = chooser.getSelectedFile().getAbsoluteFile();if(filedir=null)/ JOptionPane.showOptionDialog(parentComponent, message, title, optionType, messageType, icon, options, initialValue)Object options1 = “确定“,“ 取消“;JOptionPane.showOptionD
38、ialog(null, “路径不存在,是否保存到默认位置:n d:/xiaobai?“,“提示“,JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null,options1, options10);int sta = 0;if(sta = JOptionPane.OK_OPTION)filedir = new File(“d:xiaobai“);else if(state = JFileChooser.CANCEL_OPTION) / if(filedir=null)/ /JOptionPane.showOptionDia
39、log(parentComponent, message, title, optionType, messageType, icon, options, initialValue)/ Object options1 = “确定“,“取消“;/ JOptionPane.showOptionDialog(null, “路径不存在,是否保存到默认位置:n d:/xiaobai?“,/ “提示“,JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null,大神请绕道 / / / / / / return filedir; optio
40、ns1, options10); int sta = 0; if(sta = JOptionPane.OK_OPTION) filedir = new File(“d:xiaobai“); LinkListener.javaimport java.awt.Desktop;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.IOException;import .URISyntaxException;public class LinkListener implements Mous
41、eListener /* String blog = “http:/ Advertisement,我的博客public LinkListener() try Desktop.getDesktop().browse(new .URI(blog); catch (IOException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); catch (URISyntaxException e1) / TODO 自动生成的 catch 块 e1.printStackTrace();/ TODO 自动生成的构造函数存根*/Overridepublic void
42、mouseClicked(MouseEvent e) String url = “http:/ try Desktop.getDesktop().browse(new .URI(url);大神请绕道 catch (IOException e1) / TODO 自动生成的 catch 块 e1.printStackTrace(); catch (URISyntaxException e1) / TODO 自动生成的 catch 块 e1.printStackTrace(); Override public void mouseEntered(MouseEvent e) Override public void mouseExited(MouseEvent e) / TODO 自动生成的方法存根 Override public void mousePressed(MouseEvent e) Override public void mouseReleased(MouseEvent e) / TODO 自动生成的方法存根 / TODO 自动生成的方法存根 / TODO 自动生成的方法存根