2014-07-22 154 views
-1

所以我做了一个全功能的信用卡验证器,它使用Luhn算法和所有爵士乐来验证卡的类型和编号。目前它只使用Scanner和控制台打印出来,但我想把我的程序提升到一个新的水平。需要关于接近一个小图形项目的建议

我想用Java图形编写一个应用程序,它可以输入到我的小程序/ japplet /您建议的任何信用卡号码,并且基本上可以执行与上述程序相同的过程,但是我想给它图形的美学吸引力。

因此,我真的有点用Java中的图形(不知道是否奇怪)淹没,但这是我想要的建议。

  1. 我该如何处理我的图形项目?我应该使用JApplet,Applet,JFrame还是其他?

  2. 我想让用户输入他或她的信用卡的文本字段,这样做的方法是什么?我查了一下JTextFields,但是我对如何使用它感到不知所措。我看了一下API,但在我看来,它并没有做很好的解释。

我的主要问题是文本框,有人可以给我一个可以接受用户输入数据的文本框的例子吗?有点像扫描仪在控制台中,但在我的图形应用程序。

对不起,我的话墙,你们一直对我很有帮助:) 提示,技巧,和其他任何你认为会帮助我将不胜感激。

+0

发现样品这里[如何使用文本字段(http://docs.oracle.com/javase/tutorial/uiswing/components/ textfield.html) – Braj

+0

你需要学习Swing。 http://docs.oracle.com/javase/tutorial/uiswing/index.html –

+0

一般性建议,避免小程序(在任何味道),他们进行了很多凹陷,这是最好的避免,当开始。阅读[tutorials](http://docs.oracle.com/javase/tutorial/uiswing/),在[particualr](http://docs.oracle.com/javase/tutorial/uiswing/components/text。 html),并留意'DocumentFilter',这将在稍后派上用场。您可能还会发现[this](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html)有帮助 – MadProgrammer

回答

0

下面是一个使用摆动文本字段的例子:

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

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class GUI extends JFrame { // The JFrame is the window 
    JTextField textField; // The textField 

    public GUI() { 
     textField = new JTextField(10); // The user can enter 10 characters into the textField 
     textField.addActionListener(new ActionListener() { // This will listen for actions to be performed on the textField (enter button pressed) 

      @Override 
      public void actionPerformed(ActionEvent e) { // Called when the enter button is pressed 
       // TODO Auto-generated method stub 
       String inputText = textField.getText(); // Get the textField's text 
       textField.setText(""); // Clear the textField 
       System.out.println(inputText); // Print out the text (or you can do something else with it) 
      } 
     }); 

     JPanel panel = new JPanel(); // Make a panel to be displayed 
     panel.add(textField); // Add the textField to the panel 
     this.add(panel); // Add the panel to the JFrame (we extend JFrame) 

     this.setVisible(true); // Visible 
     this.setSize(500, 500); // Size 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); // Exit when the "x" button is pressed 
    } 

    public static void main(String[] args) { 
     GUI gui = new GUI(); 
    } 
}