swing - Java GUI to accept only certain names -
i'm working on gui enter name , tells whether or not has been accepted. if names "john" or "jane" entered "verified" message or "unverified" message if type other name. here's have far, unsure how add if statement detecting names. thanks.
nameprompt.java
import java.awt.borderlayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; public class nameprompt extends jframe{ private static final long serialversionuid = 1l; string name; public nameprompt(){ setlayout(new borderlayout()); jlabel enteryourname = new jlabel("enter name here:"); jtextfield textboxtoentername = new jtextfield(21); jpanel paneltop = new jpanel(); paneltop.add(enteryourname); paneltop.add(textboxtoentername); jbutton submit = new jbutton("submit"); submit.addactionlistener(new submitbutton(textboxtoentername)); jpanel panelbottom = new jpanel(); panelbottom.add(submit); //add paneltop jframe add(paneltop, borderlayout.north); add(panelbottom, borderlayout.south); //jframe set-up settitle("name prompt program"); setdefaultcloseoperation(exit_on_close); pack(); setlocationrelativeto(null); } public static void main(string[] args) { nameprompt promptforname = new nameprompt(); promptforname.setvisible(true); } }
submitbutton.java
import java.awt.component; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jtextfield; public class submitbutton implements actionlistener { jtextfield nameinput; public submitbutton(jtextfield textfield){ nameinput = textfield; } @override public void actionperformed(actionevent submitclicked) { component frame = new jframe(); joptionpane.showmessagedialog(frame , "you've submitted name " + nameinput.gettext() + " allowed."); } }
the actionperformed
method called after clicking submit button.
public void actionperformed(actionevent submitclicked) { component frame = new jframe(); joptionpane.showmessagedialog(frame , "you've submitted name " + nameinput.gettext() + " allowed."); // can store value of whatever user enters. string inputname = nameinput.gettext(); // , add if statements: if(inputname.equals("john") { joptionpane.showmessagedialog(frame, "verified"); } }
alternatively can create list of strings contains names accepted. example:
list<string> acceptednames = arrays.aslist(new string[]{"john", "jane"}); // , check if acceptednames.contains(inputname) { // verified. }
Comments
Post a Comment