how to create a GUI java for password and ID using array -
application ask user id , password , whether existing or new user. if new user prompted re-enter details confirm id , password , stored in array. once user clicks login button application search through array of ids , password verification. (set array contain maximum of 10 users – given small array can use linear search) application display appropriate message.
my code far
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class log extends jframe { public static void main(string[] args) { log frametabel = new log(); } jlabel title = new jlabel("please type id , password"); jlabel id = new jlabel("id:"); jlabel psword = new jlabel("password:"); jbutton blogin = new jbutton("login"); jpanel panel = new jpanel(); jtextfield txuser = new jtextfield(15); jpasswordfield pass = new jpasswordfield(15); jradiobutton radnew = new jradiobutton("new", true); jradiobutton radexisting = new jradiobutton("existing", false); buttongroup radiogroup1 = new buttongroup(); log() { super("week 9 question 3"); setsize(300,250); setlocation(600,250); panel.setlayout (null); title.setbounds(40,5,300,20); id.setbounds(52,40,150,20); txuser.setbounds(70,40,150,20); psword.setbounds(7,80,150,20); pass.setbounds(70,80,150,20); blogin.setbounds(180,130,80,20); radnew.setbounds(30, 130, 50, 20); radexisting.setbounds(90, 130, 90, 20); panel.add(title); panel.add(id); panel.add(psword); panel.add(blogin); panel.add(txuser); panel.add(pass); add(radnew); add(radexisting); radiogroup1.add(radnew); radiogroup1.add(radexisting); map.put("test", "12345"); getcontentpane().add(panel); setdefaultcloseoperation(jframe.exit_on_close); setvisible(true); actionlogin(); } public void actionlogin() { blogin.addactionlistener(new actionlistener() { public void actionperformed(actionevent ae) { string puname = txuser.gettext(); string ppaswd = new string(pass.getpassword()); joptionpane.showmessagedialog(null, "correct information", "correct", joptionpane.information_message); } else { joptionpane.showmessagedialog(null, "no password/id found on system", "incorrect", joptionpane.information_message); txuser.settext(""); pass.settext(""); txuser.requestfocus(); if (radnew.isselected()) { } joptionpane.showmessagedialog(null, "re-enter password , id confirm", "information saved", joptionpane.information_message); } } } }); } }
use
char[] ppaswd = pass.getpassword();
instead of
string ppaswd = pass.gettext();
because
the method gettext() type jpasswordfield deprecated.
as @madprogrammer says:
the password stored in char array , never convert string.
strings live in jvm life of jvm, making easier nasty people examine string pool , find passwords.
use map
store username/password.
map<string, char[]> map = new hashmap<string, char[]>();
put initial values
map.put("test", "12345".tochararray()); map.put("admin", "admin".tochararray());
here modified code
public void actionlogin() { blogin.addactionlistener(new actionlistener() { public void actionperformed(actionevent ae) { string puname = txuser.gettext(); char[] ppaswd = pass.getpassword(); if (map.keyset().contains(puname) && arrays.equals(map.get(puname), ppaswd)) { joptionpane.showmessagedialog(null, "correct information", "correct", joptionpane.information_message); } else { joptionpane.showmessagedialog(null, "no password/id found on system", "incorrect", joptionpane.information_message); txuser.settext(""); pass.settext(""); txuser.requestfocus(); if (radnew.isselected()) { if (!puname.equals("") && ppaswd.length > 0) { map.put(puname, ppaswd); } joptionpane.showmessagedialog(null, "re-enter password , id confirm", "information saved", joptionpane.information_message); } } } }); }
Comments
Post a Comment