照例前言
今天下午花了两个多钟,按照老师的需求http://blog.csdn.net/dyz1982/article/details/21413679修改上次的作业 也就是这个货http://blog.csdn.net/cp_wl/article/details/21117189
同时,相对于上次修改了一个小BUG,另外还有就是按照需求加入了文件对话框和随机点名,外加对前面的代码做了一下小调整。
然后就是,界面因为懒得调整,所以还是一如既往的丑,不要吐槽
随机点名数为点名人数的三分之一
如果今晚有时间的话 可能会做一下头像版本,也就是,老师的第五个需求
话不多说 上代码 这次可以彻底的抛弃DOS(PS:还是需要DOS启动的)
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.swing.*;
public class RegisterApp extends JFrame implements ActionListener {
static int count2 = 0;//读取到的行数
static JButton but1 = new JButton("到");//两个按钮
static JButton but2 = new JButton("缺席");
static JLabel textfield = new JLabel("嗯哼", JLabel.CENTER);//标签 用于显示名字和号数。
static JTextArea textarea = new JTextArea();//文本域,用于最后的输入
static String namel[][];//字符组 将花名册读入数组
static int count = 0;//人数
static int nAbsent=0;//缺席人数
static String strAbsent;//缺席人名
static String inname,outname;
static int name_partcount;//部分名字数,默认为点到人数的三分之一
static int nameNo [];//被点到的号码
static JFileChooser filechoose;//文件选择框
static JButton but_file = new JButton("选择文件");//按钮打开文件选择框
static File filein ;//文件输入
static JTextField text_outname;//输出文件名
static JPanel panel;//中间面板
//static JPanel imgpanel;//图形面板
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args){
/************************************图型部分*********************************************/
textfield.setText("请选择文件");
RegisterApp1 g = new RegisterApp1();
g.setLayout(new BorderLayout());
filechoose = new JFileChooser();
//imgpanel = new JPanel();
//imgpanel.setSize(200, 300);
panel = new JPanel(new GridLayout(2, 1));
panel.add(text_outname = new JTextField("classname"));
panel.add(textarea);
g.add("North", textfield);
g.add("East", but1);
g.add("West", but2);
g.add("Center",panel);
g.add("South",but_file);
g.setSize(300, 200);
g.setVisible(true);
textarea.setEditable(false);
// TODO Auto-generated method stub
but1.addActionListener(g);
but2.addActionListener(g);
but_file.addActionListener(g);
g.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
/**************************************************************************************/
}
public static void getclassname(){
outname=text_outname.getText();
}
public void onchooserfile() throws FileNotFoundException{
filein=filechoose.getSelectedFile();
Scanner fin = new Scanner(filein);
Scanner fin1 = new Scanner(filein);
while (fin.hasNext()) {//确定人数
count++;
fin.nextLine();
}
//System.out.println(count);
fin.close();
namel = new String[count][2];
for (int a = 0; a < count; a++) {//读入数组
namel[a][0] = fin1.nextLine();
//System.out.println(namel[a][0]);
}
fin1.close();
randomname();//调用随机生成名字的一个方法。
textfield.setText(namel[nameNo[0]][0]);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("到") && count2 < name_partcount) {
// 到时
selectis();
} else if (e.getActionCommand().equals("缺席") && count2 < name_partcount) {
// 缺席时
selectpass();
} else if (count2 == name_partcount && !e.getActionCommand().equals("选择文件")) {
// 点完时
over();
} else if (e.getActionCommand().equals("选择文件")) {
//点击选择文件时
int result = filechoose.showOpenDialog(filechoose);
if(result==0){
try {
onchooserfile();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
textarea.setText("请输入文件");
}
}
}
public static String redate() {//返回日期
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHMM");// 可以方便地修改日期格式
String strDate = dateFormat.format(now);
// System.out.println("当前时间:"+strDate);
return strDate;
}
public static void randomname(){//随机生成点名人名
name_partcount=count/3;//三分之一人数,用于确认需要随机生成的人数
nameNo = new int [name_partcount];
System.out.println(name_partcount);
for(int a = 0;a<name_partcount;a++){//随即生成人名
int randomNo=(int)(Math.random()*count);
for(int i=0;i<name_partcount;i++){//验证是否有重复部分
if(randomNo==nameNo[i]){//有重复部分时重新生成,一直到不重复
i=0;
randomNo=(int)(Math.random()*count);
}
}
nameNo[a]=randomNo;
}
}
public static void selectis(){//到时
/*if (namel[nameNo[count2]][0].equals("5 陈鹏")) {// 屏蔽自己部分
textfield.setText(namel[nameNo[count2]][0]);
namel[nameNo[count2]][1] = "1";
count2++;
} else {*/
namel[nameNo[count2]][1] = "1";
count2++;
if(count2<name_partcount){
textfield.setText(namel[nameNo[count2]][0]);
}
//}
}
public static void selectpass(){//缺课时
namel[nameNo[count2]][1] = "0";
strAbsent = strAbsent + namel[nameNo[count2]][0] + " " + "\r\n";
count2++;
nAbsent++;
if(count2<name_partcount){
textfield.setText(namel[nameNo[count2]][0]);
}
}
public static void over(){//点完时
getclassname();
File fileout = new File(outname + redate() + ".txt");
try {
@SuppressWarnings("resource")
PrintWriter fout = new PrintWriter(fileout);
for (int a = 0; a < name_partcount; a++) {
String s = namel[nameNo[a]][0] + " "
+ namel[nameNo[a]][1];
fout.println(s);
}
fout.close();
textarea.setText("考勤结束." + "\r\n" + "一共点名" + name_partcount
+ "人" + "\r\n" + "一共有" + nAbsent + "个同学缺课" + "\r\n"
+ "分别是" + strAbsent + "\r\n" + "点击右上角关闭");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}