java - How to change the value of an integer by pressing a JButton? -
i'm making program everytime press jbutton decrease value of int. want make i decrease 20 each time button pressed. example:
static int = 100; static final int f = 20; public static void main(string[] args) { jpanel content = new jpanel(); jbutton 1 = new jbutton("move 1"); jlabel label = new jlabel ("health: " + i); content.add(one); content.add(label); one.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e) { if(one.isselected()) { = i-f ; } } }); jframe window = new jframe("fight!"); window.setcontentpane(content); window.setsize(400,400); window.setlocation(300,150); window.setvisible(true); window.setdefaultcloseoperation(jframe.exit_on_close); window.repaint(); }
you have update text of jlabel explicitly:
if(one.isselected()) { = i-f ; label.settext("health: " + i); }
Comments
Post a Comment