0
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。我知道我可能应该开始使用更多的方法,而不是把它全部放在主要方法中。

4

1 回答 1

1

虽然您仍然需要更多地查看教程,但您可以查看以下区域,JOPtionPane 是一种接收用户输入的 GUI 方式。

下面是一些如何使用 JOPtionPane 帮助您入门的示例。

import javax.swing.JOptionPane;

public class SO {
public static void main(String[] args) {

JOptionPane.showMessageDialog(null, "JOptionPane show");
//*********************************************************************************
int response = JOptionPane.showConfirmDialog(null, "Is this helpful");
//do something based of int response....


//*********************************************************************************
String[] choices = {"Java", "C", "C#"};
int doAgain;

do {
    int responses = JOptionPane.showOptionDialog(
null                // center over parent
, "Favorite language"       // message
, "Poll"            // title in titlebar
, JOptionPane.YES_NO_OPTION // Option type
, JOptionPane.PLAIN_MESSAGE // message type
, null              // icon
, choices           // Options
, "None of your business"   // initial value
);

JOptionPane.showMessageDialog(null, "Response = " + responses);

doAgain = JOptionPane.showConfirmDialog(null, "Again?");
} while (doAgain == JOptionPane.YES_OPTION); //Loops if you choose to do again
//********************************************************************************* 
String input = JOptionPane.showInputDialog(null,
        "Please enter new quantity");
JOptionPane.showMessageDialog(null, input);
}
}

祝你好运!

于 2013-11-03T16:51:37.497 回答