将我的程序转换为JFrame

时间:2013-11-03 16:05:04

标签: java jframe textarea

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。我知道我应该开始使用更多方法,而不是将它全部放在main方法中。

1 个答案:

答案 0 :(得分: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);
}
}
祝你好运!