第三周作业-实现随机点名的签到程序 第五需求,头像版

来源:互联网 发布:模拟通话环境软件 编辑:程序博客网 时间:2024/06/05 19:12
第三周作业-实现随机点名的签到程序 第五需求,头像版
分类: javawork 2014-03-20 20:59 99人阅读 评论(0) 收藏 举报
java源代码gui
前言
老师的作业需求http://blog.csdn.net/dyz1982/article/details/21413679
基于前面的第四需求修改http://blog.csdn.net/cp_wl/article/details/21545589
按照丁老师的第五需求,加入了图片,同时,为了适应这个需求,修改了一下名字列表这个数组,同时也对整个界面做出一些调整。
另外,由于老师所收集的网络13班的头像并不齐全,并为了保护他们的一个隐私,就在网上找了一些动漫头像代替,当然,这个头像男女不分,点击这里下载头像文件。下载后解压文件。
点击这里下载可执行的JAR文件点击这里下载网络131班级花名册
另外,在写程序过程中也写了一段代码用于批量修改文件,会附上。

程序使用方法,打开后点击选择照片文件夹,选择刚才所解压的头像文件夹。PS:如需替换,只需将照片名字改成对应的学号.jPG文件,暂时只支持JPG格式,同时照片请将分辨率修改为200*200,否则会超出面板的大小。

然后点击选择文件选择花名册。
然后输入班级名称作为输出的文件名,也就是在CLASSNAME那个文本框修改。
然后就可以开始点名了,另外,随机点名人数还是总人数的1/3

下面开始上代码,总共有两个公共类,请确保在同一个包下
[java] view plaincopy
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
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.imageio.ImageIO;
import javax.swing.*;

public class RegisterApp2 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 JLabel textarea = new JLabel();// 文本域,用于最后的输入
static String namel[][];// 字符组 将花名册读入数组
static int count = 0;// 人数
static int nAbsent = 0;// 缺席人数
static String strAbsent;// 缺席人名
static String outname;// 输出文件名,也就是班级名称
static int name_partcount;// 部分名字数,默认为点到人数的三分之一
static int nameNo[];// 被点到的号码
static JFileChooser filechoose;// 文件选择框
static JFileChooser imgpathchooser;// 照片文件夹选择框
static JButton but_file = new JButton("选择文件");// 按钮打开文件选择框
static JButton but_img = new JButton("选择照片文件夹");// 按钮打开文件选择框
static File filein;// 文件输入
static JTextField text_outname;// 输出文件名
static JPanel panel;// 中间面板
static Image img;// 图片
static Pa p = new Pa();// 实例化面板
static String imgpath;// 照片文件夹

/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) {

/************************************ 图型部分 *********************************************/
textfield.setText("请选择文件");
RegisterApp2 g = new RegisterApp2();
g.setLayout(new BorderLayout());
filechoose = new JFileChooser();
imgpathchooser = new JFileChooser();
imgpathchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);// 只能选择目录

panel = new JPanel(new GridLayout(5, 1));// 设置
panel.add(but1);
panel.add(but2);
panel.add(but_file);
panel.add(text_outname = new JTextField("classname"));
panel.add(but_img);

g.add("North", textfield);
g.add("East", panel);
g.add("Center", p);
g.add("South", textarea);
g.setSize(345, 250);
g.setVisible(true);
// TODO Auto-generated method stub

but1.addActionListener(g);
but2.addActionListener(g);
but_file.addActionListener(g);
but_img.addActionListener(g);

g.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
/**************************************************************************************/

}

public static String ifexitimg(String x) {
String imgname = null;
File imgf = new File(imgpath + x + ".jpg");
if (imgf.exists()) {
imgname = imgf.getName();
try {
img = ImageIO.read(imgf);
p.repaint();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} else {
imgname = "1";
}

return imgname;
}

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][3];
for (int a = 0; a < count; a++) {// 读入数组
namel[a][0] = fin1.next();
namel[a][1] = fin1.next();

}
fin1.close();

randomname();// 调用随机生成名字的一个方法。
textfield.setText(namel[nameNo[0]][0] + " " + namel[nameNo[0]][1]);
ifexitimg(namel[nameNo[count2]][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("选择文件")
&& !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("请输入文件");
}

} else if (e.getActionCommand().equals("选择照片文件夹")) {

// 选择文件夹
int result = imgpathchooser.showOpenDialog(imgpathchooser);
if (result == 0) {
imgpath = imgpathchooser.getSelectedFile().getPath() + "\\";
}
}

}

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];
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() {// 到时
namel[nameNo[count2]][2] = "1";
count2++;
if (count2 < name_partcount) {
textfield.setText(namel[nameNo[count2]][0] + " "
+ namel[nameNo[count2]][1]);
ifexitimg(namel[nameNo[count2]][0]);

}
// }

}

public static void selectpass() {// 缺课时

namel[nameNo[count2]][2] = "0";
strAbsent = strAbsent + namel[nameNo[count2]][0] + " "
+ namel[nameNo[count2]][1] + " " + "\r\n";
count2++;
nAbsent++;
if (count2 < name_partcount) {
textfield.setText(namel[nameNo[count2]][0] + " "
+ namel[nameNo[count2]][1]);
ifexitimg(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]
+ " " + namel[nameNo[a]][2];
fout.println(s);
}
fout.close();
textarea.setText("考勤结束." + "一共点名" + name_partcount + "人" + "一共有"
+ nAbsent + "个同学缺课" + "分别是" + strAbsent + "点击右上角关闭");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}

}

[java] view plaincopy
import java.awt.Graphics;

import javax.swing.JPanel;


public class Pa extends JPanel
{
Pa()
{
this.setSize(200, 200);
//this.paint(null);
}
public void paint(Graphics g){

g.drawImage(RegisterApp2.img, 0, 0, this);
//super.paint(g);

}

}

下面是一个批量改名
[java] view plaincopy
import java.io.File;

public class ReName {

public static void main(String[] args) {
String path = "f:\\头像文件\\";
File f= new File("f:\\","头像文件");
System.out.println(f.isDirectory());
long a = 201336615101L;
String filename[] =f.list();
for(int i=0;i<filename.length;i++){
File img = new File(path,filename[i]);
System.out.println(filename[i]);
File newname = new File(path,a+".jpg");
img.renameTo(newname);
a++;
System.out.println(i);
}
// TODO Auto-generated method stub

}

}





感谢百度和几位前辈的帮助
更多 0
上一篇:第三周作业-实现随机点名的签到程序 第四需求,非头像版
0 0
原创粉丝点击