gpt4 book ai didi

java - Java Hangman 程序中 JOptionPane/JPanel 的奇怪问题

转载 作者:行者123 更新时间:2023-12-04 05:30:24 25 4
gpt4 key购买 nike

enter image description here

我有一个关于 Hangman 程序的非常具体的问题。

您可能希望自己运行该程序以了解我遇到的问题。

当计数器(保存错误猜测次数的变量)达到 6 时,问题就会出现。此时,用户已用完所有分配的猜测,并可以选择再次玩。如果用户选择再次播放,则调用 askQuestion 方法,在该方法中,JOptionPane 从用户那里获取一个字符串,该字符串将成为要猜测的新单词。

在用户输入一个词并单击“确定”后,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);

}

}

这是我的 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() 来解决,但我一直无法这样做。还想提一下,这个程序显然还有很多其他的缺陷;它远非健壮。那是因为它远未完成。你不必让我注意到这些问题,我会在解决这个错误后处理它们。谢谢你看这个!
编辑:我的问题已解决,但是我仍然希望将其打开一段时间,看看是否有人能确切说明为什么我的原始代码不起作用。

最佳答案

问题可能与您显示JOptionPane.showConfirmDialog有关。在 paintComponent方法。该方法负责组件绘制。避免将程序逻辑放入该方法中。绘画操作应该是快速和优化的,以获得更好的性能和用户体验。从这种方法弹出一个模态对话框不是一个好主意。此外,您无法控制何时调用此方法。调用 repaint()只调度组件的更新请求。

退房 Performing Custom Painting tutorial有关详细信息和示例。还看Painting in AWT and Swing .

关于java - Java Hangman 程序中 JOptionPane/JPanel 的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12701798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com