我正在制作一个刽子手游戏,并且在我的程序中遇到了我的JOptionPane问题。它出现的任何时候都会被绘制到JPanel上。我不确定问题是什么,但感谢任何帮助。 Issue
Hangman Class
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
public class Hangman
{
protected JFrame frame=new JFrame("Hangman");
private HangmanPanel panel;
private final int GAMEWIDTH=800, GAMEHEIGHT=600;
protected WordHandler wh;
private Timer timer;
private boolean reset = true;
private boolean showAnswer = false;
private int readyToQuit=1;
public Hangman(){
try {
wh = new WordHandler("dictionary.txt");
}
catch(FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
}
}
public int getReadyToQuit(){
return readyToQuit;
}
public void run(){
frame.setSize(GAMEWIDTH, GAMEHEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); frame.setResizable(false);
//frame.addKeyListener(tetris);
//frame.addKeyListener(new KeyListener(){ });
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
panel=new HangmanPanel();
frame.getContentPane().add(panel);
frame.getContentPane().validate();
frame.getContentPane().repaint();
frame.addKeyListener(panel);
frame.pack();
timer=new Timer(1000/60,new ActionListener(){
public void actionPerformed(ActionEvent e)
{update();}});
timer.start();
}
public void addLetter(String letter)
{
if(wh.guess(letter))
{
panel.repaint();
}
}
public void update()
{
panel.repaint();
}
public int drawQuitFrame(){
String[] options = new String[] {"Play Again", "Quit"}; int i=-1;
while(i<0)
i=JOptionPane.showOptionDialog(null, "Do you want to play again?","GAME OVER", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(i==0)
return 0;
else{
return 1;
}
}
public void drawResetFrame(){
String[] options = new String[] {"Yes", "No"};
int i=-1;
while(i<0)
{
i=JOptionPane.showOptionDialog(null, "Do you want to reset the game?","RESET", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
}
switch(i){
case 0: wh.resetSolution(); reset = true; showAnswer = false; break;
default: break;
}
timer.start();
}
public void drawCloseFrame(){
String[] options = new String[] {"Yes", "No"}; int i=-1;
while(i<0)
i=JOptionPane.showOptionDialog(null, "Do you want to return to the menu?","Quit?", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
readyToQuit=i;
}
public void resetGame(){
wh.resetSolution(); reset = true; showAnswer = false;
}
public class HangmanPanel extends JPanel implements KeyListener
{
public HangmanPanel(){
setPreferredSize(new Dimension(GAMEWIDTH,GAMEHEIGHT));
}
public void paintComponent(Graphics g){
if(reset)
{
g.setColor(Color.WHITE);
g.fillRect(0,0,GAMEWIDTH,GAMEHEIGHT);
reset = false;
}
g.setFont(new Font("Arial", Font.BOLD, 15));
g.drawString("Press esc to quit",GAMEWIDTH-160,GAMEHEIGHT-30);
g.drawString("Press enter to restart",GAMEWIDTH-160,GAMEHEIGHT-10);
if(!showAnswer)
{
g.setFont(new Font("Arial", Font.BOLD, 50));
for(int i = 0; i < wh.getCurrGuess().length(); i++)
{
g.drawString(wh.getCurrGuess().substring(i,i+1),((i*75)+50),500);
g.drawString("__",((i*75)+40),510);
}
}
else
{
g.setFont(new Font("Arial", Font.BOLD, 50));
for(int i = 0; i < wh.getCurrGuess().length(); i++)
{
g.drawString(wh.getSolution().substring(i,i+1),((i*75)+50),500);
g.drawString("__",((i*75)+40),510);
}
}
g.setFont(new Font("Arial", Font.BOLD, 30));
g.drawString("Guessed Letters",533,40);
for(int i = 0; i < wh.getGuessedLetters().size(); i++)
{
g.drawString(wh.getGuessedLetters().get(i),(((i%5)*50)+540),(((i/5)*50)+100));
}
g.fillRect(40,340,225,20);
g.fillRect(140,30,20,310);
g.fillRect(140,30,200,20);
g.fillRect(340,30,20,40);
if(wh.getRemainingGuesses() == 5)
{
g.fillOval(320,65,60,60);
}
else if(wh.getRemainingGuesses() == 4)
{
g.fillOval(320,65,60,60);
g.fillRect(340,120,20,110);
}
else if(wh.getRemainingGuesses() == 3)
{
g.fillOval(320,65,60,60);
g.fillRect(340,120,20,110);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(20));
g2.drawLine(355,230,410,275);
}
else if(wh.getRemainingGuesses() == 2)
{
g.fillOval(320,65,60,60);
g.fillRect(340,120,20,110);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(20));
g2.drawLine(355,230,410,275);
g2.drawLine(345,230,290,275);
}
else if(wh.getRemainingGuesses() == 1)
{
g.fillOval(320,65,60,60);
g.fillRect(340,120,20,110);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(20));
g2.drawLine(355,230,410,275);
g2.drawLine(345,230,290,275);
g2.drawLine(345,170,290,125);
}
else if(wh.getRemainingGuesses() == 0)
{
g.fillOval(320,65,60,60);
g.fillRect(340,120,20,110);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(20));
g2.drawLine(355,230,410,275);
g2.drawLine(345,230,290,275);
g2.drawLine(345,170,290,125);
g2.drawLine(355,170,410,125);
}
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_A){addLetter("A");}
else if (key == KeyEvent.VK_B){addLetter("B");}
else if (key == KeyEvent.VK_C){addLetter("C");}
else if (key == KeyEvent.VK_D){addLetter("D");}
else if (key == KeyEvent.VK_E){addLetter("E");}
else if (key == KeyEvent.VK_F){addLetter("F");}
else if (key == KeyEvent.VK_G){addLetter("G");}
else if (key == KeyEvent.VK_H){addLetter("H");}
else if (key == KeyEvent.VK_I){addLetter("I");}
else if (key == KeyEvent.VK_J){addLetter("J");}
else if (key == KeyEvent.VK_K){addLetter("K");}
else if (key == KeyEvent.VK_L){addLetter("L");}
else if (key == KeyEvent.VK_M){addLetter("M");}
else if (key == KeyEvent.VK_N){addLetter("N");}
else if (key == KeyEvent.VK_O){addLetter("O");}
else if (key == KeyEvent.VK_P){addLetter("P");}
else if (key == KeyEvent.VK_Q){addLetter("Q");}
else if (key == KeyEvent.VK_R){addLetter("R");}
else if (key == KeyEvent.VK_S){addLetter("S");}
else if (key == KeyEvent.VK_T){addLetter("T");}
else if (key == KeyEvent.VK_U){addLetter("U");}
else if (key == KeyEvent.VK_V){addLetter("V");}
else if (key == KeyEvent.VK_W){addLetter("W");}
else if (key == KeyEvent.VK_X){addLetter("X");}
else if (key == KeyEvent.VK_Y){addLetter("Y");}
else if (key == KeyEvent.VK_Z){addLetter("Z");}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ESCAPE){
timer.stop();
drawCloseFrame();
if(readyToQuit==1)
timer.start();
}
else if(key == KeyEvent.VK_ENTER){
timer.stop();
drawResetFrame();
}
}
}
Word Handler Class
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class WordHandler
{
private ArrayList<String> words;
private int remainingGuesses;
private String solution;
private String currGuess;
private ArrayList<String> guessedLetters;
/**
* Construct an EvilHangman object and initiailize all instance variables.
* @param fileName the name of the file that contains the word list.
* @throws FileNotFoundException when the fileName file does not exist.
*/
public WordHandler(String fileName)
throws FileNotFoundException
{
inputWords(fileName);
remainingGuesses = 6;
int pos = (int)(Math.random()*words.size());
solution = words.get(pos);
//solution = "ANT";
guessedLetters = new ArrayList<String>();
updateCurrGuess();
}
public boolean guess(String letter)
{
for(int i = 0; i < guessedLetters.size(); i++)
{
if(guessedLetters.get(i).equalsIgnoreCase(letter))
{
return false;
}
}
guessedLetters.add(letter);
if(contains(letter))
{
updateCurrGuess();
}
else
{
remainingGuesses--;
}
return true;
}
public void updateCurrGuess()
{
currGuess = "";
for(int i = 0; i < solution.length(); i++)
{
for(int j = 0; j < guessedLetters.size(); j++)
{
if(solution.substring(i,i+1).equalsIgnoreCase(guessedLetters.get(j)))
{
currGuess += guessedLetters.get(j);
j = guessedLetters.size();
}
}
if(currGuess.length() <= i)
{
currGuess += " ";
}
}
}
public boolean contains(String letter)
{
for(int i = 0; i < solution.length(); i++)
{
if(solution.substring(i,i+1).equalsIgnoreCase(letter))
{
return true;
}
}
return false;
}
public String getCurrGuess()
{
return currGuess;
}
public String getSolution()
{
return solution;
}
public ArrayList<String> getGuessedLetters()
{
return guessedLetters;
}
public int getRemainingGuesses()
{
return remainingGuesses;
}
public void resetSolution()
{
int pos = (int)(Math.random()*words.size());
solution = words.get(pos);
remainingGuesses = 6;
guessedLetters.clear();
updateCurrGuess();
}
public boolean gameOver()
{
return remainingGuesses == 0 || currGuess.equalsIgnoreCase(solution);
}
////////// PRIVATE HELPER METHODS //////////
/**
* Helper method for the constructor:
* Inputs the word length from the user, reads in the words from
* the fileName file, and initializes the words instance variable
* with the words of the correct length. This method loops until
* a valid word length is entered.
* @param fineName the name of the file that contains the word list.
* @throws FileNotFoundException when the fileName file does not exist.
*/
private void inputWords(String fileName) throws FileNotFoundException
{
words = new ArrayList<String>();
while (words.size() == 0)
{
Scanner file = new Scanner(new File(fileName));
while (file.hasNext())
{
String word = file.next();
if(word.length() < 11)
{
words.add(word);
}
}
}
}
/**
* Helper method for playGame:
* Inputs a one-letter string from the user.
* Loops until the string is a one-character character in the range
* a-z or A-Z.
* @return the single-letter string converted to upper-case.
*/
private String inputLetter()
{
// String letter = null;
// while (letter == null)
// {
// System.out.print("Next letter? ");
// letter = scanner.next().toUpperCase();
//
// if (letter.length() != 1 ||
// "A".compareTo(letter) > 0 ||
// "Z".compareTo(letter) < 0)
// {
// letter = null;
// System.out.println("Invalid input!");
// }
// }
// return letter;
return null;
}