jeudi 1 janvier 2015

IF statement not working in a simple login programme

Hello I am writing a simple login programme in Java and I am having issues as even when the correct account number and password is entered I still get the 'Please enter the correct Account number/ Password' and I cannot seem to see where the error is



import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main2 extends JFrame{

public static int x=0;

public JPanel panel;
String accountnumber = "123456";
JTextField inputUser;
String password = "admin";
JPasswordField inputPass;

public JLabel lblctr;

public Main2(){
setTitle("Login");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 450, 500, 500);
panel = new JPanel();
setContentPane(panel);
panel.setLayout(null);

inputUser = new JTextField();
inputUser.setBounds(130, 30,125, 20);
panel.add(inputUser);
inputUser.setColumns(10);

inputPass = new JPasswordField();
inputPass.setBounds(130, 60, 125, 20);
panel.add(inputPass);
inputPass.setColumns(10);

lblctr = new JLabel("Attempts:");
lblctr.setBounds(80, 135, 72, 17);
panel.add(lblctr);

JButton cancelbtn = new JButton("Cancel");
cancelbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});

JButton loginbtn = new JButton("Login");

loginbtn.addActionListener(new ActionListener(){
@SuppressWarnings("deprecation") public void actionPerformed(ActionEvent arg0){

if((inputUser.getText().equals(accountnumber)) && (inputPass.getPassword().equals("password"))){
JOptionPane.showMessageDialog(null,"Press OK to Continue!","Login success!",JOptionPane.INFORMATION_MESSAGE);
}
else{
x++;
if(x>3){
JOptionPane.showMessageDialog(null,"Too many attempts","Access Denied!",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
else{
JOptionPane.showMessageDialog(null,"Please input correct Account Number/Password","Login Failed!",JOptionPane.ERROR_MESSAGE);
}
}
lblctr.setText("Attempts : "+x);
}});

cancelbtn.setBounds(170,105, 89, 21);
panel.add(cancelbtn);

loginbtn.setBounds(50,105, 89, 21);
panel.add(loginbtn);

JLabel lbluser = new JLabel("Account Number:");
lbluser.setBounds(12, 34, 100, 10);
panel.add(lbluser);

JLabel lblpass = new JLabel("Password:");
lblpass.setBounds(50, 62, 72, 17);
panel.add(lblpass);
}

public static void main (String[]args){
Main2 proj = new Main2();
proj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
proj.setVisible(true);
proj.setSize(320,190);
proj.setLocationRelativeTo(null);
}
}

Aucun commentaire:

Enregistrer un commentaire