前言
老師的作業需求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
下面開始上代碼,總共有兩個公共類,請確保在同一個包下
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();
}
}
}
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);
}
}
下面是一個批量改名
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
}
}
感謝百度和幾位前輩的幫助