This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java swing problem  RSS feed

 
abdul rahman ahmad
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a game that I wrote with Java that runs perfectly on command line. However, I've been building a GUI for it and have been changing it so it runs on the GUI but I'm having issues. It's a hangman game that allows the player to guess letters to try to guess the hangman word. If the player makes a correct guess, the game displays a certain message, and if the player makes an incorrect guess, the game displays a different message. The game stops working after I've made two guesses using the GUI version though... I've been trying to fix it for a couple of days but no luck...I've tried calling javax.swing.SwingUtilities.invokeLater but it's still giving me issues. Any help would be appreciated, heres the code (p.s. I'm still in the process of moving stuff from command line to GUI):


 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not be attempting to do a simple straight conversion from a command line game to a GUI game.

A GUI is based on events. That is a person clicks a button and the game starts. There is no need for looping in your code. Once the game starts the GUI just sits there waiting for the user to do something. Input should come from a GUI component like a JTextField, not the command line that uses a Scanner.

For example, when a user enters a character and the game does something. Either the character matches a letter in the game or it doesn't. If they have made too many guesses the game is over. You can either use a DocumentListener to determine when a character has been entered in a text field, or you can force the user to click on a button to process the character entered.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!