java - 将我的程序转换为 JFrame

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

最佳答案

虽然您仍然需要更多地研究教程,但这里有一个您可以查看的区域,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);
}
}

祝你好运!

关于java - 将我的程序转换为 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19754931/

相关文章:

java - 不同 JFrame 中 LayoutManager 的问题

java - 如何去除 JFrame 边框让图像触及边缘

php - 在文本区域中捕获换行符(换行符、换行符)

php - AJAX/JQUERY 不更新 MySQL 行

java - 我可以在 catch 语句中使用 Scanner

Java数组,打印其他内容

返回 Map 的 Java 流

java - Graphics Context 只会在一个线程中绘制

java - 图片之间切换(JFrame、JButton)

css - div : why there is an extra 1px line? 内的文本区域