Java newbie needing friendly helpful advise with reading in a textfield to make a button appear/change -


firstly, have started coding in java evening apologizes if seem total newb , code atrocious, im novice. want image change colour when number entered text field on separate j frame. car appears blue when loaded , if press 1 car moves , changes color. issues cant seem access text variable manipulate it. appreciated, thank x

public class imagebackground extends jframe  {  public static void main(string[] args) throws exception {     jframe f = new jframe("crossroads simulation");     textdemo textdemo = new textdemo();        try     {         f.setcontentpane(new jlabel(new imageicon(imageio.read(new       file("c:/users/stacy/workspace/test/crossroadsbackground.png")))));  }     catch(ioexception e)     {         system.out.println("image doesnt exist");     }      f.setresizable(false);     f.pack();     f.setvisible(true);       jbutton button = new jbutton(new imageicon("c:/users/stacy/workspace /test/carblueone.png"));      button.setsize(new dimension(50, 50));      button.setlocation(130, 210);  button.setopaque(false); button.setcontentareafilled(false); button.setborderpainted(false);        f.add(button);      textdemo.createandshowgui();      //make sure new text visible, if there     //was selection in text area.      textdemo.textarea.setcaretposition(textdemo.textarea.getdocument().getlength(    ));            }  } 

textdemo.java

public class textdemo extends jpanel implements actionlistener { public jtextfield textfield; public jtextarea textarea; public final static string newline = "\n";     jframe f = new jframe("crossroads simulation"); imagebackground imagebackground = new imagebackground();   public textdemo() {         super(new gridbaglayout());     textfield = new jtextfield(20);     textfield.addactionlistener(this);         textarea = new jtextarea(5, 20);     textarea.seteditable(false);     jscrollpane scrollpane = new jscrollpane(textarea);      //add components panel.     gridbagconstraints c = new gridbagconstraints();     c.gridwidth = gridbagconstraints.remainder;      c.fill = gridbagconstraints.horizontal;     add(textfield, c);      c.fill = gridbagconstraints.both;     c.weightx = 1.0;     c.weighty = 1.0;     add(scrollpane, c);  }  protected static void createandshowgui() {     //create , set window.     jframe frame = new jframe("textdemo");     frame.setdefaultcloseoperation(jframe.exit_on_close);      //add contents window.     frame.add(new textdemo());      //display window.     frame.pack();     frame.setvisible(true);           } public void actionperformed(actionevent evt) {      string text = textfield.gettext();     int input = integer.parseint(text);  if (input == 1) {      jbutton button = new jbutton(new imageicon("c:/users/stacy/workspace /test/carredone.png"));      button.setsize(new dimension(50, 50));      button.setlocation(170, 210); } textarea.append(text + newline); textfield.selectall();   }      } 

you not have code expose text variable in textdemo other classes. should read/modify text textfield variable. should similar this:

public string gettext() { return textfield.gettext(); }

public void settext(string text) { textfield.settext(text); }


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -