我先来描述一下我的问题:就是现在功能基本差不多了,除了无法统计/*...*/这样的多行注释外,都没有问题。但是当把得到的代码行放到JTable中显示出来的时候,开始什么都没有,只有把对话框拖动一下,才能显示出来,搞了半天不知道怎么整。下面贴出代码和图:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;import javax.swing.*;
import javax.swing.table.DefaultTableModel;public class CountCodes extends JFrame{
private long codeLines = 0;
private long commentLines = 0;
private long blankLines = 0;
private File fileName;
private JLabel pathLabel;
private JTextField pathField;
private JButton openButton;
private JPanel panelNorth, panelSouth; private ArrayList<File> list = new ArrayList<File> ();
private String[] tableHeader = {"File Name", "Code Lines", "Comment Lines", "Blank Lines"};
private Vector<String> vectorHeader = new Vector<String> ();
private Vector<Vector> values = new Vector<Vector> ();
private JTable table;
private DefaultTableModel tableModel;
private JScrollPane scrollPane;
public CountCodes(){
super("Count Codes");
pathLabel = new JLabel("File Path: ");
pathField = new JTextField(100);
pathField.setEditable(false);
openButton = new JButton("Open");
openButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
openFile();
if (fileName == null)
return;
analysisFileOrDir(fileName);
for(File file : list){
analyze(file);
}
}
});
panelNorth = new JPanel();
panelNorth.setLayout(new BorderLayout());
panelNorth.add(pathLabel, BorderLayout.WEST);
panelNorth.add(pathField, BorderLayout.CENTER);
panelNorth.add(openButton, BorderLayout.EAST);
for (int i=0; i<tableHeader.length; i++)
vectorHeader.add(tableHeader[i]);
tableModel = new DefaultTableModel(values, vectorHeader); table = new JTable(tableModel);
scrollPane = new JScrollPane(table);
panelSouth = new JPanel();
panelSouth.setLayout(new BorderLayout());
panelSouth.add(scrollPane, BorderLayout.CENTER);
setLayout(new BorderLayout());
add(panelNorth, BorderLayout.NORTH);
add(panelSouth, BorderLayout.CENTER);
setSize(450, 400);
setVisible(true);
}
public static void main(String args[]){
CountCodes countCodes = new CountCodes();
countCodes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void openFile(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION)
return;
fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
pathField.setText(fileName.toString());
}
private void analysisFileOrDir(File fileName){
if(fileName.isFile()){
list.add(fileName);
return;
}
else{
File[] fileArray = fileName.listFiles();
for(File inArray : fileArray){
if(inArray.isFile() && inArray.getName().matches(".*\\.java$")){
list.add(inArray);
}
else if(inArray.isDirectory()){
analysisFileOrDir(inArray);
}
}
}
}
private void analyze(File fileName){
Vector<String> value = new Vector<String> ();
String[] strArray = fileName.toString().split("\\\\");
value.add(strArray[strArray.length-1]);
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(fileName));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches("^[\\s&&[^\\n]]*$")) {
blankLines++;
} 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 {
codeLines++;
}
}
value.add(String.valueOf(codeLines));
value.add(String.valueOf(commentLines));
value.add(String.valueOf(blankLines));
values.add(value);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;import javax.swing.*;
import javax.swing.table.DefaultTableModel;public class CountCodes extends JFrame{
private long codeLines = 0;
private long commentLines = 0;
private long blankLines = 0;
private File fileName;
private JLabel pathLabel;
private JTextField pathField;
private JButton openButton;
private JPanel panelNorth, panelSouth; private ArrayList<File> list = new ArrayList<File> ();
private String[] tableHeader = {"File Name", "Code Lines", "Comment Lines", "Blank Lines"};
private Vector<String> vectorHeader = new Vector<String> ();
private Vector<Vector> values = new Vector<Vector> ();
private JTable table;
private DefaultTableModel tableModel;
private JScrollPane scrollPane;
public CountCodes(){
super("Count Codes");
pathLabel = new JLabel("File Path: ");
pathField = new JTextField(100);
pathField.setEditable(false);
openButton = new JButton("Open");
openButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
openFile();
if (fileName == null)
return;
analysisFileOrDir(fileName);
for(File file : list){
analyze(file);
}
}
});
panelNorth = new JPanel();
panelNorth.setLayout(new BorderLayout());
panelNorth.add(pathLabel, BorderLayout.WEST);
panelNorth.add(pathField, BorderLayout.CENTER);
panelNorth.add(openButton, BorderLayout.EAST);
for (int i=0; i<tableHeader.length; i++)
vectorHeader.add(tableHeader[i]);
tableModel = new DefaultTableModel(values, vectorHeader); table = new JTable(tableModel);
scrollPane = new JScrollPane(table);
panelSouth = new JPanel();
panelSouth.setLayout(new BorderLayout());
panelSouth.add(scrollPane, BorderLayout.CENTER);
setLayout(new BorderLayout());
add(panelNorth, BorderLayout.NORTH);
add(panelSouth, BorderLayout.CENTER);
setSize(450, 400);
setVisible(true);
}
public static void main(String args[]){
CountCodes countCodes = new CountCodes();
countCodes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void openFile(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION)
return;
fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
pathField.setText(fileName.toString());
}
private void analysisFileOrDir(File fileName){
if(fileName.isFile()){
list.add(fileName);
return;
}
else{
File[] fileArray = fileName.listFiles();
for(File inArray : fileArray){
if(inArray.isFile() && inArray.getName().matches(".*\\.java$")){
list.add(inArray);
}
else if(inArray.isDirectory()){
analysisFileOrDir(inArray);
}
}
}
}
private void analyze(File fileName){
Vector<String> value = new Vector<String> ();
String[] strArray = fileName.toString().split("\\\\");
value.add(strArray[strArray.length-1]);
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(fileName));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches("^[\\s&&[^\\n]]*$")) {
blankLines++;
} 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 {
codeLines++;
}
}
value.add(String.valueOf(codeLines));
value.add(String.valueOf(commentLines));
value.add(String.valueOf(blankLines));
values.add(value);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
解决方案 »
- 求解java中正则表达式
- java socket
- jsp的command按钮的onclick 如何写
- 请问一下synchronized如何实现同步
- 正则表达式问题
- [求助]访问tomcat服务器上的https页面 出现的问题
- [求教:]清华书上的能关闭窗口的代码:(一字不差!),但就是抱错:
- 谁能详细解释一下ISO-8859-1 GB2312 GBK UNICODE UTF编码有什么不同????
- 我要得到一个当前日期的java.sql.date的值,如何最才最简单的方法
- 我的java不能运行了,谁能帮我一把?在线等待,我是初学者,请解答得详细一点,谢谢!
- 如何使用List Interface
- 帮帮看看找找错误
也即不刷新 手动在analyze方法中添加红色一行 have a tryvalues.add(value);
this.table.revalidate();