java - Wait for radio change -
okay trying wait user select different selection , hit continue button before program reads his/her answer.
currently program doesn't wait for user change answer , hit "choose" button.
it doesnt change option text yet different road names
currently have this:
import java.awt.event.actionlistener; import java.awt.event.containerevent; import java.awt.event.containerlistener; import java.io.filewriter; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.util.scanner; import java.util.logging.level; import java.util.logging.logger; public class gamescreen extends javax.swing.jframe implements actionlistener, containerlistener { static string classstring; static int classint = 0; // 1- wiz 2-warr 3-arch 4-lock static int option; public static void classstore() throws ioexception { filewriter classchose = new filewriter("classchose.txt"); system.out.print(classint); system.out.print(classstring); classchose.write(classstring); classchose.close(); start.setvisible(true); } public gamescreen() { initcomponents(); } @suppresswarnings("unchecked") private void initcomponents() { buttongroup1 = new javax.swing.buttongroup(); jpopupmenu1 = new javax.swing.jpopupmenu(); jscrollpane1 = new javax.swing.jscrollpane(); info = new javax.swing.jtextarea(); opt1 = new javax.swing.jradiobutton(); opt2 = new javax.swing.jradiobutton(); opt3 = new javax.swing.jradiobutton(); opt4 = new javax.swing.jradiobutton(); optiononetext = new javax.swing.jtextfield(); optiontwotext = new javax.swing.jtextfield(); optionthreetext = new javax.swing.jtextfield(); optionfourtext = new javax.swing.jtextfield(); classbutton = new javax.swing.jbutton(); jbutton2 = new javax.swing.jbutton(); optionbutton = new javax.swing.jbutton(); start = new javax.swing.jbutton(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); info.setcolumns(20); info.setrows(5); info.settext("welcome. please choose class."); jscrollpane1.setviewportview(info); buttongroup1.add(opt1); opt1.settext("option one"); buttongroup1.add(opt2); opt2.settext("option two"); buttongroup1.add(opt3); opt3.settext("option three"); buttongroup1.add(opt4); opt4.settext("option four"); optiononetext.seteditable(false); optiononetext.setbackground(new java.awt.color(255, 255, 255)); optiononetext.settext("wizard"); optiontwotext.seteditable(false); optiontwotext.setbackground(new java.awt.color(255, 255, 255)); optiontwotext.settext("warrior"); optionthreetext.seteditable(false); optionthreetext.setbackground(new java.awt.color(255, 255, 255)); optionthreetext.settext("archer"); optionfourtext.seteditable(false); optionfourtext.setbackground(new java.awt.color(255, 255, 255)); optionfourtext.settext("warlock"); classbutton.settext("choose"); classbutton.addactionlistener(this); jbutton2.settext("exit"); jbutton2.addactionlistener(this); optionbutton.settext("choose"); optionbutton.setenabled(false); optionbutton.addcontainerlistener(this); optionbutton.addactionlistener(this); start.settext("begin"); start.addactionlistener(this); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jscrollpane1) .addgroup(layout.createsequentialgroup() .addgap(11, 11, 11) .addcomponent(classbutton) .addgap(34, 34, 34) .addcomponent(optionbutton) .addgap(34, 34, 34) .addcomponent(start) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(jbutton2)) .addgroup(layout.createsequentialgroup() .addcomponent(opt3) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(optionthreetext, javax.swing.grouplayout.preferred_size, 302, javax.swing.grouplayout.preferred_size)) .addgroup(layout.createsequentialgroup() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing) .addcomponent(opt4) .addcomponent(opt2) .addcomponent(opt1, javax.swing.grouplayout.alignment.leading)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(optionfourtext) .addcomponent(optiononetext) .addcomponent(optiontwotext, javax.swing.grouplayout.alignment.trailing))) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 115, javax.swing.grouplayout.preferred_size) .addgap(18, 18, 18) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(opt1) .addcomponent(optiononetext, javax.swing.grouplayout.preferred_size, 25, javax.swing.grouplayout.preferred_size)) .addgap(18, 18, 18) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(opt2) .addcomponent(optiontwotext, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addgap(18, 18, 18) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(opt3) .addcomponent(optionthreetext, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addgap(18, 18, 18) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(opt4, javax.swing.grouplayout.preferred_size, 25, javax.swing.grouplayout.preferred_size) .addcomponent(optionfourtext, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 10, short.max_value) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(classbutton) .addcomponent(jbutton2) .addcomponent(optionbutton) .addcomponent(start)) .addcontainergap()) ); pack(); } public void actionperformed(java.awt.event.actionevent evt) { if (evt.getsource() == classbutton) { gamescreen.this.classbuttonactionperformed(evt); } else if (evt.getsource() == jbutton2) { gamescreen.this.jbutton2actionperformed(evt); } else if (evt.getsource() == start) { gamescreen.this.startactionperformed(evt); } else if (evt.getsource() == optionbutton) { gamescreen.this.optionbuttonactionperformed(evt); } } public void componentadded(java.awt.event.containerevent evt) { if (evt.getsource() == optionbutton) { gamescreen.this.optionbuttoncomponentadded(evt); } } private void jbutton2actionperformed(java.awt.event.actionevent evt) { system.exit(0); } private void classbuttonactionperformed(java.awt.event.actionevent evt) { //check option while (classint < 1) { if (opt1.isselected()) { classstring = "wizard"; classint = 1; optionbutton.setvisible(true); classbutton.setvisible(false); } else if (opt2.isselected()) { classstring = "warrior"; classint = 2; optionbutton.setvisible(true); classbutton.setvisible(false); } else if(opt3.isselected()) { classstring = "archer"; classint = 3; optionbutton.setvisible(true); classbutton.setvisible(false); } else if(opt4.isselected()) { classstring = "warlock"; classint = 4; optionbutton.setvisible(true); classbutton.setvisible(false); } else { info.settext("please select class below."); } break; } try { buttongroup1.clearselection(); classstore(); info.settext("you have chosen an: " + classstring); } catch (ioexception ex) { logger.getlogger(gamescreen.class.getname()).log(level.severe, null, ex); } } private void optionbuttoncomponentadded(java.awt.event.containerevent evt) { optionbutton.setvisible(false); } private void startactionperformed(java.awt.event.actionevent evt) { start.setvisible(false); optionbutton.setenabled(true); wizardstory(); } private void optionbuttonactionperformed(java.awt.event.actionevent evt) { //check option if (opt1.isselected()) {option = 1; } else if (opt2.isselected()) option = 2; else if (opt3.isselected()) option = 3; else option = 4; } public static void main(string args[]) { try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(gamescreen.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(gamescreen.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(gamescreen.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(gamescreen.class.getname()).log(java.util.logging.level.severe, null, ex); } java.awt.eventqueue.invokelater(new runnable() { public void run() { new gamescreen().setvisible(true); optionbutton.setvisible(false); start.setvisible(false); } }); } public int getoption() { optionbutton.isselected(); optionbutton.isenabled(); return option; } public void wizardstory() { info.settext("this story begins.\nplease select road wish travel."); if (option == 1) { info.settext("didn't work."); system.out.print("didnt work"); } else if (option == 2) { info.settext("did work."); system.out.print("worked"); } } private javax.swing.buttongroup buttongroup1; private javax.swing.jbutton classbutton; private javax.swing.jtextarea info; private javax.swing.jbutton jbutton2; private javax.swing.jpopupmenu jpopupmenu1; private javax.swing.jscrollpane jscrollpane1; private javax.swing.jradiobutton opt1; private javax.swing.jradiobutton opt2; private javax.swing.jradiobutton opt3; private javax.swing.jradiobutton opt4; public static javax.swing.jbutton optionbutton; private javax.swing.jtextfield optionfourtext; private javax.swing.jtextfield optiononetext; private javax.swing.jtextfield optionthreetext; private javax.swing.jtextfield optiontwotext; private static javax.swing.jbutton start; @override public void componentremoved(containerevent ce) { throw new unsupportedoperationexception("not supported yet."); } }
gui's event driven environments. is, unlike console program where, typically, program moves 1 sequence of instructions another, gui's "wait" kind of event occur , respond events.
in swing, don't need wait, taken care you. need able do, respond appropriate events occur.
take closer @ creating gui swing more details.
the basic idea devise way 1- put options 1 screen , 2- respond actionevent
when occurs , update state of program when does.
Comments
Post a Comment