关于我的 Hangman 程序,我有一个非常具体的问题。
您可能希望自己运行该程序以了解我遇到的问题。
当计数器(包含错误猜测次数的变量)达到 6 时,就会出现问题。在这一点上,用户已经用完了所有分配的猜测,并且可以选择再次玩。如果用户选择再次播放,则调用 askQuestion 方法,其中 JOptionPane 从用户那里获取一个字符串,该字符串将作为要猜测的新单词。
在用户输入一个单词并单击 OK 后,JOptionPane 出于某种原因被绘制到了面板上。
这是我遇到的问题。在您提交一封信作为猜测后它会消失,但它存在时是一个非常难看的错误。我在上面包含了一个屏幕截图。这是我的代码:
//********************************************************
// HangmanPanel.java
//
// Creates your run-of-the mill hangman game.
//********************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HangmanPanel extends JPanel
{
private JLabel inputLabel; // Input field for letter guesses
private String word; // Variable to hold answer word
private String guessed = "Guessed: "; // Holds letters already guessed
private String l, text = ""; // l: Variable to hold letter guess; text: Tells user whether their guess was correct
private JTextField letter; // Text Field for Input Label
private int counter = 0; // Incremented when wrong guess is made; controls drawing of hangman
private String underscore = ""; // Shows answer as sequence of underscores, which are replaced with correct guesses
private boolean playing = true;
//------------------------------------------
// Sets up the hangman panel.
//------------------------------------------
public HangmanPanel()
{
setBackground (Color.white);
setPreferredSize (new Dimension(300, 300));
askQuestion();
inputLabel = new JLabel ("Enter a letter.");
add (inputLabel);
letter = new JTextField (1);
add (letter);
letter.addActionListener(new TempListener());
}
public void askQuestion()
{
word = JOptionPane.showInputDialog ("Enter the word.");
for (int i = 0; i < word.length(); i++)
{
underscore += "-";
}
repaint();
}
//----------------------------------
// Paints a hanging man.
//----------------------------------
public void paintComponent (java.awt.Graphics page)
{
super.paintComponent (page);
final int xBound = 200, yBound = 20;
if (playing)
{
page.setColor (Color.black);
page.fillRect(xBound + 30, yBound + 80, 60, 10); // Stand
page.fillRect (xBound +55, yBound, 10, 80); // Gallows Pole
page.fillRect(xBound + 25, yBound, 40, 5);
page.fillRect(xBound + 25, yBound, 3, 20); // Rope
if (counter > 0)
page.fillOval(xBound + 18, yBound + 15, 16, 16); // Head
if (counter > 1)
page.fillRect(xBound + 24, yBound + 23, 5, 30); // Torso
if (counter > 2)
page.drawLine(xBound + 23, yBound + 40, xBound + 13, yBound + 30); // Right Arm
if (counter > 3)
page.drawLine(xBound + 29, yBound + 40, xBound + 39, yBound + 30); // Left Arm
if (counter > 4)
page.drawLine(xBound + 23, yBound + 53, xBound + 18, yBound + 63); // Right Leg
if (counter > 5)
{
page.drawLine(xBound + 29, yBound + 53, xBound + 34, yBound + 63); // Left Leg
text = "";
counter=0;
underscore = "";
guessed = "Guessed: ";
//page.drawString("Play Again?", 50, 130);
int again = JOptionPane.showConfirmDialog (null, "Do Another?");
if (again == JOptionPane.YES_OPTION)
askQuestion();
else
playing = false;
}
page.drawString(guessed, 20, 130);
page.drawString(underscore, 20, 110);
page.drawString(text, 20, 250);
}
else
page.drawString("Goodbye", 50, 50);
}
private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
l = letter.getText(); // Stores letter guess as a string
letter.setText(""); // Clears Text Field
char let = l.charAt(0); // Creates a char variable for letter guess
guessed = guessed + let + " ";
int index = word.indexOf(let);
if (index != -1)
{
text = "Correct";
underscore = underscore.substring(0,index) + word.substring(index, index+1) + underscore.substring(index+1); // Replaces underscore with found letter
String substring = word.substring(index+1);
int index2 = substring.indexOf(let);
while (substring.indexOf(let) != -1)
{
index2 = substring.indexOf(let);
index = index + index2 + 1;
underscore = underscore.substring(0,index) + word.substring(index, index+1) + underscore.substring(index+1);
substring = word.substring(index+1);
}
}
else
{
text = "Wrong";
counter++;
}
if (underscore.indexOf('-') == -1)
{
text = "You Win!";
//askQuestion
}
repaint();
}
}
}
这是 HangmanFrame 类。这与我的问题无关,我只为那些想要运行该程序的人提供它:
import javax.swing.JFrame;
public class HangmanFrame
{
//----------------------------------------------
// Creates the main frame of the program.
//----------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Hangman");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
HangmanPanel panel = new HangmanPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
这是我的搜索类,它也与我的问题没有直接关系:
public class Search{
public static Comparable linearSearch (String word, String letter, int start)
{
while (!found && index < word.length())
{
if (word.charAt(index) == letter.charAt(0))
found = true;
else
index++;
}
if (found)
return index;
else
return null;
}
}
我为那里的格式问题道歉,我尽力了。无论如何,我希望有人能指出我正确的方向,我怀疑这个问题可以通过调用 repaint() 来解决,但我一直无法这样做。还想提一下,这个程序显然还有很多其他缺陷;它远非健壮。那是因为它远未完成。您不必将这些问题提请我注意,我会在解决此错误后解决它们。谢谢你看看这个!编辑:我的问题已解决,但是我仍然想将其保持开放一段时间,看看是否有人可以准确地阐明为什么我的原始代码不起作用。