[英]Java applet need some guidance
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.我一直在制作一个基本的 java 小程序,它会在第一次输入时输入用户名和密码,然后让我在第二次输入时猜测用户名和密码,但它只允许用户尝试输入 3 次,如果用户未能输入正确的输入,它将变为灰色且不可编辑到目前为止我已经设法完成了大部分工作,除了我必须将用户限制为 3 次输入尝试的部分以及如何存储输入的部分数据。 TL;DR I need to know how to limit the number of tries by the user.
TL;DR 我需要知道如何限制用户的尝试次数。
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);
}
}
}
}
}
This is the needed logic:这是需要的逻辑:
Done.完毕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.