/**
* 程序功能:简单的签到程序图形界面化,能够保存签到后的结果
*/
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();
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();
//添加事件
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) { //当自变量不为2时,就输出以下信息
System.out.println("参数输入不对");
System.out.println("使用方法(示例):java RegisterApp 名单文件名称 班级名称");
System.exit(0); //退出
}
inname = args[0]; //缺席为0
outname = args[1]; //到席为1
File filein = new File(inname); //创建新文件
Scanner fin = new Scanner(filein);
Scanner fin1 = new Scanner(filein);
while (fin.hasNext()) { //用while语句渐进
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]); //设计文本记录数据
//(c)通过按钮事件,读取名单,老师可以开始点名,最终统计把结果记录以文档输出
yes.addActionListener(new ActionListener(){ //yes的添加行为监听
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"; //输入为1
count2++; //自动加1
}
}
});
no.addActionListener(new ActionListener(){ //no的添加行为监听
public void actionPerformed(ActionEvent e) { //执行事件
if (e.getActionCommand().equals("缺席") && count2 < count)
{
nameText.setText(namel[count2][0]); //获取行为命令方法
namel[count2][1] = "0"; //输入为0
count2++; //自动加1
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) { //扑捉 异常
e3.printStackTrace();
}
}
}
});
}
}
127.0.0.1 activate.adobe.com
127.0.0.1 practivate.adobe.com
127.0.0.1 ereg.adobe.com
127.0.0.1 activate.wip3.adobe.com
127.0.0.1 wip3.adobe.com
127.0.0.1 3dns-3.adobe.com
127.0.0.1 3dns-2.adobe.com
127.0.0.1 adobe-dns.adobe.com
127.0.0.1 adobe-dns-2.adobe.com
127.0.0.1 adobe-dns-3.adobe.com
127.0.0.1 ereg.wip3.adobe.com
127.0.0.1 activate-sea.adobe.com
127.0.0.1 wwis-dubc1-vip60.adobe.com
127.0.0.1 activate-sjc0.adobe.com
第三周作业2
最新推荐文章于 2024-09-15 19:46:31 发布