java - What do I do in order to make my Madlibs program accessible from this log in/password program? -
(first post on stack, woopee!) wrote madlibs program takes in user input, , makes story out of it. made user log in program want test using mad libs program. works fine, there 1 problem , i'll go through it. problem starts out log in screen. once use input proper keywords(dodo , foo), log in screen logs in, turns off, , calls madlibsgui program. here's problem: when this, reason madlibsgui program spawns 2 windows. have suspicious problem lies in madlibsgui's main
method . i've tried fixing already, , hasn't seemed work. program works fine, 2 windows bother me. i'll post both code classes below read , at. both simple (i'm beginner programmer), shouldn't have of problem them. if have additional comments or corrections, please don't hesitate correct them.
loginscreen:
package passwordprogram; import java.awt.borderlayout; import java.awt.color; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jpasswordfield; import javax.swing.jtextfield; import javax.swing.uimanager; import madlibs.madlibsgui; public class loginscreen implements actionlistener { public static void main(string[] args) { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (exception e) { } loginscreen logger = new loginscreen(); logger.start(); } jbutton login; jframe frame; jtextfield username; jpasswordfield password; jlabel title; public void start() { frame = new jframe(); jpanel panel = new jpanel(); panel.setbackground(color.red); jbutton login = new jbutton("log in"); login.addactionlistener(this); title = new jlabel("welcome username/password system"); jlabel usernametxt = new jlabel("username: "); username = new jtextfield(15); jlabel passwordtxt = new jlabel("password: "); password = new jpasswordfield(15); frame.getcontentpane().add(borderlayout.center, panel); frame.getcontentpane().add(borderlayout.south, login); frame.getcontentpane().add(borderlayout.north, title); panel.setlayout(new gridbaglayout()); gridbagconstraints left = new gridbagconstraints(); left.anchor = gridbagconstraints.east; gridbagconstraints right = new gridbagconstraints(); right.weightx = (int) 2; right.fill = gridbagconstraints.horizontal; right.gridwidth = gridbagconstraints.remainder; panel.add(usernametxt, left); panel.add(passwordtxt, right); panel.add(username, right); panel.add(passwordtxt, left); panel.add(password, right); login.addactionlistener(this); frame.setvisible(true); frame.setsize(500, 300); } public void actionperformed(actionevent event) { if (username.gettext().equals("dodo") && new string(password.getpassword()).equals("foo")) { madlibsgui mlibs = new madlibsgui(); mlibs.start(); frame.setvisible(false); } else { title.settext("invalid username/password. please try again."); } } }
madlibsgui class:
package madlibs; import java.awt.borderlayout; import java.awt.color; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.borderfactory; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextarea; import javax.swing.jtextfield; import javax.swing.uimanager; public class madlibsgui implements actionlistener { jframe frame; jpanel panel; jtextfield nametxt; jtextfield verbtxt1; jtextfield adjtxt; jtextfield verbtxt2; jtextfield nountxt; jtextarea story; public void start() { jframe frame = new jframe(); jpanel panel = new jpanel(); jbutton madlibbutton = new jbutton("lib it!"); story = new jtextarea(); jlabel title = new jlabel("welcome mad libs! \n put in words , press 'lib it' button play!"); jlabel namelabel = new jlabel("name: "); jlabel verblabel1 = new jlabel("verb: "); jlabel adjlabel = new jlabel("adjective: "); jlabel verblabel2 = new jlabel("verb: "); jlabel nounlabel = new jlabel("noun: "); nametxt = new jtextfield(25); verbtxt1 = new jtextfield(25); adjtxt = new jtextfield(25); verbtxt2 = new jtextfield(25); nountxt = new jtextfield(25); frame.getcontentpane().add(borderlayout.south, story); frame.getcontentpane().add(borderlayout.north, title); panel.setlayout(new gridbaglayout()); panel.setbackground(color.green); frame.getcontentpane().add(panel); gridbagconstraints left = new gridbagconstraints(); left.anchor = gridbagconstraints.east; gridbagconstraints right = new gridbagconstraints(); right.weighty = 1.2; gridbagconstraints middle = new gridbagconstraints(); middle.anchor = gridbagconstraints.center; right.fill = gridbagconstraints.horizontal; right.gridwidth = gridbagconstraints.remainder; panel.add(namelabel, left); panel.add(nametxt, right); panel.add(verblabel1, left); panel.add(verbtxt1, right); panel.add(adjlabel, left); panel.add(adjtxt, right); panel.add(verblabel2, left); panel.add(verbtxt2, right); panel.add(nounlabel, left); panel.add(nountxt, right); panel.add(madlibbutton, right); panel.setborder(borderfactory.createemptyborder(5, 5, 5, 5)); frame.pack(); frame.setvisible(true); frame.setsize(615, 500); madlibbutton.addactionlistener(this); } public void actionperformed(actionevent event) { string text = ("\tthere once boy named " + nametxt.gettext() + " loved " + verbtxt1.gettext() + ". \n\tone day, " + nametxt.gettext() + " walking down street when saw " + adjtxt.gettext() + " bird \n\twas hurt. quietely said, \" it's okay \n\tlittle bird, " + "won't hurt you!\" instead, " + nametxt.gettext() + "\n\tdecided going " + " " + verbtxt2.gettext() + " bird! sadly, bird \n\t" + verbtxt2.gettext() + "ed too" + " much. " + nametxt.gettext() + " sad. \n\the sat in room, playing " + nountxt.gettext() + ". \n\n \t\t\t|the end|"); story.append(text); } public static void main(string[] args) { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (exception e) { e.printstacktrace(); } madlibsgui main = new madlibsgui(); main.start(); } }
you call following twice in method start()
:
login.addactionlistener(this);
which means each time button clicked actionperformed()
method executed twice.
Comments
Post a Comment