gpt4 book ai didi

Java小程序是空白的

转载 作者:行者123 更新时间:2023-11-30 04:04:32 27 4
gpt4 key购买 nike

我用java编写了一个刽子手游戏,它作为一个应用程序运行得很好,但现在我正试图将它变成一个小程序。我按照教程了解如何将应用程序更改为小程序,但是当我将代码嵌入 html 并在本地主机服务器上运行它时,它要求我运行小程序,并开始加载 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>

最佳答案

您在哪里向小程序本身添加任何内容?我没有看到您在代码中的任何地方执行此操作。

您需要:

  • 仅使用 Swing 组件,即 JApplet,而不是 Applet。
  • 阅读有关如何创建小程序的教程。如果你没有研究过它们,你就无法编写它们。他们将告诉您如何向小程序添加组件(即通过调用 add(...) 方法)。

关于Java小程序是空白的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21078785/

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