package UI;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Jiemian extends JFrame implements ActionListener{
JLabel wlh=new JLabel("物理行");
JLabel dmh=new JLabel("代码行");
JLabel zsh=new JLabel("注释行");
JLabel kh=new JLabel("空行");
JLabel lj=new JLabel("文件路径");
JTextField text=new JTextField(10);
JButton bs=new JButton("browser");
JButton dqtj=new JButton("代码读取并统计");
JButton tc=new JButton("退出");
JButton gy=new JButton("关于");
public Jiemian(){
this.setLayout(new BorderLayout());//添加监听事件
bs.addActionListener(this);
tc.addActionListener(this);
dqtj.addActionListener(this);
gy.addActionListener(this);
this.setTitle("代码统计器");
this.setVisible(true);
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setLayout(new GridLayout(3,1));//总布局
JPanel pane1=new JPanel();//分布局1
pane1.setLayout(new GridLayout(4,1));//总布局
pane1.add(wlh);
pane1.add(dmh);
pane1.add(zsh);
pane1.add(kh);
this.add(pane1);//,BorderLayout.NORTH
JPanel pane2=new JPanel();//分布局2
//pane2.setLayout(new GridLayout(1,3));总布局
pane2.add(lj);
pane2.add(text);
pane2.add(bs);
this.add(pane2);//,BorderLayout.CENTER
JPanel pane3=new JPanel();//分布局3
pane3.setLayout(new GridLayout(3,1));//总布局
pane3.add(dqtj);
pane3.add(tc);
pane3.add(gy);
this.add(pane3);//,BorderLayout.SOUTH
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Jiemian jm=new Jiemian();
}
public void actionPerformed(ActionEvent e){// 监听事件
String soruceName=e.getActionCommand();
if(soruceName.equals("browser")){// 监听事件
FileDialog fd=new FileDialog(new JFrame());//弹出显示窗口
fd.setVisible(true);
text.setText(fd.getDirectory()+fd.getFile());// 路径地址+文件名
}else if(soruceName.equals("退出")){// 监听事件
System.exit(0);//退出
}else if(soruceName.equals("代码读取并统计")){// 监听事件
Yewu oper=new Yewu();//业务操作类
String fileName=text.getText();//得到路径
try{
Shiti st=oper.getCount(fileName);
setValue(st);
System.out.println("总数行="+st.getWlhLine());
System.out.println("空行"+st.getKhLine());
System.out.println("代码行"+st.getDmhLine());
System.out.println("注释行"+st.getZshLine());
}catch(Exception el){
el.printStackTrace();
}
}else if(soruceName.equals("关于")){// 监听事件
Dialog dia=new Dialog(new Frame());
JPanel aboutNorthpipe=new JPanel();
JPanel aboutSouthpipe=new JPanel();
Label lab1=new Label();
Label lab2=new Label();
Label lab3=new Label();
Label lab4=new Label();
Label lab5=new Label();
dia.setTitle("软件介绍");
dia.setLayout(new BorderLayout());
dia.add(aboutNorthpipe,"North");
dia.add(aboutSouthpipe,"South");
aboutNorthpipe.setLayout(new GridLayout(8,3));
aboutNorthpipe.add(lab1);
aboutNorthpipe.add(lab2);
aboutNorthpipe.add(lab3);
aboutNorthpipe.add(lab4);
aboutNorthpipe.add(lab5);
lab1.setText("编写者:xx");
lab2.setText("指导老师:xx");
lab3.setText("介 绍: 本软件功能是源程序代码统计器");
lab4.setText(" 并将统计结果显示出来");
dia.setSize(270,200);
dia.setVisible(true);
dia.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
}
});
}
}
public void setValue(Shiti st){
wlh.setText("物理行"+" "+st.getWlhLine()+" 100%");
long dmhValue=(long)(((double)st.getDmhLine()/(double)st.getWlhLine())*100);
dmh.setText("代码行"+" "+st.getDmhLine()+" "+dmhValue+"%");
long zshValue=(long)(((double)st.getZshLine()/(double)st.getWlhLine())*100);
zsh.setText("注释行"+" "+st.getZshLine()+" "+zshValue+"%");
long khValue=(long)(((double)st.getKhLine()/(double)st.getWlhLine())*100);
kh.setText("空行"+" "+st.getKhLine()+" "+khValue+"%");
}
}
用java语言编写的一个程序实现源代码的代码统计功能
需积分: 10 137 浏览量
2015-04-27
16:03:47
上传
评论
收藏 3KB RAR 举报
qq_27752243
- 粉丝: 0
- 资源: 1