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

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2

    Help with hangman game


    I'm writing this hangman game. I'm trying to show the number of dashes in a word, example, cat is - - - to the JLabel, but it displays only one dash. But, the System.out.print displays the correct number of dashes. How can I make the JLabel show more than one dash? I've tried this with JTextField also,but still only one dash.

    Code:
    package hangman;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.util.Random;
    
    public class hangmanPanel extends JPanel{
        
        String[] wordsArray = new String[] {"lion", "cat", "india", "fat", "mouse", "cheetah", "airplane"};
        
        ImageIcon[] imageArray = new ImageIcon[] {new ImageIcon("zero.png"), new ImageIcon("one.png"), new ImageIcon("two.png"), 
                                                  new ImageIcon("three.png"), new ImageIcon("four.png"), new ImageIcon("five.png"), 
                                                  new ImageIcon("six.png")};
        public JLabel imageLabel, dashLabel, label;
        public JTextField guess;
        public JButton play;
        public int selectWord;
        public String showWord;
        
        public hangmanPanel()
        {
            Random rand = new Random();
            selectWord = rand.nextInt(wordsArray.length) + 0;
            
            showWord = wordsArray[selectWord];
            
            System.out.println(showWord);
            
            dashLabel = new JLabel("");
            guess = new JTextField(6);
            label = new JLabel("Enter a character.");
            play = new JButton("Show");       
            imageLabel = new JLabel(imageArray[0]);  
                    
            for(int a = 0; a < showWord.length(); a++)
            {
                dashLabel.setFont(new Font("Helvetica", Font.PLAIN, 48));
                dashLabel.setText(" - ");
                System.out.print(" - ");
            }
            
            add(dashLabel);
            add(guess);
            add(label);
            add(play);
            add(imageLabel);
                    
            setPreferredSize(new Dimension(500, 500));
        }
        
    }
    Last edited by tonynsx; September 18th, 2013 at 10:27 AM.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Code:
     dashLabel.setText(" - ");
    This would set the text of the label to " - ". If you want to have more than one - shown, then you need to build a String with the desired number of - and use that with the setText() method.
    Build the String in the loop and call setText() after the loop with the String that was built.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    Code:
     dashLabel.setText(" - ");
    This would set the text of the label to " - ". If you want to have more than one - shown, then you need to build a String with the desired number of - and use that with the setText() method.
    Build the String in the loop and call setText() after the loop with the String that was built.
    Tried that, but still same output.

    Code:
    package hangman;
    
    import java.awt.Dimension;
    import java.awt.Font;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.util.Random;
    
    public class hangmanPanel extends JPanel{
        
        String[] wordsArray = new String[] {"lion", "cat", "india", "fat", "mouse", "cheetah", "airplane", "toy", "jump", "push", "elephant"};
        
        ImageIcon[] imageArray = new ImageIcon[] {new ImageIcon("zero.png"), new ImageIcon("one.png"), new ImageIcon("two.png"), 
                                                  new ImageIcon("three.png"), new ImageIcon("four.png"), new ImageIcon("five.png"), 
                                                  new ImageIcon("six.png")};
        public JLabel imageLabel, dashLabel, label;
        public JTextField guess;
        public JButton play;
        public int selectWord;
        public String showWord, dashString;
        
        public hangmanPanel()
        {
            Random rand = new Random();
            selectWord = rand.nextInt(wordsArray.length) + 0;
            
            showWord = wordsArray[selectWord];
            
            System.out.println(showWord);
            
            dashLabel = new JLabel("");
            guess = new JTextField(6);
            label = new JLabel("Enter a character.");
            play = new JButton("Try");       
            imageLabel = new JLabel(imageArray[0]);  
                    
            for(int a = 0; a < showWord.length(); a++)
            {
                dashString = " - ";
                System.out.print(dashString);
            }
            
            dashLabel.setFont(new Font("Helvetica", Font.PLAIN, 48));
            dashLabel.setText(dashString);
            add(dashLabel);
            add(guess);
            add(label);
            add(play);
            add(imageLabel);
                    
            setPreferredSize(new Dimension(500, 500));
        }
        
    }
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Where does the code "build" a String with multiple -s? I only see where it sets a String to " -". It needs to use String concatenation to "add" on more -s. The + operator is used for concatenting Strings: "asdf" + "wert" gives "asdfwert"
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    Where does the code "build" a String with multiple -s? I only see where it sets a String to " -". It needs to use String concatenation to "add" on more -s. The + operator is used for concatenting Strings: "asdf" + "wert" gives "asdfwert"
    Thanks for the clarification, now it works like I want. I forgot about the string concatenation. Why does it work differently for JLabels and for System.out.print?

    Code:
    package hangman;
    
    import java.awt.Dimension;
    import java.awt.Font;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.util.Random;
    
    public class hangmanPanel extends JPanel{
        
        String[] wordsArray = new String[] {"lion", "cat", "india", "fat", "mouse", "cheetah", "airplane", "toy", "jump", "push", "elephant"};
        
        ImageIcon[] imageArray = new ImageIcon[] {new ImageIcon("zero.png"), new ImageIcon("one.png"), new ImageIcon("two.png"), 
                                                  new ImageIcon("three.png"), new ImageIcon("four.png"), new ImageIcon("five.png"), 
                                                  new ImageIcon("six.png")};
        public JLabel imageLabel, dashLabel, label;
        public JTextField guess;
        public JButton play;
        public int selectWord;
        public String showWord, dashString;
        
        public hangmanPanel()
        {
            Random rand = new Random();
            selectWord = rand.nextInt(wordsArray.length) + 0;
            
            showWord = wordsArray[selectWord];
            
            System.out.println(showWord);
            
            dashLabel = new JLabel("");
            guess = new JTextField(6);
            label = new JLabel("Enter a character.");
            play = new JButton("Try");       
            imageLabel = new JLabel(imageArray[0]); 
            
            dashString = "";
                    
            for(int a = 0; a < showWord.length(); a++)
            {
                dashString = dashString + " - ";
            }
            
            dashLabel.setFont(new Font("Helvetica", Font.PLAIN, 48));
            dashLabel.setText(dashString);
            add(dashLabel);
            add(label);
            add(guess);        
            add(play);
            add(imageLabel);
                    
            setPreferredSize(new Dimension(450, 300));
        }
        
    }
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Why does it work differently for JLabels and for System.out.print?
    Old output to the console is not replaced by the print method. Every call to print() adds a new string to the console. The setText() method replaces old content with new.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    Old output to the console is not replaced by the print method. Every call to print() adds a new string to the console. The setText() method replaces old content with new.
    Thank you sir,

    I'm continuing with my program.

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo