java - using a predefined String in a GUI -
i have gui set way want i'm trying output string called answerkey have predefined in dashreplace() tried draw string , use jlabel can't seem find right method of doing such.
import java.awt.*; 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 { 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 static 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"); //button.addactionlistener((actionlistener) this); jlabel tflable = new jlabel("please enter letter:"); //trying out put predefined string jlabel answerkey = new jlabel(answerkey); jtextfield text = new jtextfield(10); //string input = text.gettext(); heading.setsize(200, 50); tflable.setsize(150, 50); text.setsize(50, 30); button.setsize(60, 20); heading.setlocation(300, 10); tflable.setlocation(50, 40); text.setlocation(50, 80); button.setlocation(100, 85); this.add(heading); this.add(tflable); this.add(text); this.add(button); } }
the method called answerkey, not answerkey. in fact, surely won't compile you've written - you're trying assign variable while using variable constructor parameter. should be:
jlabel answerkey = new jlabel(dashreplace(answerkey()).tostring()); to make work, however, you'll need make method non-static. also, having variable named answerkey , method named answerkey crying out confusion - i'd suggest better name jlabel.
Comments
Post a Comment