计算小工具
2012-05-10 08:56
162 查看
package mymenu;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class CodeCounterFrame extends JFrame {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenuItem openMenuItem = new JMenuItem("打开");
JTextArea txa = new JTextArea();
JScrollPane jsp =new JScrollPane(txa);
String output = "";
static long codeLines = 0;
static long commentLines = 0;
static long blankLines = 0;
public CodeCounterFrame()
{
this.setJMenuBar(menuBar);
menuBar.add(fileMenu);
fileMenu.add(openMenuItem);
txa.setEditable(false); //Tax里面的内容不能被编辑
add(jsp); //内部匿名类监听器
openMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == openMenuItem)
{
//点击了打开菜单
//弹出文件选择器
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
//获取选中的文件
File selectedFile = chooser.getSelectedFile();
BufferedReader br = null;
boolean flag = false;
try {
FileReader reader = new FileReader(selectedFile);//读取文档的内容
br = new BufferedReader(reader);
String line = "";
//2读文件
while ((line = br.readLine()) != null)
{
line = line.trim(); // 除去注释前的空格
if (line.matches("^[ ]*$"))
{
// 匹配空行
blankLines++;
}
else if (line.startsWith("//"))
{
commentLines++;
}
else if (line.startsWith("/*") && !line.endsWith("*/"))
{
commentLines++;
flag = true;
}
else if (line.startsWith("/*") && line.endsWith("*/"))
{
commentLines++;
}
else if (flag == true)
{
commentLines++;
if (line.endsWith("*/"))
{
flag = false;
}
}
else
{
codeLines++;
}
//将结果保存到output
output+=("代码行数:" + codeLines+"\n"+"注释行数:" + commentLines+"\n"
+"空白行数: " + blankLines+"\n"+"总行数:" + (codeLines+commentLines+blankLines));
//将统计的结果在tax里面显示
txa.setText(output);
}
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (br != null)
{
try {
br.close();
br = null;
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
}
});
}
//Frame主程序
public static void main(String[] args)
{
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows界面风格
} catch (Exception e) {
e.printStackTrace();
}
CodeCounterFrame frame = new CodeCounterFrame();
frame.setTitle("统计代码的小工具");
frame.setSize(300,200);
frame.setVisible(true);
frame.setLocationRelativeTo(null);//窗体居中显示
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class CodeCounterFrame extends JFrame {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenuItem openMenuItem = new JMenuItem("打开");
JTextArea txa = new JTextArea();
JScrollPane jsp =new JScrollPane(txa);
String output = "";
static long codeLines = 0;
static long commentLines = 0;
static long blankLines = 0;
public CodeCounterFrame()
{
this.setJMenuBar(menuBar);
menuBar.add(fileMenu);
fileMenu.add(openMenuItem);
txa.setEditable(false); //Tax里面的内容不能被编辑
add(jsp); //内部匿名类监听器
openMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == openMenuItem)
{
//点击了打开菜单
//弹出文件选择器
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
//获取选中的文件
File selectedFile = chooser.getSelectedFile();
BufferedReader br = null;
boolean flag = false;
try {
FileReader reader = new FileReader(selectedFile);//读取文档的内容
br = new BufferedReader(reader);
String line = "";
//2读文件
while ((line = br.readLine()) != null)
{
line = line.trim(); // 除去注释前的空格
if (line.matches("^[ ]*$"))
{
// 匹配空行
blankLines++;
}
else if (line.startsWith("//"))
{
commentLines++;
}
else if (line.startsWith("/*") && !line.endsWith("*/"))
{
commentLines++;
flag = true;
}
else if (line.startsWith("/*") && line.endsWith("*/"))
{
commentLines++;
}
else if (flag == true)
{
commentLines++;
if (line.endsWith("*/"))
{
flag = false;
}
}
else
{
codeLines++;
}
//将结果保存到output
output+=("代码行数:" + codeLines+"\n"+"注释行数:" + commentLines+"\n"
+"空白行数: " + blankLines+"\n"+"总行数:" + (codeLines+commentLines+blankLines));
//将统计的结果在tax里面显示
txa.setText(output);
}
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (br != null)
{
try {
br.close();
br = null;
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
}
});
}
//Frame主程序
public static void main(String[] args)
{
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows界面风格
} catch (Exception e) {
e.printStackTrace();
}
CodeCounterFrame frame = new CodeCounterFrame();
frame.setTitle("统计代码的小工具");
frame.setSize(300,200);
frame.setVisible(true);
frame.setLocationRelativeTo(null);//窗体居中显示
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
相关文章推荐
- 并行计算工具OpenMP
- 通过开源工具orabm计算TPS值来测试服务器CPU性能
- 分享自用小工具:TF-IDF计算文档相似性的python实现
- 云计算仿真工具CloudSim介绍和使用
- 基于GPU计算的贴图生成工具Knald
- C#代码行数的计算工具(递归的实现)
- 代码计算工具(附源码)
- Python多版本切换工具-Pyenv\virtualenv及Anaconda科学计算环境的配置
- Distributed System: 常见的分布式计算工具以及MapReduce详解
- [python] 使用scikit-learn工具计算文本TF-IDF值
- 掩码计算工具netmask
- Pyhton科学计算工具Pandas(三) —— 数据结构Dataframe的基本操作
- 发一个贷款月供额计算工具[原创]
- taco:编译器级的张量计算优化工具
- 花了一晚上时间写了个ASN.1语法中TAG值计算工具,有图有源码!!!
- 计算文件夹内视频总长度工具
- C# 定向计算小工具
- 云计算仿真工具中文注释VmSchedulerSpaceShared.java
- 云计算仿真工具CloudSim介绍和使用
- 今天开始学Java 开发一个坐标计算工具