Search code examples
javaswinggraphicsappletjtextfield

Need advice on approaching a little graphics project


So I made a fully functional credit card validator which uses Luhn's Algorithm and all that jazz to validate the card type and number. It currently only uses Scanner and the console to print out stuff, but I wanted to take my program to the next level.

I wanted to make an application with Java graphics that can take in a credit card number entered into my applet/japplet/whatever you suggest and can essentially do the same process as the previously mentioned program, but I want to give it the aesthetic appeal of graphics.

So I'm honestly a little overwhelmed with the graphics in Java (not sure if that's weird), but here's what I want advice on.

  1. How should I approach my graphics project? Should I use JApplet, Applet, JFrame, or something else?

  2. I want to make a text field that the user enters his or her credit card into, what is the method of doing that? I looked up JTextFields but I'm at a loss on how to use it. I looked at the API but it doesn't do a very good job of explaining things in my opinion.

My main problem is the textfield, can someone give me an example of a textfield that can take in data that the user types? Sort of like Scanner in the console but in my graphics application.

Sorry for my word wall, you guys have been very helpful to me in the past :) Tips, tricks, and anything else you think would help me out would be greatly appreciated.


Solution

  • Here is an example of a text field using swing:

    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();
        }
    }