將命令行程序GUI化還是挺蛋疼的
但是一下午出來還是弄出了這個東西
雖然 感覺做得有些難看
但是 勉強說得過去吧
話不多說
沒看過的請參考 http://blog.csdn.net/cp_wl/article/details/20570369
另外 沒有封裝成jar包 還需要一定的命令行支持
“簡單簽到程序”網頁:http://blog.csdn.net/dyz1982/article/details/20311823
import java.awt.BorderLayout;
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 grah 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;
public static void main(String[] args) throws FileNotFoundException {
if (args.length != 2) {
System.out.println("參數輸入不對");
System.out.println("使用方法(示例):java RegisterApp 名單文件名稱 班級名稱");
System.exit(0);
}
inname=args[0];
outname=args[1];
File filein = new File(inname);
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();
/************************************圖型部分*********************************************/
textfield.setText(namel[count2][0]);
grah g = new grah();
g.setLayout(new BorderLayout());
g.add("North", textfield);
g.add("East", but1);
g.add("West", but2);
g.add("Center",textarea);
g.setSize(300, 100);
g.setVisible(true);
textarea.setEditable(false);
// TODO Auto-generated method stub
but1.addActionListener(g);
but2.addActionListener(g);
g.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
/**************************************************************************************/
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("到") && count2 < count) {
//到時
textfield.setText(namel[count2][0]);
namel[count2][1] = "1";
count2++;
} else if (e.getActionCommand().equals("缺席") && count2 < count) {
//缺席時
textfield.setText(namel[count2][0]);
namel[count2][1] = "0";
strAbsent=strAbsent+namel[count2][0]+" "+"\r\n";
count2++;
nAbsent++;
} else if (count2 == count) {
//點完時
File fileout = new File(outname+ redate() + ".txt");
try {
@SuppressWarnings("resource")
PrintWriter fout = new PrintWriter(fileout);
for (int a = 0; a < count; a++) {
String s = namel[a][0] + " " + namel[a][1];
fout.println(s);
}
fout.close();
textarea.setText("考勤結束."+"\r\n"+"一共有"+nAbsent+"個同學缺課"+"\r\n"+"分別是"+strAbsent+"\r\n"+"點擊右上角關閉");
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
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;
}
}
運行中:
運行完成