java - Make a JPanel Change After ActionListener -
i have make game school project in cs. want make person may input name , subsequent panels have name on it. using card layout, works part. when run game , input name, works on card. however, when go next card, string name supposed appear, appears null. suspicion program creates of cards before given chance accept name input , therefore failing. teacher doesn't know how either.
here segment of code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class novelcards extends jframe { private string name; private cardlayout cards; private jtextfield txt = new jtextfield(10); private jlabel label = new jlabel(); public novelcards() { super("novel"); masterpanel = new jpanel(); cards = new cardlayout(); txt.addactionlistener( new actionlistener(){ public void actionperformed(actionevent e){ name = txt.gettext(); label.settext(name); } }); button2 = new jbutton("next"); button2.addactionlistener(new nextlistener()); jpanel card2 = new jpanel(); card2.add(button2); card2.add(txt); card2.add(label); card2.add(new jlabel(" enter name ")); jbutton button3 = new jbutton("next"); button3.addactionlistener(new nextlistener()); jpanel card3 = new jpanel(); card3.add(new jlabel("hello there, " + name + "!")); card3.add(button3); masterpanel.setlayout(cards); masterpanel.add("2",card2); masterpanel.add("3",card3); this.setcontentpane(masterpanel); cards.show(masterpanel, "1"); } public class nextlistener implements actionlistener{ public void actionperformed(actionevent event){ cards.next(masterpanel); } here photos of program when running.
here name enter screen after picking name , hitting enter:

and here subsequent card:

thanks in advance help! (i have directly posted images don't have enough reputation)
the question need ask is, how other cards/panels know value of name is?
a simple solution pass name panels. becomes problematic when want add new cards/panels in, need update code, sure, use list or array , have cards/panels implement common interface...
or use "model" of kind, cards/panels shared reference to. allow more share data between them without having mess around much
so if perhaps direct me need learn pass name panel?
make class, preferably starting interface, acts container data, becomes model...
public interface novelcardsmodel { public string getname(); public void setname(string name); public void addpropertychangelistener(propertychangelistener listener); public void removepropertychangelistener(propertychangelistener listener); // add accessors other data might think // need share } now, create implementation of model
public class defaultnovelcardsmodel implements novelcardsmodel { private propertychangesupport propertychangesupport; private string name; public defaultnovelcardsmodel() { propertychangesupport = new propertychangesupport(this); } @override public string getname() { return name; } @override public void setname(string value) { if (value == null || !value.equals(name)) { string old = name; name = value; propertychangesupport.firepropertychange("name", old, name); } } @override public void addpropertychangelistener(propertychangelistener listener) { propertychangesupport.addpropertychangelistener(listener); } @override public void removepropertychangelistener(propertychangelistener listener) { propertychangesupport.removepropertychangelistener(listener); } } for each panel going need know name, need pass same instance of model it...
defaultnovelcardsmodel model = new defaultnovelcardsmodel(); masterpanel = new masterpane(model); // other cards. each panel wants know when name changes, need register propertychangelistener , monitor changes name property
public class masterpanel extends jpanel { private novelcardsmodel model; public masterpanel(novelcardsmodel model) { this.model = model; this.model.addpropertychangelistener(new propertychangelistener() { @override public void propertychange(propertychangeevent evt) { if ("name".equals(evt.getpropertyname())) { // name has changed } } }); } }
Comments
Post a Comment