我被困在一件作业上。我们的任务是创建一个刽子手游戏,当点击开始按钮时,将创建一个单词。单击按钮时,它将搜索该字符并将其返回。它还有一个停止按钮,将结束游戏。如果再次单击“开始”按钮,则需要获取一个新单词,从而可以选择一个新的空白字符串。正如您在下面看到的,我目前有几个最终变量,因此可以将它们传递给搜索字符的方法。单击“开始”按钮时,需要覆盖这些值。这有可能吗?我真的只需要在点击开始按钮时重启游戏。
package hangman;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Component;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
public class HangmanGame {
private JFrame frmHangman;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HangmanGame window = new HangmanGame();
window.frmHangman.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public HangmanGame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
String words[];
words = new String[] {"SMALL", "HINGE", "ORTHODOX","EMPTY", "SOUTH", "SOCIAL",
"AFTERSHOCK", "CROWN","GRIEVING", "NEBULA"};
int randomNum;
randomNum = 0 + (int)(Math.random()*10);
final String currentWord = words[randomNum];
System.out.println(currentWord);
final StringBuffer blanks;
blanks = makeBlanks(currentWord);
int tries = 0;
frmHangman = new JFrame();
frmHangman.setTitle("Hangman");
frmHangman.setBounds(100, 100, 506, 372);
frmHangman.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmHangman.getContentPane().setLayout(null);
final JButton btnStop = new JButton("Start Game");
btnStop.setForeground(Color.BLACK);
btnStop.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnStop.setBackground(Color.LIGHT_GRAY);
btnStop.setBounds(10, 269, 132, 30);
frmHangman.getContentPane().add(btnStop);
JLabel lblWelcome = new JLabel("WELCOME TO HANGMAN");
lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
lblWelcome.setFont(new Font("Tahoma", Font.BOLD, 16));
lblWelcome.setBounds(74, 9, 329, 41);
frmHangman.getContentPane().add(lblWelcome);
final JLabel lblXXX = new JLabel("XXXXXXXXXXXXXXXX");
lblXXX.setFont(new Font("Tahoma", Font.BOLD, 15));
lblXXX.setHorizontalAlignment(SwingConstants.CENTER);
lblXXX.setBounds(74, 73, 329, 30);
frmHangman.getContentPane().add(lblXXX);
JPanel firstRowKey = new JPanel();
firstRowKey.setBounds(2, 113, 477, 33);
frmHangman.getContentPane().add(firstRowKey);
final JButton btnA = new JButton("A");
btnA.setEnabled(false);
firstRowKey.add(btnA);
final JButton btnB = new JButton("B");
btnB.setEnabled(false);
firstRowKey.add(btnB);
final JButton btnC = new JButton("C");
btnC.setEnabled(false);
firstRowKey.add(btnC);
final JButton btnD = new JButton("D");
btnD.setEnabled(false);
firstRowKey.add(btnD);
final JButton btnE = new JButton("E");
btnE.setEnabled(false);
firstRowKey.add(btnE);
final JButton btnF = new JButton("F");
btnF.setEnabled(false);
firstRowKey.add(btnF);
final JButton btnG = new JButton("G");
btnG.setEnabled(false);
firstRowKey.add(btnG);
final JButton btnH = new JButton("H");
btnH.setEnabled(false);
firstRowKey.add(btnH);
final JButton btnI = new JButton("I");
btnI.setEnabled(false);
firstRowKey.add(btnI);
final JButton btnJ = new JButton("J");
btnJ.setEnabled(false);
firstRowKey.add(btnJ);
JPanel secondRowKey = new JPanel();
secondRowKey.setBounds(0, 146, 479, 33);
frmHangman.getContentPane().add(secondRowKey);
final JButton btnK = new JButton("K");
btnK.setEnabled(false);
secondRowKey.add(btnK);
btnK.setVerticalAlignment(SwingConstants.TOP);
final JButton btnL = new JButton("L");
btnL.setEnabled(false);
secondRowKey.add(btnL);
btnL.setVerticalAlignment(SwingConstants.TOP);
final JButton btnM = new JButton("M");
btnM.setEnabled(false);
secondRowKey.add(btnM);
btnM.setVerticalAlignment(SwingConstants.TOP);
final JButton btnN = new JButton("N");
btnN.setEnabled(false);
secondRowKey.add(btnN);
btnN.setVerticalAlignment(SwingConstants.TOP);
final JButton btnO = new JButton("O");
btnO.setEnabled(false);
secondRowKey.add(btnO);
btnO.setVerticalAlignment(SwingConstants.TOP);
final JButton btnP = new JButton("P");
btnP.setEnabled(false);
secondRowKey.add(btnP);
btnP.setVerticalAlignment(SwingConstants.TOP);
final JButton btnQ = new JButton("Q");
btnQ.setEnabled(false);
secondRowKey.add(btnQ);
btnQ.setVerticalAlignment(SwingConstants.TOP);
final JButton btnR = new JButton("R");
btnR.setEnabled(false);
secondRowKey.add(btnR);
btnR.setVerticalAlignment(SwingConstants.TOP);
final JButton btnS = new JButton("S");
btnS.setEnabled(false);
secondRowKey.add(btnS);
btnS.setVerticalAlignment(SwingConstants.TOP);
final JButton btnT = new JButton("T");
btnT.setEnabled(false);
secondRowKey.add(btnT);
btnT.setVerticalAlignment(SwingConstants.TOP);
JPanel thirdRowKey = new JPanel();
thirdRowKey.setBounds(0, 181, 479, 41);
frmHangman.getContentPane().add(thirdRowKey);
final JButton btnU = new JButton("U");
btnU.setEnabled(false);
thirdRowKey.add(btnU);
btnU.setVerticalAlignment(SwingConstants.TOP);
final JButton btnV = new JButton("V");
btnV.setEnabled(false);
thirdRowKey.add(btnV);
btnV.setVerticalAlignment(SwingConstants.TOP);
final JButton btnW = new JButton("W");
btnW.setEnabled(false);
thirdRowKey.add(btnW);
btnW.setVerticalAlignment(SwingConstants.TOP);
final JButton btnX = new JButton("X");
btnX.setEnabled(false);
thirdRowKey.add(btnX);
btnX.setVerticalAlignment(SwingConstants.TOP);
final JButton btnY = new JButton("Y");
btnY.setEnabled(false);
thirdRowKey.add(btnY);
btnY.setVerticalAlignment(SwingConstants.TOP);
final JButton btnZ = new JButton("Z");
btnZ.setEnabled(false);
thirdRowKey.add(btnZ);
btnZ.setVerticalAlignment(SwingConstants.TOP);
final JPanel panelTries = new JPanel();
panelTries.setBackground(Color.BLACK);
panelTries.setBounds(0, 0, 101, 62);
frmHangman.getContentPane().add(panelTries);
panelTries.setVisible(false);
final JLabel lblNumberTries = new JLabel(Integer.toString(tries));
panelTries.add(lblNumberTries);
lblNumberTries.setHorizontalAlignment(SwingConstants.CENTER);
lblNumberTries.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNumberTries.setForeground(Color.WHITE);
lblNumberTries.setVisible(false);
final JLabel lblTries = new JLabel ("Tries");
panelTries.add(lblTries);
lblTries.setForeground(Color.WHITE);
lblTries.setBackground(new Color(0, 0, 0));
lblTries.setFont(new Font("Tahoma", Font.BOLD, 16));
lblTries.setVisible(false);
JMenuBar menuBar = new JMenuBar();
frmHangman.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
btnA.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'A');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnB.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'B');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnC.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'C');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnD.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'D');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnE.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'E');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnF.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'F');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnG.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'G');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnH.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'H');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnI.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'I');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnJ.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'J');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnK.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'K');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnL.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'L');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnM.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'M');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnN.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'N');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnO.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'O');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnP.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'P');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnQ.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'Q');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnR.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'R');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnS.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'S');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnT.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'T');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnU.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'U');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnV.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'V');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnW.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'W');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnX.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'X');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnY.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'Y');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnZ.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
String displayText =matchLetter(currentWord, blanks, 'Z');
lblXXX.setText(displayText);
int counter = counterUpdate(Integer.parseInt(lblNumberTries.getText()));
lblNumberTries.setText(Integer.toString(counter));
}
});
btnStop.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
if (lblXXX.getText() == "XXXXXXXXXXXXXXXX"){
StringBuffer blanks = makeBlanks(currentWord);
String displayText =matchLetter(currentWord, blanks, '-');
lblXXX.setText(displayText);
lblTries.setVisible(true);
lblNumberTries.setVisible(true);
panelTries.setVisible(true);
btnStop.setText("Stop Game");
btnA.setEnabled(true);
btnB.setEnabled(true);
btnC.setEnabled(true);
btnD.setEnabled(true);
btnE.setEnabled(true);
btnF.setEnabled(true);
btnG.setEnabled(true);
btnH.setEnabled(true);
btnI.setEnabled(true);
btnJ.setEnabled(true);
btnK.setEnabled(true);
btnL.setEnabled(true);
btnM.setEnabled(true);
btnN.setEnabled(true);
btnO.setEnabled(true);
btnP.setEnabled(true);
btnQ.setEnabled(true);
btnR.setEnabled(true);
btnS.setEnabled(true);
btnT.setEnabled(true);
btnU.setEnabled(true);
btnV.setEnabled(true);
btnW.setEnabled(true);
btnX.setEnabled(true);
btnY.setEnabled(true);
btnZ.setEnabled(true);
}
else{
lblXXX.setText("XXXXXXXXXXXXXXXX");
lblTries.setVisible(false);
btnStop.setText("Start Game");
lblNumberTries.setVisible(false);
panelTries.setVisible(false);
lblNumberTries.setText("0");
btnA.setEnabled(false);
btnB.setEnabled(false);
btnC.setEnabled(false);
btnD.setEnabled(false);
btnE.setEnabled(false);
btnF.setEnabled(false);
btnG.setEnabled(false);
btnH.setEnabled(false);
btnI.setEnabled(false);
btnJ.setEnabled(false);
btnK.setEnabled(false);
btnL.setEnabled(false);
btnM.setEnabled(false);
btnN.setEnabled(false);
btnO.setEnabled(false);
btnP.setEnabled(false);
btnQ.setEnabled(false);
btnR.setEnabled(false);
btnS.setEnabled(false);
btnT.setEnabled(false);
btnU.setEnabled(false);
btnV.setEnabled(false);
btnW.setEnabled(false);
btnX.setEnabled(false);
btnY.setEnabled(false);
btnZ.setEnabled(false);
}
}
});
}
public static StringBuffer makeBlanks(String s)
{
StringBuffer blanks = new StringBuffer(s.length());
for (int count=0; count < s.length(); count++)
blanks.append('-');
return blanks;
}
public static String matchLetter(String currentWord, StringBuffer blanks, char searchChar)
{
String newBlanks;
String thisCurrentWord = currentWord;
if(searchChar=='-'){
newBlanks = makeBlanks(thisCurrentWord).toString();
}
for (int index = 0; index < thisCurrentWord.length(); index++)
if (thisCurrentWord.charAt(index) == searchChar)
blanks.setCharAt(index, searchChar);
newBlanks = blanks.toString();
return newBlanks;
}
public static int counterUpdate(int currentNumber)
{
currentNumber += 1;
return currentNumber;
}
}