第三週作業-實現隨機點名的簽到程序 第四需求,非頭像版

照例前言

今天下午花了兩個多鍾,按照老師的需求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();
		}
		
	}
	
}







最後,感謝度孃的幫助和幾位前輩

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章