java - calling within ActionListener -


i'm trying plug code gui @ moment can't find way call text in actionlistener out breaking code know there things in findandreplace() need work on work in gui... @ moment i'm trying account actionlistener.

import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; import java.util.arrays; import java.util.collections;  import javax.swing.*;    public class hangmanpanel extends jpanel {     static boolean found;     private static final long serialversionuid = -5793357804828609325l;      public static string answerkey() {         //get random array element         string array[] = new string[10];         array[0] = "hamlet";         array[1] = "mysts of avalon";         array[2] = "the iliad";         array[3] = "tales edger allan poe";         array[4] = "the children of hurin";         array[5] = "the red badge of courage";         array[6] = "of mice , men";         array[7] =  "utopia";          array[8] =  "chariots of gods";         array[9] =  "a brief history of time";          arraylist<string> list = new arraylist<string>(arrays.aslist(array));         collections.shuffle(list);         string s = list.get(0);         return s;     }      public stringbuilder dashreplace(string s) {         //replace non-white space char dashes , creates stringbuilder object         string tw = s.replaceall("\\s", "-");          system.out.print(tw + "\n");           stringbuilder answerkey = new stringbuilder(tw);         return answerkey;     }          public hangmanpanel(){         this.setlayout(null);          jlabel heading = new jlabel("welcome hangman app");         jbutton button = new jbutton("ok");         //get input         button.addactionlistener(new input());           jlabel tflable = new jlabel("please enter letter:");           jlabel answerkey = new jlabel(dashreplace(answerkey()).tostring());          jtextfield text = new jtextfield(10);           heading.setsize(200, 50);         tflable.setsize(150, 50);         text.setsize(50, 30);         button.setsize(60, 20);         answerkey.setsize(200, 100);          heading.setlocation(300, 10);         tflable.setlocation(50, 40);         text.setlocation(50, 80);         button.setlocation(100, 85);         answerkey.setlocation(100,85);          this.add(heading);         this.add(tflable);         this.add(text);         this.add(button);         this.add(answerkey);     }     public class input implements actionlistener{          @override         public void actionperformed(actionevent e) {             // can't access text             sc = text.gettext();             char ch = sc.charat(0);             findandreplace(s, answerkey, input, ch);         }       }      public static int findandreplace(string s, stringbuilder answerkey, string sc,             char ch) {         //find position of user input , replace         int pos = -1;         found = false;         while(true){         pos = s.indexof(sc, pos+1);         if(pos < 0){              break;         }else{             found = true;             answerkey.setcharat(pos, ch);         }          }         system.out.println(answerkey);         return pos;     }  } 

first of not use lowercase class names , uppercase variable names, code hard read.

you can use anonymous inner class add event listener this:

button.addactionlistener(new actionlistener() {     @override     public void actionperformed(actionevent e) {         // can't access text         sc = text.gettext();         char ch = sc.charat(0);         findandreplace(s, answerkey, input, ch);     } }); 

mark text variable final in order access in anonymous class:

final jtextfield text = new jtextfield(10); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -