I've been making a basic java applet where it would input a username and a password on the first input then make me guess the username and password on the second input but it would only allow the user to have 3 tries on input then if the user fails to enter the right input it would become grayed out and non-editable I've so far managed to do most of it except the part where I have to limit the user to 3 input tries and the part on how to store the input data. TL;DR I need to know how to limit the number of tries by the user.
Here is the code that I've managed to make
import java.applet.Applet; // import Applet class
import java.awt.*; // (Abstract Windowing Toolkit)
import java.awt.event.*;
import javax.swing.*;
public class SetA extends Applet implements ActionListener
{
int x = 0;
// User Input
Label lblinputuser = new Label("Input Username");
TextField txtuserinput = new TextField(20);
// Password Input
Label lblinputpass = new Label("Input Password");
TextField txtpassinput = new TextField(20);
// User Guess
Label lbluser = new Label("Username");
TextField txtuser = new TextField(20);
// Password Guess
Label lblpass = new Label("Password");
TextField txtpass = new TextField(20);
// Button Confirmation
Button btnOk = new Button("Ok");
Button btnOk2 = new Button("Ok");
public void init()
{
add(lblinputuser);
add(txtuserinput);
add(lblinputpass);
add(txtpassinput);
add(btnOk2);
add(lbluser);
add(txtuser);
add(lblpass);
add(txtpass);
add(btnOk);
txtuserinput.setForeground(Color.RED);
txtuserinput.setBackground(Color.BLACK);
txtpassinput.setForeground(Color.RED);
txtpassinput.setBackground(Color.BLACK);
txtuserinput.setEchoChar('*');
txtpassinput.setEchoChar('*');
btnOk2.addActionListener(this);
btnOk.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnOk2)
{
int confirmOption = JOptionPane.showConfirmDialog(null, "Are you sure?","Message",JOptionPane.YES_NO_OPTION);
if(confirmOption == 1)
{
txtuserinput.setEditable(false);
txtpassinput.setEditable(false);
}
}
if (e.getSource() == btnOk)
{
if(x != 3)
{
if(txtuser.getText() != (txtuserinput.getText()) && txtpass.getText() != (txtpassinput.getText()))
{
x = x++;
JOptionPane.showMessageDialog(null,"Error Password/Username Incorrect","Error",JOptionPane.ERROR_MESSAGE);
}
if(x == 3)
{
JOptionPane.showMessageDialog(null,"You've run out of tries. Program Closing","Error",JOptionPane.ERROR_MESSAGE);
txtuser.setEditable(false);
txtpass.setEditable(false);
txtuser.setEchoChar('*');
txtpass.setEchoChar('*');
}
else
{
JOptionPane.showMessageDialog(null,"You've guessed the right input","Congratulations",JOptionPane.PLAIN_MESSAGE);
}
}
}
}
}