import java.io.*;
import java.util.Scanner;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class SignIn extends JFrame{
// public static void setFrame(){
// JFrame frame = new JFrame("Test");
// frame.setSize(400, 300);
// frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
// frame.setLayout(new FlowLayout());
// frame.setVisible(true);
//
// JLabel label = new JLabel("this is a label");
// JPanel panel = new JPanel();
//
// JTextArea textArea = new JTextArea(10,10);
// textArea.append("this is an uneditable text area./n balls");
// JTextField textField = new JTextField(9);
// JButton button = new JButton("Submit");
// frame.add(panel);
// panel.add(label);
// panel.add(textArea);
// panel.add(textField);
// panel.add(button);
// frame.add(panel);
// }
public static int getIndex(int[] array, int x){
int index = -1;
for(int i = 0; i < array.length; i++){
if(array[i] == x)
index = i;
}
return index;
}
public static void main(String[] args) throws Exception {
int i;
String w;
Scanner keyboard = new Scanner(System.in);
DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
Date date = new Date();
File file = new File("Attendance/" + (dateFormat.format(date) + "_Attendance.txt"));
File numfile = new File("DataBase/Student Number DataBase.txt");
File namfile = new File("DataBase/Student Name DataBase.txt");
file.getParentFile().mkdirs();
PrintWriter info = new PrintWriter(file);
PrintWriter numdata = new PrintWriter(new FileWriter(numfile,true));
PrintWriter namdata = new PrintWriter(new FileWriter(namfile,true));
Scanner sc = new Scanner(namfile);
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
String[] namedatabase = lines.toArray(new String[150]);
Scanner scc = new Scanner(numfile);
List<Integer> numbers = new ArrayList<Integer>();
while (scc.hasNextInt()) {
numbers.add(scc.nextInt());
}
int[] numberdatabase = new int[150];
for(int p=0, len = numbers.size(); p < len; p++){
numberdatabase[p] = numbers.get(p);
}
setFrame();
while(true){
System.out.println("");
System.out.print("Write your Student ID: ");
int id = keyboard.nextInt();
String idstring = Integer.toString(id);
if(id == 000)break;
while(idstring.length() != 9){
System.out.println("");
System.out.print("Invalid Student ID, retype it: ");
id = keyboard.nextInt();
idstring = Integer.toString(id);
}
while(getIndex(numberdatabase, id) == -1){
System.out.println("");
System.out.print("Is your Student Number " + id + "?(yes or no): ");
keyboard.nextLine();
w = keyboard.nextLine();
if(w.equals("yes")){
for(i = 0; i < numberdatabase.length; i++){
if(numberdatabase[i] == 0) break;
}
numberdatabase[i] = id;
numdata.println(id);
System.out.println("");
System.out.print("Write your FIRST AND LAST name: ");
namedatabase[i] = keyboard.nextLine();
namdata.println(namedatabase[i]);
}
else{
System.out.println("");
System.out.print("Retype your Student ID: ");
id = keyboard.nextInt();
idstring = Integer.toString(id);
while(idstring.length() != 9){
System.out.println("");
System.out.print("Invalid Student ID, retype it: ");
id = keyboard.nextInt();
idstring = Integer.toString(id);
}
}
}
DateFormat timeFormat = new SimpleDateFormat("hh:mm a");
Date time = new Date();
System.out.println("");
System.out.println("You logged in at " + timeFormat.format(time) + " as " + namedatabase[getIndex(numberdatabase, id)] + " : " + id);
info.print("Student ID: " + id);
info.print(" Name: " + namedatabase[getIndex(numberdatabase, id)]);
info.println(" " + timeFormat.format(time));
}
info.close();
numdata.close();
namdata.close();
}
}
好的,所以我正在尝试将我编写的这个程序转换为 JFrame 格式。不幸的是,我从未使用过 JFrame,所以我不知道自己在做什么。我从教程中获得了基础知识,但我似乎无法将 2 和 2 放在一起。有人可以帮我轻松地为这个程序实现 JFrame 吗?谢谢。
PS。我知道我可能应该开始使用更多的方法,而不是把它全部放在主要方法中。