i'm learning on own program in java , i'm trying make health bar, health shows when press "up" button (i making skeleton simple 2d game , button know command) health doesn't go down. when press button 3 times shows on cmd:
here code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class test{ private static jframe frame = new jframe("test"); private static jpanel panel = new jpanel(); private static jpanel panel1 = new jpanel(); private static jpanel panel2 = new jpanel(); private static jbutton button1 = new jbutton("up"); private static jbutton button2 = new jbutton("dn"); private static jbutton button3 = new jbutton("l"); private static jbutton button4 = new jbutton("r"); private static jlabel background = new jlabel(new imageicon("c:\\users\\beau\\downloads\\background.jpg")); private static int starthealth = 20; private static int healthboost = 0; private static int maxhealth = starthealth + healthboost; private static int damage = 0; private static int damagetaken; private static int curenthealth; public static void main(string[] args){ //jframe properties frame.setsize(900,600); frame.setlocationrelativeto(null); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setresizable(false); frame.setvisible(true); //shown stuff properties , adding damagetaken += damage; curenthealth = maxhealth - damagetaken; jprogressbar health = new jprogressbar(0,curenthealth); health.setstringpainted(true); health.setstring(curenthealth + "/" + maxhealth); health.setvalue(curenthealth); frame.add(background); panel.setlayout(null); background.setlayout(null); button1.setbounds(61,420,50,50); button2.setbounds(61,520,50,50); button3.setbounds(11,470,50,50); button4.setbounds(111,470,50,50); panel.setbounds(725,0,175,600); health.setbounds(10,20,150,20); panel.add(button1); panel.add(button2); panel.add(button3); panel.add(button4); panel.add(health); background.add(panel); button1.addactionlistener (new action1()); background.revalidate(); } static class action1 implements actionlistener{ public void actionperformed (actionevent e){ damage = 5; main(new string[] {"a","b","c"}); } } static class action2 implements actionlistener{ public void actionperformed (actionevent e){ } } static class actionreset implements actionlistener{ public void actionperformed (actionevent e){ } } } import javax.swing.*; import java.awt.*; import java.awt.event.*; public class test{ private static jframe frame = new jframe("test"); private static jpanel panel = new jpanel(); private static jpanel panel1 = new jpanel(); private static jpanel panel2 = new jpanel(); private static jbutton button1 = new jbutton("up"); private static jbutton button2 = new jbutton("dn"); private static jbutton button3 = new jbutton("l"); private static jbutton button4 = new jbutton("r"); private static jlabel background = new jlabel(new imageicon("c:\\users\\beau\\downloads\\background.jpg")); private static int starthealth = 20; private static int healthboost = 0; private static int maxhealth = starthealth + healthboost; private static int damage = 0; private static int damagetaken; private static int curenthealth; public static void main(string[] args){ //jframe properties frame.setsize(900,600); frame.setlocationrelativeto(null); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setresizable(false); frame.setvisible(true); //shown stuff properties , adding damagetaken += damage; curenthealth = maxhealth - damagetaken; jprogressbar health = new jprogressbar(0,curenthealth); health.setstringpainted(true); health.setstring(curenthealth + "/" + maxhealth); health.setvalue(curenthealth); frame.add(background); panel.setlayout(null); background.setlayout(null); button1.setbounds(61,420,50,50); button2.setbounds(61,520,50,50); button3.setbounds(11,470,50,50); button4.setbounds(111,470,50,50); panel.setbounds(725,0,175,600); health.setbounds(10,20,150,20); panel.add(button1); panel.add(button2); panel.add(button3); panel.add(button4); panel.add(health); background.add(panel); button1.addactionlistener (new action1()); background.revalidate(); } static class action1 implements actionlistener{ public void actionperformed (actionevent e){ damage = 5; main(new string[] {"a","b","c"}); } } static class action2 implements actionlistener{ public void actionperformed (actionevent e){ } } static class actionreset implements actionlistener{ public void actionperformed (actionevent e){ } } }
i searched on google , lot on stackoverflow.com didn't find problem looked problem. can please me problem?
you these exception if max value lower min value:
new jprogressbar(0,-5);
see code:
/** * initializes value, extent, minimum , maximum. adjusting false. * throws <code>illegalargumentexception</code> if following * constraints aren't satisfied: * <pre> * min <= value <= value+extent <= max * </pre> */ public defaultboundedrangemodel(int value, int extent, int min, int max) { if ((max >= min) && (value >= min) && ((value + extent) >= value) && ((value + extent) <= max)) { this.value = value; this.extent = extent; this.min = min; this.max = max; } else { throw new illegalargumentexception("invalid range properties"); } }
Comments
Post a Comment