我正在制作一个JTable,它会覆盖将图标放入所需的表格中。这就是我所做的。但结果在所有单元格中打印图标不仅仅是我的第一个单元格而且不是我想要的。结果如下:
import random
from __future_ import print_function
HANGMANPICS = ['''
+---+
| |
|
|
|
|
|
====== ''' , '''
+---+
| |
0 |
|
|
|
|
====== ''' , '''
+---+
| |
0 |
| |
|
|
|
====== ''' , '''
+---+
| |
0 |
/| |
|
|
|
======= ''' , '''
+----+
| |
0 |
/|\ |
|
|
|
======== ''' , '''
+----+
| |
0 |
/|\ |
/ |
|
|
======== ''' , '''
+------+
| |
0 |
/|\ |
/ \ |
|
|
========= ''']
words = """ cougar badger beaver cobra lion tiger skin ventriloquist magician monkey moose pigeon
rabbit rhino trout wombat kangaroo python lizard raven skunk peacock hoki crab prawns cancer
sloth snake spider parrot penguin ferret eagle cock hen peahen turkey turtle dinosaur metaphor
iteration object apple """.split()
def getRandomWord(wordList):
#this function returns a random string from the words list
wordIndex = random.randint(0,len(wordList)-1) #as we count from 0
return wordList[wordIndex]
def displayBoard(HANGMANPICS , missedLetters, correctLetters, secretWord):
print (HANGMANPICS[len(missedLetters)])
print() #give space after every string character
print ('Missed letters: ', end = ' ')
for letter in missedLetters:
print (letter, end = ' ')
print()
blanks = '_' * len(secretWord)
#replace blanks with correct guesses
for i in range (len(secretWord)):
if secretWord[i] in correctLetters:
blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
#show secret word with spaces between each letter
for letter in blanks:
print (letter , end=' ')
print()
def getGuess(alreadyGuessed): #makes sure player enters a letter and nothing else
while True:
print ('Guess a letter.')
guess = raw_input()
guess = guess.lower()
if len(guess) != 1:
print "please enter a single character."
elif guess in alreadyGuessed:
print "you have already guessed that letter. Choose another."
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print "please only enter a letter"
else:
return guess #returns user i/p
#function is true if player wants to play again , else false
def playAgain():
print "do you wanna play again? (yes or no)"
return raw_input.lower().startswith('y')
print "<<<<<<<<<<< H A N G M A N >>>>>>>>>>>>"
missedLetters = ''
correctLetters = ''
secretWord = getRandomWord(words)
gameIsDone = False
while True:
displayBoard(HANGMANPICS , missedLetters , correctLetters , secretWord)
#getting a letter from user
guess = getGuess(missedLetters + correctLetters)
if guess in secretWord:
correctLetters = correctLetters + guess
#check if the player has won
foundAllLetters = True
for i in range(len(secretWord)):
if secretWord[i] not in correctLetters:
foundAllLetters = False
break
if foundAllLetters:
print "Yes! the secret word is " + secretWord + "! You win!"
gameisDone = True
else:
missedLetters = missedLetters + guess
#check if player has exhausted his guess limits and lost
if len(missedLetters) == len(HANGMANPICS) - 1:
displayBoard(HANGMANPICS , missedLetters , correctLetters , secretWord)
print " you have run out of guesses! \n After " + str(len(missedLetters)) + "missed guesses and" + str(len(correctLetters))
+ "correct guesses the word was " + secretWord
gameisDone = True
if gameisDone:
if playAgain():
missedLetters = ''
correctLetters = ''
gameisDone = False
secretWord = getRandomWord(words)
else:
break
答案 0 :(得分:1)
将label.setIcon(null);
添加到else
块,否则您只需返回上次设置的内容
有关详细信息,请参阅Concepts: Editors and Renderers