The Code statistics tool is not very functional. You are welcome to give more comments.
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. JFileChooser;
Import javax. swing. JFrame;
Import javax. swing. JMenu;
Import javax. swing. JMenuBar;
Import javax. swing. JMenuItem;
Import javax. swing. JScrollPane;
Import javax. swing. JTextArea;
Import javax. swing. UIManager;
Public class CodeCounterFrame extends JFrame
{
JMenuBar menuBar = new JMenuBar ();
JMenu fileMenu = new JMenu ("file ");
JMenuItem openMenuItem = new JMenuItem ("open ");
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); // contents in txa cannot be edited.
Add (jsp );
// Internal anonymous listener
OpenMenuItem. addActionListener (new ActionListener ()
{
Public void actionreceivmed (ActionEvent e)
{
If (e. getSource () = openMenuItem)
{
// Click Open menu
// Pop-up file Selector
JFileChooser chooser = new JFileChooser ();
Chooser. showOpenDialog (null );
// Obtain the selected file
File selectedFile = chooser. getSelectedFile ();
BufferedReader br = null;
Boolean flag = false;
Try {
FileReader reader = new FileReader (selectedFile); // read the document content
Br = new BufferedReader (reader );
String line = "";
// 2 read the file
While (line = br. readLine ())! = Null)
{
Line = line. trim (); // remove the space before the Annotation
If (line. matches ("^ [] * $ "))
{
// Match empty rows
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 ++;
}
}
// Save the result to output
Output + = ("code lines:" + codeLines + "\ n" + "comment lines:" + commentLines + "\ n"
+ "Number of blank lines:" + balancer lines + "\ n" + "Total number of lines:" + (codeLines + commentLines + balancer lines ));
// Display the statistical result in txa
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 main program
Public static void main (String [] args)
{
Try {
UIManager. setLookAndFeel ("com. sun. java. swing. plaf. windows. WindowsLookAndFeel"); // windows interface style
} Catch (Exception e ){
E. printStackTrace ();
}
CodeCounterFrame frame = new CodeCounterFrame ();
Frame. setTitle ("small tool for code Statistics ");
Frame. setSize (300,200 );
Frame. setVisible (true );
Frame. setLocationRelativeTo (null); // The center of the form is displayed.
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
}
}
Run window:
Output result: