2009-11-29 130 views
0

我對這個程序有幾個問題,我想要做的第一件事是讓它可以比較並查看文本字段是否等於colorValues [x]位置。第二個問題是if語句說如果inText ==爲colorValues.length - 1打開一個框,表示congradulations不起作用。第三個問題,即使它得到抱歉消息和或congradulations消息你如何使它的文本字段不顯示?簡單的FlowLayOut幫助

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

    public class AlbertCardonaProg7 extends JFrame 
{ 
    private static final int WIDTH = 350; 
    private static final int HEIGHT = 250; 
    private static final String[] colorValues = {"red","white", 
"yellow","green","blue"};// I dentifies the colors 
    private JTextField nameBox; 
    private JLabel greeting; 
    private String[] message = {"Input color number 1", 
"Input color number 2: ","Input color number 3: " 
    ,"Input color number 4:","Input color number 5:"}; 
    private JLabel namePrompt = new JLabel(this.message[0]); 

    public AlbertCardonaProg7() 
    { 
    setTitle("MEMORY GAME"); 
    setSize(WIDTH, HEIGHT); 
    setLayout(new FlowLayout(FlowLayout.CENTER)); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    createContents(); 
    setVisible(true); 
    }// end constructor 
    //****************************************** 
private void createContents() 
{ 
    nameBox = new JTextField(15); 
    greeting = new JLabel(); 
    add(namePrompt); 
    add(nameBox); 
    add(greeting); 
    nameBox.addActionListener(new Listener()); 
    }//end createContents 

//************************************************ 
private class Listener implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
    int inText; 
    for(inText =0; inText < 5; inText++) 
    { 
     if(nameBox.getText().equals(colorValues[inText])) 
    { 
    namePrompt.setText(message[inText]); // its not working trying 
     //to see if it is equal to the proper spot 
     //in the colorValues[array] 

     add(nameBox); 
     nameBox.setText(""); 
     nameBox.requestFocus(); 
     inText++; 
     } 

     if(!nameBox.getText().equals(colorValues[inText])) 
      { 
      AlbertCardonaProg7 darn = new AlbertCardonaProg7(); 
      darn.namePrompt.setText("Sorry, drink more Ginseng "); 

      add(namePrompt); 
      break; 
      } 

     if(inText == (colorValues.length -1)) 
     { 
     AlbertCardonaProg7 darn = new AlbertCardonaProg7(); 
     darn.namePrompt.setText("Congradulations, 
      Your mind is Awesome!!!"); 

      add(namePrompt); 
      break; 
     } 

     }// loop 
    }//end action performed 
    }// end class Listener 

    //************************************** 
    public static void main(String[] args) 
    { 
     String colors = ""; 
     for(int i = 0; i < colorValues.length; i++) 
     colors += colorValues[i] + " "; 
     JOptionPane.showMessageDialog(null,"How good is your memory.\n 
     See if you can memorize this sequence.\n\n" + colors, 
     "Message", JOptionPane.INFORMATION_MESSAGE); 

     AlbertCardonaProg7 outBox = new AlbertCardonaProg7(); 

     }// end main class 
     }//end Class AlberCardonaProg7 
+0

我來編輯它使每個人都可以看到所有的代碼並且可以給我一些我應該做的步驟的建議。 – daddycardona 2009-11-30 00:15:24

+0

有人可以幫我用我的循環? – daddycardona 2009-11-30 01:56:30

回答

0

首先,我建議你學會正確地設置你的代碼的格式,因爲它可以更容易地看到發生了什麼事情。

其次,您發佈的代碼在第64行和第80行有語法錯誤,導致編譯失敗。問題在於Java不允許在源代碼中包含多行字符串文字,因此您必須將兩個字符串連接在一起。例如:

darn.namePrompt.setText("Congradulations, 
    Your mind is Awesome!!!"); 

應該是:

darn.namePrompt.setText("Congradulations," + 
    " Your mind is Awesome!!!"); 

現在,不幸的是你的問題並不能使它特別清楚該方案的預期的行爲應該是什麼。我對此的解釋是,您希望爲用戶提供文本框,要求他們輸入第一種顏色,然後顯示一個對話框,表示祝賀或對不起,具體取決於他們是否答對了答案。如果他們得到的答案是正確的,那麼你想顯示第二種顏色的輸入框,檢查答案等。

我的第一個建議是在實例化JFrame時創建所有控件,但只是隱藏直到用戶輸入正確的值爲止。接下來,我建議你在進入和編寫代碼之前計劃動作監聽者要做的事情。

在這種情況下,程序需要存儲用戶當前正在處理的輸入字段的數組索引。偵聽器需要檢查這個變量,並驗證inputFields數組中的相應字段。聽衆需要向用戶顯示一個對話框,說明他們是否得到了正確的答案,如果用戶確實如此,則啓用下一個輸入字段。

全部放在一起,你會得到:

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 

public class AlbertCardonaProg7 extends JFrame { 
    private static final int WIDTH = 350; 
    private static final int HEIGHT = 250; 
    private static final String[] colorValues = { "red", "white", "yellow", 
     "green", "blue" }; 

    private final JLabel[] inputLabels = new JLabel[colorValues.length]; 
    private final JTextField[] inputFields = new JTextField[colorValues.length]; 
    private int index = 0; 

    public AlbertCardonaProg7() { 
     //Create the UI controls 
     for (int i = 0; i < colorValues.length; i++) { 
      inputLabels[i] = new JLabel("Input color number " + i + ":"); 
      inputLabels[i].setVisible(false); 
      inputFields[i] = new JTextField(15); 
      inputFields[i].setVisible(false); 
      inputFields[i].addActionListener(new Listener()); 

      add(inputLabels[i]); 
      add(inputFields[i]); 
     } 

     //Make the first set visible 
     inputLabels[0].setVisible(true); 
     inputFields[0].setVisible(true); 

     setTitle("MEMORY GAME"); 
     setSize(WIDTH, HEIGHT); 
     setLayout(new FlowLayout(FlowLayout.CENTER)); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    private class Listener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      if (inputFields[index].getText().equals(colorValues[index])) { 
       JOptionPane.showMessageDialog(null, "Congratulations, you got the answer correct"); 

       //See if there are more controls to make visible 
       if (++index < colorValues.length) { 
        inputLabels[index].setVisible(true); 
        inputFields[index].setVisible(true); 
       } 
      } else { 
       JOptionPane.showMessageDialog(null, 
         "Sorry, your answer is wrong", "Error", 
         JOptionPane.ERROR_MESSAGE); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     String colors = ""; 
     for (int i = 0; i < colorValues.length; i++) { 
      colors += colorValues[i] + " "; 
     } 

     JOptionPane.showMessageDialog(null, "How good is your memory.\n" 
       + "See if you can memorize this sequence.\n\n" + colors, 
       "Message", JOptionPane.INFORMATION_MESSAGE); 

     AlbertCardonaProg7 outBox = new AlbertCardonaProg7(); 

    } 
} 

編輯:根據您的評論如下,我已經修改了方案,以滿足預期的行爲。主要的變化是構造不再隱藏其他控件,與聽者現在有超過每個輸入場循環,以檢查它們是正確的:

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 

public class AlbertCardonaProg7 extends JFrame { 
    private static final int WIDTH = 350; 
    private static final int HEIGHT = 250; 
    private static final String[] colorValues = { "red", "white", "yellow", 
     "green", "blue" }; 

    private final JLabel[] inputLabels = new JLabel[colorValues.length]; 
    private final JTextField[] inputFields = new JTextField[colorValues.length]; 

    public AlbertCardonaProg7() { 
     //Create the UI controls 
     for (int i = 0; i < colorValues.length; i++) { 
      inputLabels[i] = new JLabel("Input color number " + i + ":"); 
      inputFields[i] = new JTextField(15); 
      inputFields[i].addActionListener(new Listener()); 

      add(inputLabels[i]); 
      add(inputFields[i]); 
     } 

     setTitle("MEMORY GAME"); 
     setSize(WIDTH, HEIGHT); 
     setLayout(new FlowLayout(FlowLayout.CENTER)); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    private class Listener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

      // See if there are any wrong answers 
      boolean correct = true; 
      for(int i = 0; i < colorValues.length; i++) { 
       if (!inputFields[i].getText().equals(colorValues[i])) { 
        correct = false; 
       } 
      } 

      if(correct) { 
       JOptionPane.showMessageDialog(null, 
         "Congratulations, you got the answer correct"); 
      } else { 
       JOptionPane.showMessageDialog(null, 
         "Sorry, your answer is wrong", "Error", 
         JOptionPane.ERROR_MESSAGE); 

      } 
     } 
    } 

    public static void main(String[] args) { 
     String colors = ""; 
     for (int i = 0; i < colorValues.length; i++) { 
      colors += colorValues[i] + " "; 
     } 

     JOptionPane.showMessageDialog(null, "How good is your memory.\n" 
       + "See if you can memorize this sequence.\n\n" + colors, 
       "Message", JOptionPane.INFORMATION_MESSAGE); 

     AlbertCardonaProg7 outBox = new AlbertCardonaProg7(); 

    } 
} 
+0

非常感謝你,我確實瞭解這個連接,因爲它沒有在我的電腦上用這個網站正確顯示,所以我只用了一行。再次非常感謝你的回答,我真的很感激。 – daddycardona 2009-12-07 20:18:45

+0

這是關於我要去的東西,我希望他們記住所有的顏色,然後如果他們把它們都弄好,然後說,「你是正確的偉大的工作!」 如果錯誤的話,「你錯過了,但這是非常棒的。」 – daddycardona 2009-12-07 23:27:28

+0

啊我明白了。您應該很可能能夠看到需要更改的內容,但我將使用您要查找的代碼進行編輯。 – 2009-12-08 09:44:59