swing - Java keeps adding buttons! - JFrames - -
alright, so, trying make game using jframe
that, when click button, adds money, , show update text(a jlabel
).
you see, should update windows.money variable , display new variable on screen, but, adds more buttons. please note: money part works fine.
it's java doesn't want replace, add.
code:
package dev.bobdabiulder.classic.main; import javax.swing.jlabel; public class bucket extends window{ private static final long serialversionuid = 1l; public bucket() throws interruptedexception { for(int = 0; !window.paused; ) { window.money += 2; this.wait(1000); } window.buckets++; window.bucketcounter = new jlabel("you have: " + window.buckets + " buckets!"); }
}
in windows
class...
package dev.bobdabiulder.classic.main; import java.awt.button; import java.awt.component; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; public class window extends jframe{ public static long money = 0; private static final long serialversionuid = 1l; private static thread t1 = new thread(); private static thread t2 = new thread(); private static thread t3 = new thread(); private static thread t4 = new thread(); private static thread t5 = new thread(); public jbutton bucket = new jbutton("buy bucket!"); public jbutton add$ = new jbutton("add money!"); public jlabel money = new jlabel(money + ""); public static long buckets = 0; public static jlabel bucketcounter = new jlabel("you have: " + buckets + " buckets!"); public static jpanel buck = new jpanel(); public static boolean paused = false; static jframe jf = new jframe("lol!"); //window method public window() { jf.setvisible(true); jf.setdefaultcloseoperation(jframe.exit_on_close); buck.add(bucket); buck.add(money); buck.add(add$); buck.add(bucketcounter); jf.setsize(500, 500); jf.add(buck); bucket.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { if(e.getsource().equals(bucket)) { try { new bucket(); } catch (exception e1) { e1.printstacktrace(); } } system.out.println("action performed!"); } }); pack(); } //end of actionperformed //start of start() public static void start() { t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); } // // // @suppresswarnings("deprecation") public static void stop() { t1.stop(); t2.stop(); t3.stop(); t4.stop(); t5.stop(); } }
i made requested edits, , errors put *, errors read:
cannot make static reference non-static field window.(anything)
,
error spots:
for(int = 0; !*window.paused; ) { *window.money += 2; this.wait(1000); } *window.buckets++; *window.bucketcounter = new jlabel("you have: " + *window.buckets + " buckets!");
brief example of mvc, model-view. uses swing timer , not threads (not directly is) increment jlabel held in different class. uses propertychangelistener , support notify view (the gui) of changes in state of model.
import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyevent; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import java.text.numberformat; import javax.swing.*; import javax.swing.event.changeevent; import javax.swing.event.changelistener; import javax.swing.event.swingpropertychangesupport; /** * http://stackoverflow.com/q/22620807/522444 * http://stackoverflow.com/a/22621767/522444 * @author pete * */ @suppresswarnings("serial") public class shortmvc extends jpanel { private jtextfield moneyfield = new jtextfield(10); private jtextfield bucketfield = new jtextfield(10); private moneymodel model = new moneymodel(); private timer timer = new timer(model.gettimerdelay(), new timerlistener()); private jbutton moneybutton = new jbutton("add money"); private jbutton bucketbutton = new jbutton("add bucket"); public shortmvc() { moneyfield.seteditable(false); moneyfield.setfocusable(false); bucketfield.seteditable(false); bucketfield.setfocusable(false); bucketfield.settext(string.valueof(model.getbuckets())); add(new jlabel("money:")); add(moneyfield); add(moneybutton); add(new jlabel("buckets:")); add(bucketfield); add(bucketbutton); moneybutton.getmodel().addchangelistener(new moneybtnmodellistener()); moneybutton.setmnemonic(keyevent.vk_m); bucketbutton.addactionlistener(new bucketbuttonlistener()); bucketbutton.setmnemonic(keyevent.vk_b); model.addpropertychangelistener(new modellistener()); timer.setinitialdelay(0); } private class bucketbuttonlistener implements actionlistener { @override public void actionperformed(actionevent e) { model.incrementbuckets(); } } private class moneybtnmodellistener implements changelistener { private boolean pressed = false; @override public void statechanged(changeevent e) { buttonmodel model = (buttonmodel) e.getsource(); if (pressed == model.ispressed()) { return; } pressed = model.ispressed(); if (pressed) { timer.start(); } else { timer.stop(); } } } private class timerlistener implements actionlistener { @override public void actionperformed(actionevent e) { model.incrementmoney(model.getmoneyincrementamount()); } } private class modellistener implements propertychangelistener { private numberformat moneyformat = numberformat.getcurrencyinstance(); public modellistener() { moneyfield.settext(moneyformat.format(model.getmoney())); } @override public void propertychange(propertychangeevent pcevt) { if (moneymodel.money.equals(pcevt.getpropertyname())) { moneyfield.settext(moneyformat.format(model.getmoney())); } else if (moneymodel.buckets.equals(pcevt.getpropertyname())) { int buckets = model.getbuckets(); bucketfield.settext(string.valueof(buckets)); timer.setdelay(model.gettimerdelay()); } } } private static void createandshowgui() { jframe frame = new jframe("short mvc"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new shortmvc()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } } class moneymodel { public static final string money = "money"; public static final string buckets = "buckets"; private static final int init_timer_delay = 500; public static final long money_increment_amount = 2l; private long money = 0l; private int buckets = 1; private int timerdelay = init_timer_delay; private swingpropertychangesupport pcsupport = new swingpropertychangesupport( this); public void setmoney(long money) { long oldvalue = this.money; long newvalue = money; this.money = money; pcsupport.firepropertychange(money, oldvalue, newvalue); } public long getmoneyincrementamount() { return money_increment_amount; } public void incrementmoney(long addtomoney) { long oldvalue = this.money; long newvalue = money + addtomoney; this.money = newvalue; pcsupport.firepropertychange(money, oldvalue, newvalue); } public long getmoney() { return money; } public void setbuckets(int buckets) { int oldvalue = this.buckets; int newvalue = buckets; this.buckets = newvalue; timerdelay = init_timer_delay / buckets; pcsupport.firepropertychange(buckets, oldvalue, newvalue); } public void incrementbuckets(int incrementamount) { int newvalue = this.buckets + incrementamount; setbuckets(newvalue); } // increment 1 public void incrementbuckets() { incrementbuckets(1); } public int getbuckets() { return buckets; } public int gettimerdelay() { return timerdelay; } public void addpropertychangelistener(propertychangelistener listener) { pcsupport.addpropertychangelistener(listener); } public void removepropertychangelistener(propertychangelistener listener) { pcsupport.removepropertychangelistener(listener); } }
note can't use actionlistener work since actionlistener gets activated when button released. i'm assuming want accumulate money when button pressed, , stop accumulating when released. this, must extract jbutton's model, add changelistener it, , react changes model's ispressed()
method. use start , stop swing timer increments model.
edit
next iteration better mvc (model-view-control) separation:
import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyevent; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import java.text.numberformat; import javax.swing.*; import javax.swing.event.changeevent; import javax.swing.event.changelistener; import javax.swing.event.swingpropertychangesupport; import javax.swing.text.jtextcomponent; /** * http://stackoverflow.com/q/22620807/522444 * http://stackoverflow.com/a/22621767/522444 * @author pete * */ public class shortmvc { private static void createandshowgui() { shortview view = new shortview(); moneymodel model = new moneymodel(); shortcontrol control = new shortcontrol(model, view); control.init(); jframe frame = new jframe("short mvc"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(view.getmainpanel()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } } class shortview { private jtextfield moneyfield = new jtextfield(10); private jtextfield bucketfield = new jtextfield(10); private jbutton moneybutton = new jbutton(); private jbutton bucketbutton = new jbutton(); private jpanel mainpanel = new jpanel(); public shortview() { moneyfield.seteditable(false); moneyfield.setfocusable(false); bucketfield.seteditable(false); bucketfield.setfocusable(false); mainpanel.add(new jlabel("money:")); mainpanel.add(moneyfield); mainpanel.add(moneybutton); mainpanel.add(new jlabel("buckets:")); mainpanel.add(bucketfield); mainpanel.add(bucketbutton); } public jcomponent getmainpanel() { return mainpanel; } public jtextcomponent getmoneyfield() { return moneyfield; } public jtextcomponent getbucketfield() { return bucketfield; } public abstractbutton getmoneybutton() { return moneybutton; } public abstractbutton getbucketbutton() { return bucketbutton; } } @suppresswarnings("serial") class shortcontrol { private moneymodel model; private shortview view; private timer timer; private moneybtnaction moneybtnaction = new moneybtnaction("add money", keyevent.vk_m); private bucketbuttonaction bucketaction = new bucketbuttonaction("add buckets", keyevent.vk_b); public shortcontrol(moneymodel model, shortview view) { this.model = model; this.view = view; timer = new timer(model.gettimerdelay(), new timerlistener()); } public void init() { timer.setinitialdelay(0); view.getbucketfield().settext(string.valueof(model.getbuckets())); view.getmoneybutton().setaction(moneybtnaction); view.getmoneybutton().getmodel().addchangelistener(moneybtnaction); view.getbucketbutton().setaction(bucketaction); model.addpropertychangelistener(new modellistener()); } private class bucketbuttonaction extends abstractaction { public bucketbuttonaction(string name, int mnemonic) { super(name); putvalue(mnemonic_key, mnemonic); } @override public void actionperformed(actionevent e) { model.incrementbuckets(); } } private class moneybtnaction extends abstractaction implements changelistener { private boolean pressed = false; public moneybtnaction(string name, int mnemonic) { super(name); putvalue(mnemonic_key, mnemonic); } @override public void actionperformed(actionevent e) { // empty } @override public void statechanged(changeevent e) { buttonmodel model = (buttonmodel) e.getsource(); if (pressed == model.ispressed()) { return; } pressed = model.ispressed(); if (pressed) { timer.start(); } else { timer.stop(); } } } private class modellistener implements propertychangelistener { private numberformat moneyformat = numberformat.getcurrencyinstance(); public modellistener() { view.getmoneyfield().settext(moneyformat.format(model.getmoney())); } @override public void propertychange(propertychangeevent pcevt) { if (moneymodel.money.equals(pcevt.getpropertyname())) { view.getmoneyfield().settext(moneyformat.format(model.getmoney())); } else if (moneymodel.buckets.equals(pcevt.getpropertyname())) { int buckets = model.getbuckets(); view.getbucketfield().settext(string.valueof(buckets)); timer.setdelay(model.gettimerdelay()); } } } private class timerlistener implements actionlistener { @override public void actionperformed(actionevent e) { model.incrementmoney(model.getmoneyincrementamount()); } } } class moneymodel { public static final string money = "money"; public static final string buckets = "buckets"; private static final int init_timer_delay = 500; public static final long money_increment_amount = 2l; private long money = 0l; private int buckets = 1; private int timerdelay = init_timer_delay; private swingpropertychangesupport pcsupport = new swingpropertychangesupport( this); public void setmoney(long money) { long oldvalue = this.money; long newvalue = money; this.money = money; pcsupport.firepropertychange(money, oldvalue, newvalue); } public long getmoneyincrementamount() { return money_increment_amount; } public void incrementmoney(long addtomoney) { long oldvalue = this.money; long newvalue = money + addtomoney; this.money = newvalue; pcsupport.firepropertychange(money, oldvalue, newvalue); } public long getmoney() { return money; } public void setbuckets(int buckets) { int oldvalue = this.buckets; int newvalue = buckets; this.buckets = newvalue; timerdelay = init_timer_delay / buckets; pcsupport.firepropertychange(buckets, oldvalue, newvalue); } public void incrementbuckets(int incrementamount) { int newvalue = this.buckets + incrementamount; setbuckets(newvalue); } // increment 1 public void incrementbuckets() { incrementbuckets(1); } public int getbuckets() { return buckets; } public int gettimerdelay() { return timerdelay; } public void addpropertychangelistener(propertychangelistener listener) { pcsupport.addpropertychangelistener(listener); } public void removepropertychangelistener(propertychangelistener listener) { pcsupport.removepropertychangelistener(listener); } }
Comments
Post a Comment