bottlegnomes Posted February 27, 2013 Share Posted February 27, 2013 (edited) So I'm making a hangman game in java for my comp sci class and we need counters for wins and losses. The background of the panel for both is yellow, and I want to set the labels used for wins and losses to gray, but it's not working for some reason. (Heres the code:) private class Scores extends JPanel implements ActionListener { public Scores() { setSize(100, 300); setBackground(Color.yellow); //int winCount = getWinCount(); int winCount = 0; JLabel wins = new JLabel("" + winCount); //sets background color wins.setBackground(Color.GRAY); //sets wins size wins.setPreferredSize(new Dimension(100, 35)); //adds wins border wins.setBorder(BorderFactory.createTitledBorder("Wins")); //adds wins to panel add(wins); //int lossCount = getLossCount(); int lossCount = 0; JLabel losses = new JLabel("" + lossCount); //sets background color losses.setBackground(Color.GRAY); //sets losses size losses.setPreferredSize(new Dimension(100, 35)); //adds losses border losses.setBorder(BorderFactory.createTitledBorder("Losses")); //adds losses add(losses); JButton newGame = new JButton("New Game"); add(newGame); } // end of constructor for Scores Edited February 27, 2013 by bottlegnomes Quote Link to comment Share on other sites More sharing options...
Snike Posted February 27, 2013 Share Posted February 27, 2013 Is there any reason why yellow is in lowercase while gray is in upper case? Also is it a compiler error or just not showing up as a certain color? Quote Link to comment Share on other sites More sharing options...
Lenh Posted February 27, 2013 Share Posted February 27, 2013 (edited) According to the docs for the JLabel.setColor(Color) function you might want to throw a wins.setOpaque(true) statement in there. (Edit: Better link) Edited February 27, 2013 by Lenh Quote Link to comment Share on other sites More sharing options...
eclipse Posted February 27, 2013 Share Posted February 27, 2013 Are the borders showing up? Quote Link to comment Share on other sites More sharing options...
bottlegnomes Posted February 27, 2013 Author Share Posted February 27, 2013 (edited) For the first question, not really. It seems to work either way; then again, who knows. For the second, it's just not showing up. Everything compiles fine and the window opens, just wins and losses are still yellow instead of gray. Another question. private class TheWord extends JPanel { String word = "dog"; /** * Where the initial JTextFields are created. */ public TheWord() { word = "dog"; [b]} // end of constructor for TheWord[/b] for (int i; i < word.length; i++) { JLabel letter = new JLabel("__"); } } On the bolded line, I'm getting a syntax error: Syntax error on "}", { expected. Now this makes absolutely no sense, since there's a { that corresponds to it, and there aren't any other random opening curly braces. Edited February 27, 2013 by bottlegnomes Quote Link to comment Share on other sites More sharing options...
Lenh Posted February 27, 2013 Share Posted February 27, 2013 (edited) the for (int i; blah blah blah ...) {...} stuff is outside a function inside a class. You need to put it inside one (I think, my Java 101 is kind of rusty). Edited February 27, 2013 by Lenh Quote Link to comment Share on other sites More sharing options...
bottlegnomes Posted February 27, 2013 Author Share Posted February 27, 2013 I got that fixed. Had to put the for loop inside curly braces for whatever reason. Quote Link to comment Share on other sites More sharing options...
eclipse Posted February 27, 2013 Share Posted February 27, 2013 (edited) For the first question, not really. It seems to work either way; then again, who knows. For the second, it's just not showing up. Everything compiles fine and the window opens, just wins and losses are still yellow instead of gray. Another question. private class TheWord extends JPanel { String word = "dog"; /** * Where the initial JTextFields are created. */ public TheWord() { word = "dog"; [b]} // end of constructor for TheWord[/b] for (int i; i < word.length; i++) { JLabel letter = new JLabel("__"); } } On the bolded line, I'm getting a syntax error: Syntax error on "}", { expected. Now this makes absolutely no sense, since there's a { that corresponds to it, and there aren't any other random opening curly braces. I see where it's missing. private class TheWord extends JPanel { (3) String word = "dog"; /** * Where the initial JTextFields are created. */ public TheWord() { (2) word = "dog"; [b]} // end of constructor for TheWord[/b] for (int i; i < word.length; i++) { (1) JLabel letter = new JLabel("__"); } (1) } (3) Insert the missing closing bracket for (2). EDIT: Never mind, see the other set of closing brackets. I'll leave this up as-is anyway. Edited February 27, 2013 by eclipse Quote Link to comment Share on other sites More sharing options...
Lenh Posted February 27, 2013 Share Posted February 27, 2013 (edited) What're you trying to do in TheWord class? It doesn't make sense to reassign letter word.length times. I'm guessing you want to assign an array of letter labels within your construction method, using word. Declare your JLabel letter array up top along with word. I'm guessing this is what it should be. private class TheWord extends JPanel { String word = "dog"; JLabel letter[] //or however you declare an array in Java /** * Where the initial JTextFields are created. */ public TheWord() { word = "dog"; for (int i=0; i < word.length; i++) { letter[i] = new JLabel("__"); } } } (Edit: Needed to initialize i in the for statement, too) Edited February 27, 2013 by Lenh Quote Link to comment Share on other sites More sharing options...
bottlegnomes Posted February 27, 2013 Author Share Posted February 27, 2013 That picks a random word from a list of words to use and assigns it to word, which I still haven't written yet, hence the word = dog placeholder. After the word has been assigned, it creates a number of labels equal to the length of the word and adds those to the panel as placeholders for the word while people guess letters. When they guess one, it replaces all relevant labels with another label of that letter (haven't written that either). But I got that fixed, and it's working, so now I just need to figure out why the color isn't showing up. Quote Link to comment Share on other sites More sharing options...
Gryz Posted February 27, 2013 Share Posted February 27, 2013 Use setColor instead of setBackground for the gray win/loss numbers? Quote Link to comment Share on other sites More sharing options...
Lenh Posted February 27, 2013 Share Posted February 27, 2013 (edited) wins.setOpaque(true); add(wins); Really should do it. One more thing to watch out for is if you're running an old build? Edited February 27, 2013 by Lenh Quote Link to comment Share on other sites More sharing options...
bottlegnomes Posted February 28, 2013 Author Share Posted February 28, 2013 ^That did it. Thanks, all. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.