在JTextArea中显示arraylist中的元素

时间:2013-12-16 03:51:59

标签: java swing user-interface arraylist jtextarea

我正在创建一个文字游戏,其中玩家必须使用给定单词中的字母尽可能多地创建单词。 (示例:给出的单词是“elephant”,所以可能的答案是喘气和帮助)。我想在文本框中显示正确的答案。现在,唯一显示的是空白文本区域。

以下是将单词放入各自的arraylists中的代码:

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

    }

1 个答案:

答案 0 :(得分:1)

正在发生的事情是,我认为您正在尝试在没有文本时从文本区域获取输入。你在构造函数中有这个

String text = inputWords.getText(); 

没有初始文本,所以不会做任何事情。

我假设你actionPerformed,你打电话给checkAnswers,所以我会提出这个建议。从构造函数中获取该代码并将其放入actionPerformed()

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