java - JOption call method from String text? -
i'm not sure how ask question here is. please @ code (which abbreviated convenience, , don't worry "sam" temporary)
public class jeopardygui_main11 extends javax.swing.jframe { /** * creates new form jeopardygui_main */ public jeopardygui_main11() { initcomponents(); setdefaultcloseoperation( jframe.exit_on_close ); } public void info(){ string[] = new string[25]; string[] sp = new string[25]; string[] mu = new string[25]; string[] spe = new string[25]; string[] tv = new string[25]; string[] mo = new string[25]; string[] ama = new string[25]; string[] spa = new string[25]; string[] mua = new string[25]; string[] spea = new string[25]; string[] tva = new string[25]; string[] moa = new string[25]; am[0] = "sam";ama[0] = "sam"; am[1] = "sam";ama[1] = "sam"; ... am[23] = "sam";ama[23] = "sam"; am[24] = "sam";ama[24] = "sam"; mu[0] = "sam";mua[0] = "sam"; mu[1] = "sam";mua[1] = "sam"; ... mu[23] = "sam";mua[23] = "sam"; mu[24] = "sam";mua[24] = "sam"; spe[0] = "sam";spea[0] = "sam"; spe[1] = "sam";spea[1] = "sam"; ... spe[23] = "sam";spea[23] = "sam"; spe[24] = "sam";spea[24] = "sam"; tv[0] = "sam";tva[0] = "sam"; tv[1] = "sam";tva[1] = "sam"; ... tv[23] = "sam";tva[23] = "sam"; tv[24] = "sam";tva[24] = "sam"; sp[0] = "sam";spa[0] = "sam"; sp[1] = "sam";spa[1] = "sam"; ... sp[23] = "sam";spa[23] = "sam"; sp[24] = "sam";spa[24] = "sam"; mo[0] = "sam";moa[0] = "sam"; mo[1] = "sam";moa[1] = "sam"; ... mo[23] = "sam";moa[23] = "sam"; mo[24] = "sam";moa[24] = "sam"; int random_int = (int) (math.random() * ( 0 - 24 )); string am_qu = am[random_int]; string am_an = ama[random_int]; string sp_qu = sp[random_int]; string sp_an = spa[random_int]; string mu_qu = mu[random_int]; string mu_an = mua[random_int]; string spe_qu = sp[random_int]; string spe_an = spa[random_int]; string tv_qu = tv[random_int]; string tv_an = tva[random_int]; string mo_qu = mo[random_int]; string mo_an = moa[random_int]; } pack(); setlocationrelativeto(null); }// </editor-fold> private void sp1actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: } private void sp2actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: } private void am1actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: object[] options = {"yes, please", "no way!"}; int n = joptionpane.showoptiondialog(null, ""+am_qu, <-------------------------------------area in question "", joptionpane.yes_no_option, joptionpane.plain_message, null, //do not use custom icon options, //the titles of buttons options[0]); //default button title } private void mo1actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: } private void am1mouseclicked(java.awt.event.mouseevent evt) { // todo add handling code here: } /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ 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(jeopardygui_main11.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(jeopardygui_main11.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(jeopardygui_main11.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(jeopardygui_main11.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new jeopardygui_main11().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jbutton am1; private javax.swing.jbutton am2; private javax.swing.jbutton am3; ... private javax.swing.jbutton am4; private javax.swing.jbutton am5; // end of variables declaration }
at "area in question" put location of problem. optiondialog box needs string text, want replace string "public void info()" randomized math.random.
so, optiondialog should read "sam" (in middle text area) if run properly.
i'm sorry if did not make clear enough, ask anything, i'll waiting response, thanks!
here basic example of different way structure program may more efficient:
public class { public static void main(string[] args) throws classnotfoundexception{ getinfo();//set random question , answer object[] options = {"yes, please", "no way!"}; int n = joptionpane.showoptiondialog(null, so.question,//reference question set "", joptionpane.yes_no_option, joptionpane.plain_message, null, //do not use custom icon options, //the titles of buttons options[0]); //default button title joptionpane.showmessagedialog(null, so.answer); } public static void getinfo(){ //will set question , answer int category = new random().nextint(2 - 0);//random number category int choice = new random().nextint(2 - 0);//random selection so.question = questions[category][choice];//get question so.answer = answers[category][choice];//answer located in same position in answers array } /*each category represented seperate array within questions * 2 dimensional array. these declared static fields or 'global fields' * way accessible , initialised once */ private static string[][] questions = new string[][]{ {"question 1, category 1", "question 2 category 1"},//questions[0][*] 1 category {"question 1 category 2", "question 2 category 2"}//questions[1][*] on category }; /*the answers array mirrors other array each answer in 2d array * in same position relevant question in questions array * */ private static string[][] answers = new string[][]{ {"answer 1 category 1", "answer 2 category 1"}, //answers[0][*] questions [0][*] {"answer 1 category 2", "answer 2 category 2"}//answers[1][*] questions [1][*] }; //these each hold question , answer private static string question; private static string answer;
}
i have made lot smaller can feel how it's working. 'global variable' have been suggested @ bottom. should allow question , answer require joptionpane , prevent continual re-initialisation of arrays.
once again, if unsure comment me , i'll see can do. luck!
Comments
Post a Comment