第三周实验--简单的签到程序

下面这个程序再由一个高手改正辅导后的,和参考各位同学的,终于搞定了,可以过滤自己了,但是革命尚未成功,仍需努力,可以把它弄得更好。加油!代码如下:

/**
 * 程序功能:简单的签到程序图形界面化,并且能过滤自己而不被点名,能够保存签到后的结果
 * 作者:陈思颖
 * 时间:2014.03.20
 * QQ:*********联系博客
 */


import java.awt.BorderLayout;
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 java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import javax.swing.*;


public class RegisterDemo {
static int count2 = 0;
static int count = 0;
static String namel[][];
static JLabel nameText =new JLabel();
static String inname,outname;  
static int aAbsent=0;
static JLabel outlab =new JLabel();
private static Object strName;


public static void main(String[] args) throws FileNotFoundException {  


//(a)运用图形界面知识,做出简单界面
//以下是这些是创建标签
JFrame frame = new JFrame("课堂签到程序");
JLabel nameLab =new JLabel("请输入名字:");
JButton yes =new JButton("到");
JButton no =new JButton("缺席");
JButton end =new JButton("统计");
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
       //将对象加到Applet程序窗口画面内
pan1.add(nameLab);
pan1.add(nameText);
pan2.add(yes);
pan2.add(no);
pan2.add(end);
frame.setLayout(new BorderLayout(3,3));//使用BorderLayout布局
frame.add(pan1,BorderLayout.NORTH);
frame.add(pan2,BorderLayout.CENTER);
frame.add(outlab,BorderLayout.SOUTH);
frame.pack();
frame.setSize(290,150);
frame.setVisible(true);




/*(b)根据老师给出的签到程序RegisterApp.java,加以利用,使用命令行参数加载名单到nameText的标签上,
    利用数组 存储学生学号,名字和输入流 Scanner类等知识   */
if (args.length != 2) {  
System.out.println("参数输入不对");  
System.out.println("使用方法(示例):java RegisterApp 名单文件名称  班级名称");  
System.exit(0);  
}  
        //数组的定义
inname = args[0];  
outname = args[1];  
File filein = new File(inname);  
Scanner fin = new Scanner(filein);  
Scanner fin1 = new Scanner(filein);  


while (fin.hasNext()) {
count++;  
fin.nextLine();  
}  
fin.close();  
namel = new String[count][2];  
for (int a = 0; a < count; a++) { 
namel[a][0] = fin1.nextLine();  
}  
fin1.close();  


nameText.setText(namel[count2][0]);  


      
//可以方便地修改日期格式
Date now = new Date(aAbsent); 
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHMM");
String strDate = dateFormat.format( now ); 
System.out.println("当前时间:"+strDate); 


//(d)读取学生名单,args[0]为学生名单文件,args[1]为班级名称
String fileList = args[0];
String fileCheck = args[1] + strDate + ".txt";

File fileInput = new File(fileList);
File fileOutput = new File(fileCheck);


//(e)利用Scanner类读取文本数据/键盘输入数据;  PrintWriter类把签到结果写入到文件
Scanner input = new Scanner(fileInput);
Scanner sc = new Scanner(System.in);
PrintWriter output = new PrintWriter(fileOutput);

//保存缺课学生名字strAbsent ,缺课学生人数nAbsent, 是否缺课标记flag
String strAbsent = "";
int nAbsent = 0;
int flag = 0;



//(c)通过按钮事件,读取名单,老师可以开始点名,最终统计把结果记录以文档输出
yes.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("到") && count2 < count) 
{  
nameText.setText(namel[count2][0]);  
namel[count2][1] = "1";  
count2++;  
}

}
});

no.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("缺席") && count2 < count) 
{  
//过滤自己,不被点名开始部分
if(namel[count2][0].equals("6陈思颖")){  //判断如果当前读取的名字需要过滤
namel[count2][1] = "1"; //过滤为“到”
count2++; //继续下一步
}else{ 
//过滤自己,不被点名结束部分
nameText.setText(namel[count2][0]);  
namel[count2][1] = "0";  
count2++;  
aAbsent++; 
}
}
}//过滤自己,不被点名 闭合上面的符号
});

end.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {



if (e.getActionCommand().equals("统计") && count2 == count) {  


//System.out.println(000000);
File fileout = new File(outname+"--"+00+ ".txt");  
try {  
PrintWriter fout = new PrintWriter(fileout);  


for (int a = 0; a < count; a++) 
{  
String s = namel[a][0] + "    " + namel[a][1];  
fout.println(s);  
} 

fout.close();
outlab.setText("考勤结束:"+"该班共有"+count+"人"+"一共有"+aAbsent+"个同学缺席"); 



}  

catch (IOException e3) {  
// TODO Auto-generated catch block  
e3.printStackTrace(); 

}  
}
}
});
}
}


结果:


上面这个程序再由一个高手改正辅导后的,太感谢他了,

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值