Jump to content
  • Advertisement
Sign in to follow this  
fantasyme

[java] Jlabel update problem

This topic is 3169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a dice game and I use JLabel to display score. The score display according to constructor but the score doesn't update. NEED HELP!

public class roll extends JPanel {
    //======================================================= instance variables
    private mydice MYleftDie;     // component for one die 

    
	private JLabel myscore;

    //============================================================== constructor
   
    roll() {
        //... Create the dice
        MYleftDie  = new mydice(Color.WHITE,Color.BLACK);
        

        //this.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
        //...Create the button to roll the dice
        JButton rollButton = new JButton("New Roll");
        rollButton.setFont(new Font("Sansserif", Font.PLAIN, 24));
        
        //... Add listener.
        rollButton.addActionListener(new RollListener());
       
        JLabel myscore = new JLabel(""+outcome(),JLabel.CENTER);
        myscore.setFont(new Font("Sansserif", Font.PLAIN, 24));

        //... Layout components
        
      this.setLayout(new GridLayout(2, 4));   
      this.add(MYleftDie);
        this.add(myscore);
  

}
    



    //========================================================== method setScore
	 public int outcome(){
		int total;
		 total = MYleftDie.getValue(); 
		 return total;

	 }
	 
    ////////////////////////////////////////// inner listener class RollListener
    private class RollListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            MYleftDie.roll();
   
    

       	myscore.repaint();
        }
    }
}



Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by fantasyme
I am making a dice game and I use JLabel to display score.
The score display according to constructor but the score doesn't update.
NEED HELP!

*** Source Snippet Removed ***


You need to update the label text when you update the value to display, e.g.:

int val = 123;
mylabel.setText(Integer.toString(val));

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!