我在java中编写了一个刽子手游戏,它作为一个应用程序运行良好,但现在我正试图将它变成一个applet。我遵循了如何将应用程序更改为applet的教程,但是当我将代码嵌入到html中并在我的localhost服务器上运行时,它要求我运行applet,然后开始加载Java,但它只是空白。我的GUI类的代码如下,请告诉我需要修复的内容,谢谢!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
public class HangmanGui extends Applet {
//String to hold userinput from the south text field
public static String uInput = "waitingForInput123";
public static boolean startButtonClicked = false;
public static boolean invalidGuess = true;
public static boolean wordWasGuessed = false;
public static boolean correctGuessWasMade = false;
public static boolean multipleIncorrectGuess = false;
public static boolean incorrectGuessMade = false;
public static boolean updateHangmanPicture = false;
public static boolean gameIsOver = false;
public static boolean gameIsOverNow = false;
public boolean waitingForYorNo = false;
public static boolean playerWonGame = false;
HangmanGame hm = new HangmanGame();
EnterButtonHelper enterListener = new EnterButtonHelper();
EnterFieldHelper enterKeyListener = new EnterFieldHelper();
//Start Screen components
JPanel frame = new JPanel(new BorderLayout());
JPanel gamePanel1 = new JPanel(new BorderLayout());
JPanel westPanelForButtons = new JPanel(new GridLayout(10, 1, 1, 1));
JButton startButton = new JButton(new AbstractAction("Start") {
public void actionPerformed(ActionEvent e) {
buildAfterPressingStart();
hm.startGame();
}
});
JButton instructionsButton = new JButton(new AbstractAction("Instructions") {
public void actionPerformed(ActionEvent e) {
instructionsButtonWindow();
}
});
Icon aba = new ImageIcon(getClass().getResource("welcomeToHangman.png"));
JLabel picLabel = new JLabel(aba);
//After Pressing start button components
JTextField playerInputField;
JButton playerInputButton;
JPanel southPanel;
JScrollPane gameScrollPane;
JTextArea textAreaCenter;
JPanel playPanelWest; //GridLayout with hangman icon and underscores
JTextArea underScores;
Icon hangNoose = new ImageIcon(getClass().getResource("hangman1.png"));
JLabel hangNooseL = new JLabel(hangNoose);
Icon hangHead = new ImageIcon(getClass().getResource("hangmanIncorrect1.png"));
JLabel hangHeadL = new JLabel(hangHead);
Icon hangBody = new ImageIcon(getClass().getResource("hangmanIncorrect2.png"));
JLabel hangBodyL = new JLabel(hangBody);
Icon hangArm1 = new ImageIcon(getClass().getResource("hangmanIncorrect3.png"));
JLabel hangArmL = new JLabel(hangArm1);
Icon hangArm2 = new ImageIcon(getClass().getResource("hangmanIncorrect4.png"));
JLabel hangArmL2 = new JLabel(hangArm2);
Icon hangLeg1 = new ImageIcon(getClass().getResource("hangmanIncorrect5.png"));
JLabel hangLegL = new JLabel(hangLeg1);
Icon hangLeg2 = new ImageIcon(getClass().getResource("hangmanIncorrect6.png"));
JLabel hangLegL2 = new JLabel(hangLeg2);
public void init() {
buildHangmanStartScreen();
}
void updateHangmanPic() {
switch (hm.getNumIncGues()) {
case 0:
playPanelWest.remove(hangLegL2);
playPanelWest.add(nooseLabel);
playPanelWest.updateUI();
break;
case 1:
playPanelWest.remove(nooseLabel);
playPanelWest.add(hangHeadL);
playPanelWest.updateUI();
break;
case 2:
playPanelWest.remove(hangHeadL);
playPanelWest.add(hangBodyL);
playPanelWest.updateUI();
break;
case 3:
playPanelWest.remove(hangBodyL);
playPanelWest.add(hangArmL);
playPanelWest.updateUI();
break;
case 4:
playPanelWest.remove(hangArmL);
playPanelWest.add(hangArmL2);
playPanelWest.updateUI();
break;
case 5:
playPanelWest.remove(hangArmL2);
playPanelWest.add(hangLegL);
playPanelWest.updateUI();
break;
case 6:
playPanelWest.remove(hangLegL);
playPanelWest.add(hangLegL2);
playPanelWest.updateUI();
break;
}
}
void resetWestPanel() {
underScores = new JTextArea();
playPanelWest = new JPanel(new GridLayout(2, 1, 1, 1));
playPanelWest.add(underScores);
playPanelWest.add(nooseLabel);
gamePanel1.add(BorderLayout.WEST, playPanelWest);
underScores.setEditable(false);
}
JLabel hangmanPic;
JLabel nooseLabel;
public void instructionsButtonWindow() {
JFrame instructionsFrame = new JFrame();
JTextArea instructionsArea = new JTextArea();
instructionsArea.setEditable(false);
instructionsFrame.add(instructionsArea);
// add instructions
instructionsArea.append(" * Welcome to Hangman! \n");
instructionsArea.append(" * To play, type in a letter as your guess, then press ENTER! \n");
instructionsArea.append(" * If you think you know the word, type in the whole word and see if you got it right! \n");
instructionsArea.append(" * But be careful! Guessing the word incorrectly will cost you a limb! \n");
instructionsArea.append(" * Enjoy the game. \n");
//instructionsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
instructionsFrame.setSize(600, 300);
instructionsFrame.setVisible(true);
}
public void buildHangmanStartScreen() {
startButton.setPreferredSize(new Dimension(150, 30));
instructionsButton.setPreferredSize(new Dimension(150, 30));
westPanelForButtons.add(startButton);
westPanelForButtons.add(instructionsButton);
gamePanel1.add(BorderLayout.WEST, westPanelForButtons);
gamePanel1.add(BorderLayout.CENTER, picLabel);
frame.add(gamePanel1);
frame.setSize(800, 600);
frame.setVisible(true);
}
public void buildAfterPressingStart() {
//remove components
gamePanel1.remove(westPanelForButtons);
gamePanel1.remove(picLabel);
//create new components
playerInputField = new JTextField();
playerInputButton = new JButton("Enter");
southPanel = new JPanel(new BorderLayout());
gameScrollPane = new JScrollPane();
textAreaCenter = new JTextArea(); //goes inside scrollpane
playPanelWest = new JPanel(new GridLayout(2, 1, 1, 1));
underScores = new JTextArea(); //holds underscores
hangNoose = new ImageIcon(getClass().getResource("hangman1.png"));
nooseLabel = new JLabel(hangNoose);
//add components
//south
southPanel.add(BorderLayout.CENTER, playerInputField);
southPanel.add(BorderLayout.EAST, playerInputButton);
gamePanel1.add(BorderLayout.SOUTH, southPanel);
playerInputField.requestFocusInWindow();
//west
playPanelWest.add(underScores);
playPanelWest.add(nooseLabel);
gamePanel1.add(BorderLayout.WEST, playPanelWest);
underScores.setEditable(false);
//center
gameScrollPane.getViewport().setView(textAreaCenter);
gamePanel1.add(BorderLayout.CENTER, gameScrollPane);
textAreaCenter.setText("Type your first guess to start!\n");
//east
gamePanel1.add(BorderLayout.EAST, new JPanel());
//north
gamePanel1.add(BorderLayout.NORTH, new JPanel());
playerInputButton.addActionListener(enterListener);
playerInputField.addKeyListener(enterKeyListener);
//update UI
gamePanel1.updateUI();
startButtonClicked = true;
}
public void startPlayingGame() {
hm.playGame();
}
/*public static void main(String[] args) {
// TODO Auto-generated method stub
HangmanGui h = new HangmanGui();
}*/
private class EnterFieldHelper implements KeyListener {
public void keyPressed(KeyEvent event){
if (event.getKeyCode()==KeyEvent.VK_ENTER) {
if (playerInputField.getText().equals("")) {
textAreaCenter.append("Please enter a valid guess\n");
playerInputField.setText("");
playerInputField.requestFocusInWindow();
} else {
uInput = playerInputField.getText();
playerInputField.setText("");
playerInputField.requestFocusInWindow();
textAreaCenter.append(uInput + "\n");
if (!gameIsOver) {
HangmanGame.uInput1 = uInput;
startPlayingGame();
underScores.setText(HangmanGame.underScoreString);
if (correctGuessWasMade) {
textAreaCenter.append("Correct guess! \n");
correctGuessWasMade = false;
}
if (multipleIncorrectGuess) {
textAreaCenter.append("You already guessed that letter. Try again.\n");
multipleIncorrectGuess = false;
}
if (incorrectGuessMade) {
textAreaCenter.append("Incorrect guess!\n");
updateHangmanPic();
incorrectGuessMade = false;
}
/*if (updateHangmanPicture) {
updateHangmanPic();
}*/
if (wordWasGuessed) {
textAreaCenter.append("GOOD JOB!\n");
textAreaCenter.append("YOU GUESSED THE WORD: " + hm.getChosenWord().toUpperCase() + "\n");
textAreaCenter.append("Would you like to play again? (Y / N)\n");
gameIsOver = true;
wordWasGuessed = false;
waitingForYorNo = true;
}
} else if (gameIsOverNow) {
//updateHangmanPic();
textAreaCenter.append("GAME OVER\n");
textAreaCenter.append("The word was " + hm.getChosenWord().toUpperCase() + "\n");
textAreaCenter.append("Would you like to play again? (Y / N)\n");
gameIsOverNow = false;
waitingForYorNo = true;
} else if (waitingForYorNo) {
if (uInput.equalsIgnoreCase("y")) {
hm.resetGame(0, false, false);
textAreaCenter.setText("Type your first guess to start!\n");
hm.resetAllValues(0, false);
resetWestPanel();
hm.startGame();
underScores.setText(HangmanGame.underScoreString);
gameIsOver = false;
waitingForYorNo = false;
} else if (uInput.equalsIgnoreCase("n")) {
textAreaCenter.setText("Ok, thank you for playing\n");
textAreaCenter.append("see you next time\n");
waitingForYorNo = false;
} else {
textAreaCenter.append("Please type Y or N, then press the Enter button\n");
}
}
}
}
}
public void keyReleased(KeyEvent event) {
}
public void keyTyped(KeyEvent event) {
}
}
private class EnterButtonHelper implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (playerInputField.getText().equals("")) {
textAreaCenter.append("Please enter a valid guess\n");
playerInputField.setText("");
playerInputField.requestFocusInWindow();
} else {
uInput = playerInputField.getText();
playerInputField.setText("");
playerInputField.requestFocusInWindow();
textAreaCenter.append(uInput + "\n");
if (!gameIsOver) {
HangmanGame.uInput1 = uInput;
startPlayingGame();
underScores.setText(HangmanGame.underScoreString);
if (correctGuessWasMade) {
textAreaCenter.append("Correct guess! \n");
correctGuessWasMade = false;
}
if (multipleIncorrectGuess) {
textAreaCenter.append("You already guessed that letter. Try again.\n");
multipleIncorrectGuess = false;
}
if (incorrectGuessMade) {
textAreaCenter.append("Incorrect guess!\n");
updateHangmanPic();
incorrectGuessMade = false;
}
/*if (updateHangmanPicture) {
updateHangmanPic();
}*/
if (wordWasGuessed) {
textAreaCenter.append("GOOD JOB!\n");
textAreaCenter.append("YOU GUESSED THE WORD: " + hm.getChosenWord().toUpperCase() + "\n");
textAreaCenter.append("Would you like to play again? (Y / N)\n");
gameIsOver = true;
wordWasGuessed = false;
waitingForYorNo = true;
}
} else if (gameIsOverNow) {
//updateHangmanPic();
textAreaCenter.append("GAME OVER\n");
textAreaCenter.append("The word was " + hm.getChosenWord().toUpperCase() + "\n");
textAreaCenter.append("Would you like to play again? (Y / N)\n");
gameIsOverNow = false;
waitingForYorNo = true;
} else if (waitingForYorNo) {
if (uInput.equalsIgnoreCase("y")) {
hm.resetGame(0, false, false);
textAreaCenter.setText("Type your first guess to start!\n");
hm.resetAllValues(0, false);
resetWestPanel();
hm.startGame();
underScores.setText(HangmanGame.underScoreString);
gameIsOver = false;
waitingForYorNo = false;
} else if (uInput.equalsIgnoreCase("n")) {
textAreaCenter.setText("Ok, thank you for playing\n");
textAreaCenter.append("see you next time\n");
waitingForYorNo = false;
} else {
textAreaCenter.append("Please type Y or N, then press the Enter button\n");
}
}
}
}
}
}
此外,这是我的HTML
<!DOCTYPE html>
<html>
<head><title>Second HTML</title>
<style type="text/css">
body {background-color:gray;}
</style>
</head>
<center><applet code="HangmanGui.class" width="800" height="600"><param name="SIZE" value="8"></param></applet></center>
<body>
<h1 style="font-family:arial;color:black;text-align:center;">Abdul's HTML Page</h1>
<p style="background-color:lightgray;color:black;">Welcome to my page!</p>
</body>
</html>
答案 0 :(得分:2)
你在哪里向applet本身添加任何内容?我没有看到你在代码中的任何地方这样做。
你需要:
add(...)
方法)。