java - Trouble with static and non-static -
i'm new @ java , following beginners course right now. nice, i'm trying kinds of stuff i'm stuck. piece of code doesn't work. should output input in reverse order (by words).
the code flip works in piece of code wrote without gui , i'm trying work in gui fixed buttons, labels etc. i've copied example internet , trying change in such way it'll work. doesn't seem find variables use in actionperformed , setup in addcomponentstopane. has static , non-static, can't seem clear
any appreciated.
heres code.
package flipit; import java.applet.applet; import java.awt.*; import java.awt.event.*; import java.util.list; import java.util.*; //arraylist; import javax.swing.*; public class flipit extends jframe implements actionlistener { public static void addcomponentstopane(container pane) { pane.setlayout(null); jlabel greetinglabel = new jlabel("enter array "); jtextfield infield = new jtextfield(); jtextfield commentaryfield = new jtextfield(); jtextfield strlen = new jtextfield(); jbutton button = new jbutton("flipit"); pane.add(greetinglabel); pane.add(infield); pane.add(commentaryfield); pane.add(button); insets insets = pane.getinsets(); dimension size = button.getpreferredsize(); greetinglabel.setbounds ( 5 + insets.left, 35 + insets.top,size.width + 40, size.height); infield.setbounds (120 + insets.left, 33 + insets.top,size.width + 200, size.height); //size = commentaryfield.getpreferredsize(); commentaryfield.setbounds(120 + insets.left, 80 + insets.top,size.width + 200, size.height); size = button.getpreferredsize(); button.setbounds ( 5 + insets.left, 80 + insets.top,size.width + 40, size.height); } private static void createandshowgui() { //create , set window. jframe frame = new jframe("reverse input string."); frame.setdefaultcloseoperation(jframe.exit_on_close); //set content pane. addcomponentstopane(frame.getcontentpane()); //size , display window. insets insets = frame.getinsets(); frame.setsize(500 + insets.left + insets.right, 425 + insets.top + insets.bottom); frame.setvisible(true); } public static void main(string[] args) { scanner input = new scanner(system.in); //schedule job event-dispatching thread: //creating , showing application's gui. javax.swing.swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } public void actionperformed(actionevent event) { string inputstr = infield.gettext(); int length = inputstr.length(); if (inputstr.compareto("") == 0 || inputstr == null) { commentaryfield.settext("please enter data, array empty"); } else { // convert string variable arraylist. list<string> list = new arraylist<string>(arrays.aslist(inputstr.split(" "))); // converting arraylist string array string [] inputlist = list.toarray(new string[list.size()]); int = 0 ; string reverseorder = "" ; // starting loop print array in reversed order. (i=inputlist.length-1;i>=0; i--) {reverseorder = reverseorder + " " + inputlist[i] ; } // print result. commentaryfield.settext(reverseorder); strlen.settext("lengte string : "+ length); } } }
thanks, rob.
the primary problem issue scoping. since you're declaring widgets inside addcomponentstopane
method, they're not visible outside of method.
try moving widget declarations outside of addcomponentstopane
method:
jlabel greetinglabel = new jlabel("enter array "); jtextfield infield = new jtextfield(); jtextfield commentaryfield = new jtextfield(); jtextfield strlen = new jtextfield(); jbutton button = new jbutton("flipit"); public static void addcomponentstopane(container pane) { pane.setlayout(null); pane.add(greetinglabel); pane.add(infield); pane.add(commentaryfield); pane.add(button); // etc }
as you've pointed out though (sorry, bad!) static method no longer have access widgets (since they're part of class instance).
the easy way think static vs. non-static if declare static, do not need class instance in order access it. hence in code why can this:
public void run() { createandshowgui(); }
effectively, that's same doing this:
public void run() { flipit.createandshowgui(); }
note haven't created instance of flipit class; don't need to, since createandshowgui
method static
. if however, not static, have create new class instance, follows:
public void createandshowgui() { // thing - note no longer static } public void run() { // create instance flipit flipper = new flipit(); // invoke method against class instance flipper.createandshowgui(); }
so - in order code working, best solution make non-static (except of course main
method, must static).
here's entire code sample put - note may need make createandshowgui
method public
- don't think so. it's been while since coded in java can't certain.
package flipit; import java.applet.applet; import java.awt.*; import java.awt.event.*; import java.util.list; import java.util.*; //arraylist; import javax.swing.*; public class flipit extends jframe implements actionlistener { jlabel greetinglabel = new jlabel("enter array "); jtextfield infield = new jtextfield(); jtextfield commentaryfield = new jtextfield(); jtextfield strlen = new jtextfield(); jbutton button = new jbutton("flipit"); public void addcomponentstopane(container pane) { pane.setlayout(null); pane.add(greetinglabel); pane.add(infield); pane.add(commentaryfield); pane.add(button); insets insets = pane.getinsets(); dimension size = button.getpreferredsize(); greetinglabel.setbounds ( 5 + insets.left, 35 + insets.top,size.width + 40, size.height); infield.setbounds (120 + insets.left, 33 + insets.top,size.width + 200, size.height); //size = commentaryfield.getpreferredsize(); commentaryfield.setbounds(120 + insets.left, 80 + insets.top,size.width + 200, size.height); size = button.getpreferredsize(); button.setbounds ( 5 + insets.left, 80 + insets.top,size.width + 40, size.height); } private void createandshowgui() { //create , set window. jframe frame = new jframe("reverse input string."); frame.setdefaultcloseoperation(jframe.exit_on_close); //set content pane. addcomponentstopane(frame.getcontentpane()); //size , display window. insets insets = frame.getinsets(); frame.setsize(500 + insets.left + insets.right, 425 + insets.top + insets.bottom); frame.setvisible(true); } public void actionperformed(actionevent event) { string inputstr = infield.gettext(); int length = inputstr.length(); if (inputstr.compareto("") == 0 || inputstr == null) { commentaryfield.settext("please enter data, array empty"); } else { // convert string variable arraylist. list<string> list = new arraylist<string>(arrays.aslist(inputstr.split(" "))); // converting arraylist string array string [] inputlist = list.toarray(new string[list.size()]); int = 0 ; string reverseorder = "" ; // starting loop print array in reversed order. (i=inputlist.length-1;i>=0; i--) { reverseorder = reverseorder + " " + inputlist[i] ; } // print result. commentaryfield.settext(reverseorder); strlen.settext("lengte string : "+ length); } } public static void main(string[] args) { scanner input = new scanner(system.in); //schedule job event-dispatching thread: //creating , showing application's gui. javax.swing.swingutilities.invokelater(new runnable() { public void run() { flipit flipper = new flipit(); flipper.createandshowgui(); } }); } } }
hope helps!