#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    JButton only performs function once.


    hello guys. im making here a game and currently on a battle scene. and im trying to update the health of the character(decrement its hp)every time i use an ability which is on the button_1. but it seems it only works once. how do i make it work as many as i can?? on my Jframe the hp only reduces from 2000 to 1999 and it doesnt decrement anymore from there(like 1998 downwards).

    Code:
    public int subtract(int a,int b){	//function
    		return a-b;
    		}
    	int championhp=2000,dmg=1;
    	public Kaiser1stBattle() {
    		setResizable(false);
    		setIconImage(Toolkit.getDefaultToolkit().getImage("src\\LoC Icon.jpg"));
    		setTitle("Purge v1");
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 1000, 560);
    		contentPane = new JPanel();
    		contentPane.setBackground(Color.WHITE);
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		JLabel label = new JLabel("");
    		label.setIcon(new ImageIcon("src\\KaiserBattle.jpg"));
    		label.setBounds(10, 115, 200, 400);
    		contentPane.add(label);
    		
    		final JEditorPane dtrpnHp = new JEditorPane();
    		dtrpnHp.setEditable(false);
    		dtrpnHp.setText("HP:"+championhp);
    		dtrpnHp.setFont(new Font("Tahoma", Font.BOLD, 27));
    		dtrpnHp.setBackground(Color.WHITE);
    		dtrpnHp.setBounds(10, 64, 171, 40);
    		contentPane.add(dtrpnHp);
    		
    		JButton button_1 = new JButton("wew");
    		button_1.setBounds(436, 149, 292, 74);
    		contentPane.add(button_1);
    		button_1.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				championhp=Integer.parseInt("2000");
    				dmg = Integer.parseInt("1");
    				championhp=subtract(championhp,dmg);
    				dtrpnHp.setText(String.valueOf("HP: "+championhp));
    			}
    			
    		});
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    it seems it only works once.
    Do you mean that the listener is only called one time? Add a call to the println() method to print a message when it is called so you will know if it is called more than once.

    the hp only reduces from 2000 to 1999 and it doesnt decrement anymore
    Where does the code get the value that it is supposed to decrement? Should it read that value from somewhere, decrement it and then put it back?
    Last edited by NormR; March 23rd, 2013 at 07:13 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by NormR
    Do you mean that the listener is only called one time? Add a call to the println() method to print a message when it is called so you will know if it is called more than once.


    Where does the code get the value that it is supposed to decrement? Should it read that value from somewhere, decrement it and then put it back?
    Already found a solution. I tried using JOptionPane to update the hp rather refreshing the frame. thanks for the help though.

IMN logo majestic logo threadwatch logo seochat tools logo