2013-05-20 119 views
0

這裏的麻煩很大。Hangman GUI小程序java

  1. 的errorLabel不能顯示errorCounter,而不是javax.swing.JLabel中的[0.670 ...點擊新遊戲出現
  2. ,鍵盤復位,但不是wordLabel(這把新單詞爲遊戲),如何刪除舊的wordLabel並用新的替換?

這裏是我的代碼:

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); 
    } 
} 

} 
+1

錯誤#1使用空佈局 – MadProgrammer

+1

好吧,我放棄了。我無法知道這裏發生了什麼 - 沒有任何評論,沒有任何在代碼前的文本中查詢過「wordLabel」,代碼在按下按鈕後設置了佈局管理器 - 一天中只有很多時間。 。 – arcy

+0

爲什麼'計算機科學'標籤?!? –

回答

2

有一些問題,但你似乎有你周圍牯for環......

這...

主要原因之一
for (int i = 0; i <= t.length(); i++) { 

這...

for (int i = 0; i <= letterLabel.length; i++) { 

這兩個都會中斷並拋出ArrayIndexOutOfBoundsException異常。請記住,Java中的所有數組均基於0的數組。即,與長度爲5的陣列將在0-4

reset你方法應被自包含和能夠提供用於重建letterLabel陣列具有元素。這種方法也應該從init方法中調用來啓動並運行遊戲。

你「猜檢查」的算法是錯誤的...

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"); 
    } 
} 

這將基本上,減少原文每個字符猜測的數目。所以,如果原文是「testin」,我選擇a,我將不會再有猜測了。猜測應該只會減少,如果在原始文本中找不到任何匹配...