0

I have a hangmanframe, welcomeframe, and a mainframe, and I would like to append stars (*) on the mainframe when you win a hangman game

HangmanFrame:

public void win(){
    JOptionPane.showMessageDialog(null, "Congrats! The word was " + GuessWord);
    MainFrame.totalStars.append("*");
    MainFrame.totalLabel.setText(MainFrame.stars);
    setVisible(false);
    MainFrame.hangmanButton.setEnabled(false);
    MainFrame.returnMain();
}

MainFrame:

    public static final StringBuilder totalStars = new StringBuilder();
    public static String stars = totalStars.toString();

    public static void returnMain(){
            totalStars.append("* ");
            totalLabel.setText(stars);
            WelcomeFrame.playButton.doClick();
    }

WelcomeFrame:

    private void playButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String userName = nameText.getText();

    // Open MainFrame
    MainFrame MFrame = new MainFrame();
    MFrame.setVisible(true);
    setVisible(false);
    MainFrame.welcomeLabel.setText("Welcome " + userName + "!");
    MainFrame.totalStars.append("* ");
    MainFrame.totalStarsLabel.setText(MainFrame.stars);
    }

I have a label (totalLabel) that I would like to add stars (*) to. But when the method runs in the HangmanFrame, it goes to the PlayFrame but doesn't add on any stars whatsoever.

Can anyone see what I'm doing wrong?

SOLVED:

changed method name to returnMain()

changed all instances of "stars" with "totalStars.toString()"

2
  • 1
    This code would not even compile: "return" is a keyword and cannot be used as the name of a method in your MainFrame listing. On another subject, I would recommend to follow good OOP practices and properly encapsulate fields in getter/setter methods. Commented Dec 6, 2012 at 1:09
  • i changed the method to read returnMain(), thanks Commented Dec 6, 2012 at 1:13

1 Answer 1

1

First, your program will not compile because of public static void return() {..., I think you should know that return is a reserved word in Java.

Second, You make public static String stars = totalStars.toString(); and that is OK, however it doesn't mean that stars will always contain what totalString has. It is just instantaneous.

You should definitely use totalLabel.setText(totalStars.toString());

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! It worked, but it makes multiple stars, i just want one?
@CodeAddict upvote my answer then =) if you want one asterisk, append one asterisk, not more.
okay, so when you win the hangman game the code reads MainFrame.totalStars.append("*"); why is it doing it more than once if this code is only read once?
@CodeAddict if you are clicking the button many times, it will append more.
I had the append method in lots of places apparently, just took all but one out and it works. Thanks! accpeted as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.