2013-12-16 61 views
2

我正在創建一個文字遊戲,玩家必須使用給定單詞中的字母儘可能多地製作儘可能多的單詞。 (例如:給出的詞是「大象」,所以可能的答案會是喘氣和幫助)。我想在文本框中顯示正確的答案。現在,唯一顯示的是空白文本區域。在JTextArea中的陣列列表中顯示元素

這裏是放話到各自的ArrayList代碼:

public void checkAnswers() { 
    ArrayList<String> validAnswers = new ArrayList<String>(); 
    ArrayList<String> wrongAnswers = new ArrayList<String>(); 
    ArrayList<String> notFound = new ArrayList<String>(); 
    List<String> compare = new ArrayList<String>(); 

    if (r == 0){ 
    compare = arr1Sub; 
    } 
    else if (r == 1){ 
      compare = arr2Sub; 
    } 
    else if(r == 2){ 
      compare = arr3Sub; 
    } 
    else{ 
      compare.add("error"); 
      System.out.println(compare); 
    } 
    for (int i = 0; i < inputList.size(); i++){ 
     if (compare.contains(inputList.get(i))){ 
       validAnswers.add(inputList.get(i)); 
     } 
     else if (!compare.contains(inputList.get(i))){ 
       wrongAnswers.add(inputList.get(i)); 
     } 
     else{ 
       notFound.add(compare.get(i)); 
     } 

    for(String s : validAnswers){ 
     txtvalidWords.append(s.toString()); 
    } 

下面是GUI代碼:我認爲你正在試圖獲得

public void CheckAnswersGUI(){ 
    JPanel answersPanel = new JPanel(); 
    JPanel wrongPanel = new JPanel(); 
    JPanel possiblePanel = new JPanel(); 
    container = new JPanel(); 

    lblvalid = new JLabel("Valid Answers"); 
    txtvalidWords = new JTextArea("",30,30); 
    lblwrong = new JLabel("Wrong Answers"); 
    txtwrongWords = new JTextArea("",30,30); 
    lblpossible = new JLabel("Answers Not Found"); 
    txtpossibleWords = new JTextArea("",30,30); 

    btnPlayAgain = new JButton("Play Again"); 
    btnPlayAgain.addActionListener(this); 

    FlowLayout checkLayout = new FlowLayout(); 
    answersPanel.setLayout(checkLayout); 
    wrongPanel.setLayout(checkLayout); 
    possiblePanel.setLayout(checkLayout); 

    container.setLayout(new BoxLayout(container,BoxLayout.X_AXIS)); 
    container.add(answersPanel); 
    container.add(wrongPanel); 
    container.add(possiblePanel); 
    container.add(btnPlayAgain); 

    inputList = new ArrayList<String>(); 
    String text = inputWords.getText(); 
    String[] words = text.split("\\s"); 

    for (String word : words){ 
     inputList.add(word); 
    } 

    answersPanel.add(lblvalid); 
    answersPanel.add(txtvalidWords); 
    wrongPanel.add(lblwrong); 
    wrongPanel.add(txtwrongWords); 
    possiblePanel.add(lblpossible); 
    possiblePanel.add(txtpossibleWords); 
    getContentPane().add(container); 

    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    setLocationRelativeTo (null); 
    setSize (700,500); 
    setTitle ("Check Your Answers"); 
    setVisible(true); 
} 

    } 
+0

你在哪裏叫'checkAnswers'? –

回答

1

發生了什麼事是當沒有文本時,來自文本區域的輸入。你在你的構造函數中有這個

String text = inputWords.getText(); 

沒有初始文本,所以那什麼都不會做。

我假設在您的actionPerformed,是您撥打checkAnswers的地方,所以我會提供這個建議。從你的構造函數的代碼,並把它變成你的actionPerformed()

pubic void actionPerformed(ActionEvent e){ 
    inputList = new ArrayList<String>(); 
    String text = inputWords.getText(); 
    String[] words = text.split("\\s"); 

    for (String word : words){ 
     inputList.add(word); 
    } 

    // then call the check answers. 
    checkAnswers(); 
} 

當你在構造函數中有它發生的是第一次啓動程序時,它會嘗試讀取一個空的文本區域。現在,當你在actionPerformed中使用它時,它將不會嘗試讀取它,直到按下按鈕。

編輯:答案是從不同的類來(如OP註釋說明)

比方說你有這個類Answers

public class Answers { 
    private ArrayList<String> answers; 

    public ArrayList<String> getAnswers(){ 
     return answers; 
    } 
} 

在你checkAnswers(),你應該創建一個實例該方法內的類,以便您可以引用這些答案。

public void checkAnswer(){ 
    Answers ans = new Answers(); 

    ArrayList<String> answers = ans.getAnswer(); 
} 

現在,您可以使用您的其他班級的答案進行比較。

編輯2:

public class PlayGame { 
    private ArrayList<String> validAnswers = new ArrayList<String>(); 
    private ArrayList<String> wrongAnswers = new ArrayList<String>(); 
    private ArrayList<String> notFound = new ArrayList<String>(); 

    public ArrayList<String> getValidAnswers(){ 
     return validAnswers; 
    } 

    public ArrayList<String> getWrongAnswers(){ 
     return wringAnswers; 
    } 

    public ArrayList<String> getNotFound(){ 
     return notFound; 
    } 

    public void checkAnswers(String text) { 
     // get text from texArea and use that text 
     // I'm guess to populate your compare list 
    } 
} 

public class GUI extends JPanel { 
    private JTextArea inputTextArea = new JTextArea(); 
    JButton checkAnswers = new JButton("Check Answers"); 

    PlayGame game = new PlayGame(); 

    public GUI { 
     ... 

     checkAnswers.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       String text = inputTextArea.getText(); 

       game.checkAnswers(text); 

       ArrayList<String> validAnswers = game.getValidAnswers(); 
       ArrayList<String> wrongAnswers = game.getWrongAnswers(); 
       ArrayList<String> notFound = game.getNotFound(); 

       // code to append these lists to their corresponding text areas 
      } 
     }); 
    } 
} 
+0

謝謝!但是,我仍然無法顯示文字。現在,我在一個課堂上得到了答案,然後在另一個課堂上看到了GUI。我無法準確計算出如何將inputList與其他類中的answers arrayList進行比較,然後只顯示正確的答案。 – user2898681

+0

你能發佈信息嗎?你完成代碼。 –

+0

我認爲在你的'checkAnswer()'你應該創建一個你的類的實例有答案。然後在方法裏面引用那些列表 –