使用JTextfFields,我需要重置文本字段以允许多个用户输入

时间:2014-07-31 20:49:00

标签: java swing jframe

我使用JTextFields作为我的信用卡计划。到目前为止,用户可以将信用卡品牌输入到文本字段中,然后我将文本字段中的文本设置为"",这应该是空白的。我希望用户能够将他们的信用卡数字输入到同一文本字段中。但是当我这样做并通过我的validateCard()方法运行它时,它似乎只是采取了""我将文本字段设置为之前。

每次希望用户输入新信息时,如何重置JTextField?该程序将文本字段视为始终具有文本。我怎么能像打印出来的控制台中的Scanner那样对待它?就像每次使用扫描仪一样,它会提示您将新文本输入空行?

谢谢!

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.util.Scanner;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Properties;
import javax.mail.Header;
import java.util.Enumeration;
import java.util.Properties;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JApplet;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
import java.beans.*;
import java.util.ArrayList;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
import java.util.Scanner;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import javax.swing.Timer;

public class CreditGraphics {

   public String cardNum;
   public JFrame frame;
   public JPanel panel;
   public JLabel label;
   public JTextField text;
   public String cardType;
   public String carddigits;
   public boolean cardValid;
   public int length;

   public CreditGraphics() {



      frame = new JFrame("HI");
      panel = new JPanel();
      label = new JLabel();

      text = new JTextField(16);

      panel.add(label);
      panel.add(text);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      panel.setPreferredSize(new Dimension(500, 500));
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true); 


      label.setText("<html>Please enter your credit card <br> 'Master Card' 'Visa' or 'American Express'</html>");


      text.addActionListener(
            new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                  if(text.getText().equals("Visa")){
                     cardType = text.getText();
                     text.setText("";
                     waits(3);
                     label.setText("Thank you, please enter your Visa #");

                  }

                  if(text.getText().equals("Master Card")){
                     cardType = text.getText();
                     text.setText("");
                     waits(3);
                     label.setText("Thank you, please enter your MC #");

                  }


                  if(text.getText().equals("American Express")){
                     cardType = text.getText();
                     text.setText("");
                     waits(3);
                     label.setText("Thank you, please enter your AE #");
                  } 
                  waits(4);




                  carddigits = text.getText();
                  System.out.println(carddigits);
                  length = carddigits.length();
                  validateCard();



               }

            });




   }



   public static void main(String[] args) {
      SwingUtilities.invokeLater(
            new Runnable() {
               public void run() {


                  new CreditGraphics();

               }




            });
   }

   public void checkCard(){


   }


   public static void waits (int k){
      long time0, time1;
      time0 = System.currentTimeMillis();
      do{
         time1 = System.currentTimeMillis();
      }
      while ((time1 - time0) < k * 1000);
   }


   public void validateCard(){
   //check brand
      cardValid = false;
      if((cardType.equals("Visa") && carddigits.substring(0,1).equals("4")) && (length == 13 || length == 16)){
         label.setText("Thank you, next step");
         cardValid = true;
      }
      if((cardType.equals("Master Card")) && (carddigits.substring(0,2).equals("51") || carddigits.substring(0,2).equals("52") || carddigits.substring(0,2).equals("53") || carddigits.substring(0,2).equals("54") || carddigits.substring(0,2).equals("55")) && (length == 16)){
         label.setText("Thank you, next step");
         cardValid = true;
      }
      if((cardType.equals("American Express") && carddigits.substring(0,2).equals("37") && length == 15)){
         label.setText("Thank you, next step");
         cardValid = true;
      }

      if(cardValid != true){

         label.setText("ERROR");
         waits(2);
         System.exit(0);
      }

   //end check   
   }
}

0 个答案:

没有答案