Jump to content

Anyone know Java?


bottlegnomes
 Share

Recommended Posts

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 by bottlegnomes
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by bottlegnomes
Link to comment
Share on other sites

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 by Lenh
Link to comment
Share on other sites

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 by eclipse
Link to comment
Share on other sites

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 by Lenh
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...