[英]Hangman GUI applet java
Big trouble here. 这里很麻烦。
here's my code: 这是我的代码:
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.applet.Applet;
import java.util.*;
public class Hangman extends JApplet implements ActionListener
{
private static final long serialVersionUID = 2005L;
private Container window;
private JButton restart;
private JLabel errorLabel;
private WordList aWordList;
private int errorsCounter;
private JLabel[] letterLabel;
private ArrayList<JButton> letterButton;
private JPanel keyboard;
private JPanel wordPanel;
private int noWordsCorrect;
private boolean gameOver;
public void init()
{
window = new DoubleBufferedPanel();
setContentPane(window);
readWordList("words.txt");
createAppearance();
createGUI();
//createAlphabets();
// JOptionPane.showMessageDialog(this, "" + aWordList.size());
}
public void createAppearance(){
window.setLayout(null);
}
public void createGUI()
{
errorsCounter = 6;
gameOver = false;
window.removeAll();
createAlphabets();
findWord();
restart = new JButton("New Game");
restart.setLocation(50, 50);
restart.setSize(100, 50);
restart.addActionListener(this);
window.add(restart);
errorLabel = new JLabel("6 errors to go");
errorLabel.setSize(150, 50);
errorLabel.setLocation(670, 70);
window.add(errorLabel);
//testLabel = new JLabel("word");
//testLabel.setSize(400, 30);
//testLabel.setLocation(50, 120);
//window.add(testLabel);
//createAlphabets();
//findWord();
}
public void readWordList(String fileName){
try {
aWordList = new WordList(new URL(getCodeBase(), fileName), 7, 13);
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.toString());
}
}
public void reset(){
for(JButton b : letterButton){
b.setVisible(true);
}
if(letterLabel != null)
{
for(JLabel lbl : letterLabel)
{
wordPanel.remove(lbl);
}
wordPanel.invalidate();//signal java got to fix n redo layout
wordPanel.validate();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == restart)
{
reset();
String t = aWordList.getRandomWord();
wordPanel.setLayout(new GridLayout(1, t.length()));
JOptionPane.showMessageDialog(null, t);
letterLabel = new JLabel[t.length()];
for(int i = 0; i <= t.length(); i++)
{
letterLabel[i] = new JLabel(t.substring(i , i + 1));
letterLabel[i].setVisible(false);
wordPanel.add(letterLabel[i]);
}
}
else if(e.getSource() instanceof JButton)
{
JButton letter = (JButton) e.getSource();
for(int i = 0; i <= letterLabel.length; i++)
{
if((letterLabel[i].getText()).equals(letter.getText()))
{
letterLabel[i].setVisible(true);
letter.setVisible(false);
noWordsCorrect++;
}
else
{
letter.setVisible(false);
errorsCounter--;
errorLabel.setText("" + errorsCounter + " error to go");
}
}
}
else if(noWordsCorrect >= letterLabel.length)
{
JOptionPane.showMessageDialog(null, "Congratz you win!");
//createGUI();
gameOver = true;
}
else if(errorsCounter == 0)
{
JOptionPane.showMessageDialog(null, "OMG you lost!");
gameOver = true;
}
repaint();
}
public void createAlphabets(){
keyboard = new JPanel(new GridLayout(2,13));
keyboard.setSize(750,150);
keyboard.setBorder(BorderFactory.createLineBorder (Color.blue, 2));
keyboard.setLocation(200,200);
letterButton = new ArrayList<JButton>();
for(char c = 'a'; c <= 'z'; c++)
{
JButton s = new JButton("" + c);
s.addActionListener(this);
keyboard.add(s);
letterButton.add(s);
}
window.add(keyboard);
//setVisible(true);
}
public void findWord(){
wordPanel = new JPanel(new GridLayout(1, 13));
wordPanel.setSize(250,150);
wordPanel.setBorder(BorderFactory.createLineBorder (Color.red, 2));
wordPanel.setLocation(200,50);
window.add(wordPanel);
}
// JOptionPane.showMessageDialog(this, "[" + t + "]");
//testLabel.setText(t);
class DoubleBufferedPanel extends JPanel {
private static final long serialVersionUID = 44L;
public void paint(Graphics g){
super.paint(g);
}
}
}
There are a number of problems, but the main one you seem to be having revolves around you for
loops... 有许多问题,但你似乎围绕着你的主要问题
for
循环......
This... 这个...
for (int i = 0; i <= t.length(); i++) {
and this... 和这个...
for (int i = 0; i <= letterLabel.length; i++) {
Will both break and throw ArrayIndexOutOfBoundsException
exceptions. 两者都会破坏并抛出
ArrayIndexOutOfBoundsException
异常。
Remember, all arrays in Java are 0 based. 请记住,Java中的所有数组都是基于0的。 That is, an array with a length of 5 will have elements at
0-4
也就是说,长度为5的数组将具有
0-4
元素
Your reset
method should be self contained and capable for rebuilding the letterLabel
array. 您的
reset
方法应该是自包含的,并且能够重建letterLabel
数组。 This method should also be called from the init
method to get the game up and running. 还应该从
init
方法调用此方法以启动和运行游戏。
Your "guess checking" algorithm is wrong... 你的“猜测”算法错了......
for (int i = 0; i < letterLabel.length; i++) {
if ((letterLabel[i].getText()).equals(letter.getText())) {
letterLabel[i].setVisible(true);
letter.setVisible(false);
noWordsCorrect++;
} else {
letter.setVisible(false);
errorsCounter--;
System.out.println("errorsCounter = " + errorsCounter);
errorLabel.setText("" + errorsCounter + " error to go");
}
}
This will, basically, reduce the number of guess per character in the original text. 这基本上会减少原始文本中每个字符的猜测次数。 So, if the original text is "testin" and I select
a
, I will have no more guesses left. 因此,如果原始文本是“testin”并且我选择
a
,那么我将不再有猜测。 Guesses should only be reduced if no match at all can be found in the original text... 只有在原始文本中找不到匹配项时,才应该减少猜测...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.