虽然只做出了文件选择框和实习随机点名!但花费还是挺多时间的!
实现文件选择框的 FileDialog 类比较难实现,我花了很多时间研究却还是用不上,最后改用 JFileChooser类 却很简单实现了。
(这个看似简单的文件选择框坑了我很多时间- -!)
随机抽查的话,我直接看了丁老师的方法后 理解上加以利用!
缺点:界面总体上看也不美观!!!
/**
* 程序功能:签到程序GUI增加功能:能够使用文件选择框,和随机抽查人数
* 作者:李家华
* 时间:2014.03.20
* QQ:*********联系博客
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Toolkit;
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.Arrays;
import java.util.Iterator;
import java.util.Scanner;
import javax.swing.*;
public class Registre1 {
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();
static JButton open = new JButton("选择文件");
static File filein;
static JFileChooser filechoose;
static FileDialog fd;
static JTextField rs= new JTextField(3);
static JButton act = new JButton("开始");
static String nu; //获取文本框数值
static int numCheck = 0; //随机抽查学生人数
static int[] randArray; //随机抽查学生序号
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();
//fd=new FileDialog(new Frame(),"测试",FileDialog.LOAD);
filechoose = new JFileChooser();
pan1.add(nameLab);
pan1.add(nameText);
pan1.add(rs);
//pan1.add(act);
pan2.add(open);
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(500,200);
frame.setVisible(true);
nameText.setFont(new Font("", Font.BOLD, 15));
outlab.setFont(new Font("", Font.BOLD, 20));
nameLab.setFont(new Font("", Font.BOLD, 20));
yes.setFont(new Font("", Font.BOLD, 20));
no.setFont(new Font("", Font.BOLD, 20));
end.setFont(new Font("", Font.BOLD, 20));
open.setFont(new Font("", Font.BOLD, 20));
nameText.setText("请输入人数和选择文件");
outlab.setText("欢迎使用课堂签到程序 = =!");
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("选择文件")) {
nu=rs.getText();
int result = filechoose.showOpenDialog(filechoose);
if(result==0){
try {
numCheck =Integer.parseInt(nu);
filein=filechoose.getSelectedFile();
Scanner fin = new Scanner(filein);
Scanner fin1 = new Scanner(filein);
while (fin.hasNext()) {
String str = fin.nextLine();
if(!str.isEmpty()){
count++;
}
}
fin.close();
namel = new String[count][2];
for (int a = 0; a < count; a++) {
namel[a][0] = fin1.nextLine();
}
//根据学生人数,产生相应的随机数组
randArray = createRandom(0,count,numCheck);
fin1.close();
nameText.setText(namel[randArray[0]][0]);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else {
outlab.setText("请输入文件");
}
}
}
});
//(c)通过按钮事件,读取名单,老师可以开始点名,最终统计把结果记录以文档输出
yes.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("到") && count2 < numCheck)
{
namel[randArray[count2]][1] = "1";
count2++;
if(count2<numCheck){
nameText.setText(namel[randArray[count2]][0]);
}else{
nameText.setText("签到结束---");
}
}
}
});
no.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("缺席") && count2 < numCheck)
{
namel[randArray[count2]][1] = "0";
count2++;
aAbsent++;
if(count2<numCheck){
nameText.setText(namel[randArray[count2]][0]);
}else{
nameText.setText("签到结束---");
}
}
}
});
end.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("统计") && count2 == numCheck) {
//System.out.println(000000);
File fileout = new File(outname+"--"+00+ ".txt");
try {
PrintWriter fout = new PrintWriter(fileout);
for (int a = 0; a < numCheck; a++)
{
String s = namel[randArray[a]][0] + " " + namel[randArray[a]][1];
fout.println(s);
}
fout.close();
outlab.setText("考勤结束:"+"抽查人数有"+numCheck+"人"+"一共有"+aAbsent+"个同学缺席");
}
catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
}
}
});
}
//查看丁老师的实习随机抽查人数的方法
public static int[] createRandom(int a, int b, int num){
//用集合HashSet来存储随机数,利用集合Set的特性——无重复元素
java.util.HashSet<Integer> set1 = new java.util.HashSet<Integer>();
int[] randArray = new int[num];
java.util.Random r=new java.util.Random();
while(set1.size()<num){
set1.add(r.nextInt(b-a+1)+a); //生成[a,b]之间均匀分布的随机数
}
//使用Iterator遍历Set中的所有元素,赋值到randArray数组中
Iterator<Integer> it2 = set1.iterator();
int i=0;
while(it2.hasNext()){
randArray[i++] = (Integer)it2.next();
}
Arrays.sort(randArray); //对数组元素进行排序
return randArray;
}
}