Results 1 to 4 of 4
  1. #1
    fantasyme is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default Jlabel update problem

    I am new for java.

    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!

    (I also think to use paint(g) but drawstring doesn't show my text so I try on JLabel but the JLabel doesn't update when score change!)

    Java Code:
    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();
            }
        }
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 04-14-2010 at 05:08 AM. Reason: Moderator Edit: Code tags added

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    1) Use the "Code" tags when posting code, so the code is readable.

    2) The GUI and its components should only be built once. After that to update the label you just do:

    label.setText(...);

  3. #3
    fantasyme is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    I tried to use
    myscore.setText(""+outcome());
    myscore.repaint();
    but it doesn't work

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are declaring your JLabel twice, once in the class -- and this one isn't being displayed and a second time in the constructor here:
    Java Code:
    JLabel myscore = new JLabel(""+outcome(),JLabel.CENTER);
    and this one is being displayed.

    Solution, declare the variable once -- in the class only. So change the above to
    Java Code:
    myscore = new JLabel(""+outcome(),JLabel.CENTER);  // JLabel is initialized but not redeclared

Similar Threads

  1. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  2. Replies: 2
    Last Post: 02-15-2010, 06:49 AM
  3. Swing JLabel Problem
    By g123456 in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 02:17 AM
  4. Problem altering text in JLabel
    By porchrat in forum New To Java
    Replies: 3
    Last Post: 06-29-2009, 12:37 AM
  5. JLabel + GUI problem
    By tonyelaltaico in forum Java Applets
    Replies: 5
    Last Post: 02-03-2009, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •