[英]How can I fix adding of unneeded buttons and textfield when setting text to a label via setText?
I created a program that runs a calculator class if the login credentials input were correct.如果登录凭据输入正确,我创建了一个运行计算器类的程序。 But, when the login credentials input were wrong unnecessary components, like text field, buttons etc., were added to the panel alongside the label that should display "Wrong Login Credentials"
但是,当登录凭据输入错误时,不必要的组件(如文本字段、按钮等)被添加到面板中,标签旁边应该显示“错误的登录凭据”
The error looks like this:错误如下所示:
What should I do to fix this?我该怎么做才能解决这个问题? Thanks in advance
提前致谢
Below is my source code.下面是我的源代码。
import java.awt.*;
import java.awt.Font;
import javax.swing.*;
import java.awt.event.*;
public class LoginGUI implements ActionListener{
//Variables
JTextField tField1, tField2;
JPasswordField pwField;
JButton btn1, btn2;
JLabel label1, label2, label3, label4;
private ImageIcon bgImage;
private JLabel img;
simpleCal Calculator;
public LoginGUI(){
//Layout
SpringLayout layout = new SpringLayout();
bgImage = new ImageIcon(this.getClass().getResource("/assets/loginBG.png"));
img = new JLabel(bgImage);
JFrame f = new JFrame("Login");
f.setSize(1280,800);
JPanel p = new JPanel();
Container contentPane = f.getContentPane();
p.setLayout(layout);
p.setBounds(432,155,400,450);
p.setBackground(new Color(101,168,223,5));img.setSize(1280,800);
f.add(p);
//Fonts
Font font1 = new Font("calibri",Font.BOLD,20);
Font font2 = new Font("calibri",Font.BOLD,14);
//Label
label1 = new JLabel("Username:");
label1.setFont(font1);
label1.setForeground(new Color(55,55,55));
layout.putConstraint(layout.WEST, label1, 50, layout.WEST, contentPane);
layout.putConstraint(layout.NORTH, label1, 130, layout.NORTH, contentPane);
label2 = new JLabel("Password:");
label2.setFont(font1);
label2.setForeground(new Color(55,55,55));
layout.putConstraint(layout.WEST, label2, 50, layout.WEST, contentPane);
layout.putConstraint(layout.NORTH, label2, 30, layout.SOUTH, label1);
label3 = new JLabel("");
label3.setFont(font1);
label3.setForeground(new Color(55,55,55));
layout.putConstraint(layout.WEST, label3, 105, layout.WEST, contentPane);
layout.putConstraint(layout.NORTH, label3, 40, layout.NORTH, contentPane);
label4 = new JLabel("");
label4.setFont(font1);
label4.setForeground(new Color(55,55,55));
layout.putConstraint(layout.WEST, label4, 105, layout.WEST, contentPane);
layout.putConstraint(layout.NORTH, label4, 20, layout.SOUTH, label3);
//TextField
tField1 = new JTextField("");
tField1.setFont(font2);
tField1.setColumns(15);
tField1.setBackground(new Color(101,168,223));
layout.putConstraint(layout.WEST, tField1, 40, layout.EAST, label1);
layout.putConstraint(layout.NORTH, tField1, 130, layout.NORTH, contentPane);
//PasswordField
pwField = new JPasswordField();
pwField.setFont(font2);
pwField.setColumns(15);
pwField.setBackground(new Color(101,168,223));
layout.putConstraint(layout.WEST, pwField, 43, layout.EAST, label2);
layout.putConstraint(layout.NORTH, pwField, 30, layout.SOUTH, tField1);
//Button
btn1 = new JButton("Log in");
btn1.setFont(font1);
btn1.setSize(100,50);
btn1.addActionListener(this);
p.add(btn1);
layout.putConstraint(layout.WEST, btn1, 80, layout.WEST, contentPane);
layout.putConstraint(layout.NORTH, btn1, 250, layout.NORTH, contentPane);
f.add(img);
p.add(label3);
p.add(label4);
p.add(tField1);
p.add(pwField);
p.add(label1);
p.add(label2);
f.setVisible(true);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
String user, pass;
int i = 0;
int maxAttempt = 3;
if(e.getSource()==btn1){
user = tField1.getText();
pass = pwField.getText();
if(user.equals("vince")&&pass.equals("123")){
Calculator = new simpleCal();
}
else{
while(i<maxAttempt){
label3.setText("Wrong Login Credentials");
i++;
label4.setText("Attempt: " + i);
}
}
}
}
public static void main(String[]args){
new LoginGUI();
}
}
I don't know why you're getting these unnecessary components, but you don't need a while-loop inside your actionPerformed
: the first time you enter the wrong credentials, it'll run the loop three times already.我不知道为什么要获得这些不必要的组件,但是在
actionPerformed
不需要 while 循环:第一次输入错误的凭据时,它已经运行了 3 次循环。
Create these integer fields inside your LoginGUI
class:在
LoginGUI
类中创建这些整数字段:
class LoginGUI {
private static final int MAX_ATTEMPTS = 3;
private int currentAttempt = 0;
}
Every time an user try to login, increase currentAttempt
inside your actionPerformed
and check if it's lesser than MAX_ATTEMPTS
:每次用户尝试登录时,在
actionPerformed
增加currentAttempt
并检查它是否小于MAX_ATTEMPTS
:
class LoginGUI {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1){
// Don't proceed with the login if the maximum attempt has been reached
if (currentAttempt == MAX_ATTEMPTS) return;
final String user = tField1.getText();
final String pass = pwField.getText();
if (user.equals("vince") && pass.equals("123")) {
Calculator = new simpleCal();
} else {
// Increase the current attempt counter
currentAttempt += 1;
// Display it in the JLabels
if (currentAttempt == MAX_ATTEMPTS) {
label3.setText("Your login attempts have been expired");
} else {
label3.setText("Wrong Login Credentials");
}
label4.setText("Attempt: " + currentAttempt);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.