我把头撞在墙上,感觉我完全错误地处理了这件事。我正在创建一个简单的刽子手游戏,在我的一生中,我无法使用我使用 JFrame 创建的按钮来显示我的paintComponent() 方法的结果。
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class hangman extends JPanel implements ActionListener {
public static final int youLose=6;
public int wrongGuess;
public String message;
public String theWord;
public StringBuffer guessWord;
public JButton restartButton;
public JButton playButton;
public JTextArea userInput;
public static void main(String[] args) {
hangman h = new hangman();
h.initialize();
}
public void initialize() {
userInput = new JTextArea();
restartButton = new JButton("Restart");
playButton = new JButton("Play");
JFrame frame = new JFrame("Hangman");
frame.setVisible(true);
frame.setLayout(new GridLayout(1,4));
frame.setBounds(0, 0, 500, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(restartButton);
frame.add(playButton);
frame.add(new JLabel("Enter guess: "));
frame.add(userInput);
restartButton.addActionListener(this);
playButton.addActionListener(this);
startGame();
}
public void paintComponent(Graphics g) {
//(left, top, right, bottom)
g.drawLine(90, 250,200,250);
g.drawLine(125,250,125,150);
g.drawLine(125,150,175,150);
g.drawLine(175,150,175,175);
if (wrongGuess > 0){ //head
g.drawOval(170,175,10,12);
}
if (wrongGuess > 1){ //body
g.drawLine(175,187,175,205);
}
if (wrongGuess > 2){ //left arm
g.drawLine(163,185,173,190);
}
if (wrongGuess > 3){ //right arm
g.drawLine(177,190,187,185);
}
if (wrongGuess > 4){ //left leg
g.drawLine(168,220,173,205);
}
if (wrongGuess > 5){ //right leg
g.drawLine(177,205,182,220);
}
g.drawString( message, 40, 290 );
g.drawString( new String (guessWord), 40, 275);
}
public void actionPerformed(ActionEvent event){
if (event.getSource() == restartButton){
restart();
startGame();
}
if (event.getSource() == playButton){
analyzeGuess();
userInput.setText("");
repaint();
}
setVisible(true);
}
public void restart() {
Graphics g = getGraphics();
Dimension d = getSize();
Color c = getBackground();
g.setColor(c);
g.fillRect(0,0,d.width,d.height);
repaint();
}
public void startGame() {
wrongGuess = 0;
String[] wordArray = {"computer", "science", "java", "application", "programming", "university",
"homework", "assignment", "cactus", "flower", "button", "keyboard", "graphic", "interface",
"collegiate", "graduate", "headphones", "building", "radiator", "flora", "fauna", "suitcase",
"sweater", "television", "library", "elevator", "precidence", "ancient", "basketball", "bracket",
"alphabetical", "christmas", "hannukah"};
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(wordArray.length);
theWord = new String(wordArray[randomInt]);
char positions[] = new char[theWord.length()];
for (int i=0; i<theWord.length(); i++) {
positions[i] = '_';
}
String s = new String(positions);
guessWord = new StringBuffer(s);
userInput.setText("");
message="";
repaint();
}
private void analyzeGuess() {
String userGuess, temp;
char letter;
userGuess = userInput.getText();
letter = userGuess.charAt(0);
if (!Character.isLetter(letter)){
message="Invalid character";
return;
}
if (userGuess.length()>1){
message="Only enter one letter";
return;
}
temp = new String(guessWord);
if (temp.indexOf(userGuess) != -1){
message="Letter has already been guessed";
return;
}
if (theWord.indexOf(userGuess) == -1){
message="";
message = new String(message);
wrongGuess++;
message = "You have "+ (youLose-wrongGuess) + " guesses left.";
if (wrongGuess==youLose){
message="You lose! The word was '"+theWord+"'"+"\nClick restart to try again.";
}
return;
}
for (int i=0; i<theWord.length(); i++){
if (theWord.charAt(i) == letter){
guessWord.setCharAt(i, letter);
}
}
temp = new String(guessWord);
if (temp.indexOf('_') == -1){
message="You win!";
return;
}
message="";
repaint();
}
}