关于我的Hangman程序,我有一个非常具体的问题。
您可能希望自己运行该程序以了解我遇到的问题。
当计数器(保持错误猜测数量的变量)达到6时出现问题。此时,用户已经用完所有分配的猜测,并可以选择再次播放。如果用户选择再次播放,则调用askQuestion方法,其中JOptionPane从用户获取将成为要猜测的新单词的String。
用户输入单词并单击“确定”后,JOptionPane由于某种原因被绘制到Panel上。
这是我遇到的问题。它在您提交一封信作为猜测后消失,但在它存在时是一个非常难看的错误。我上面有一个截图。这是我的代码:
//********************************************************
// 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);
}
}
这是我的Search类,它与我的问题没有直接关系:
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()来解决,但我无法这样做。还想提一下,这个程序显然还有很多其他缺陷;它远非强大。那是因为它还远未完成。你不必把这些问题引起我的注意,我会在解决这个问题之后再研究它们。谢谢你看看! 编辑:我的问题已得到修复,但我仍然希望暂时保持这个问题,看看是否有人能够明确说明我的原始代码无法正常工作的原因。
答案 0 :(得分:3)
问题可能与您在JOptionPane.showConfirmDialog
方法中显示paintComponent
的事实有关。该方法负责组件绘制。避免将程序逻辑放入该方法中。绘画操作应该快速和优化,以获得更好的性能和用户体验。从这种方法弹出模态对话框不是一个好主意。而且,您无法控制何时调用此方法。调用repaint()
仅调度组件的更新请求。
查看Performing Custom Painting tutorial了解详情和示例。另请参阅Painting in AWT and Swing。