简体   繁体   English

在JTextArea的arraylist中显示元素

[英]Display elements in an arraylist in a JTextArea

I'm creating a word game in which the player must make as many words as possible using the letters in the word given. 我正在创建一个文字游戏,玩家必须使用给定单词中的字母制作尽可能多的单词。 (Example: The word given is "elephant" so possible answers would be pant and help). (示例:给定的单词是“大象”,因此可能的答案是喘气和帮助)。 I want to display the right answers into a textbox. 我想在文本框中显示正确的答案。 Right now, the only thing that displays are the blank text areas. 现在,唯一显示的是空白文本区域。

Here is the code that puts the words into their respective 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()); 
    }  

Here is the code for the GUI: 这是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);
}   

    }

What's happening is that I think you are trying to get the input from the text area when there is no text. 发生的事情是,我认为您正在尝试在没有文本的情况下从文本区域获取输入。 You have this in your constructor 您的构造函数中有这个

String text = inputWords.getText(); 

There is no initial text, so That will do nothing. 没有初始文本,因此将无济于事。

I'm assuming in your actionPerformed , is where you call the checkAnswers , so I'll offer this suggestion. 我假设在您的actionPerformed ,您将其checkAnswers ,因此我将提供此建议。 Take that code from your constructor and put it into your actionPerformed() 从构造函数中获取该代码,并将其放入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();
}

What happens when you have it in the constructor is when the program first starts, it will try and read an empty text area. 当您在构造函数中拥有它时,会在程序首次启动时发生操作,它将尝试读取一个空白文本区域。 Now when you have it in the actionPerformed, it won't try to read it until the button is pressed. 现在,当您将其放入actionPerformed中时,直到按下按钮,它才会尝试读取它。

Edit: answers are coming from a different class (as stated in OP comment) 编辑:答案来自不同的类(如OP注释中所述)

Lets say you have this class Answers 比方说,你有这个类Answers

public class Answers {
    private ArrayList<String> answers;

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

In your checkAnswers() , you should create an instance of that class inside the method so that you can reference those answer. 在您的checkAnswers() ,应该在方法内部创建该类的实例,以便可以引用这些答案。

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

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

Now you can use those answers from your other class to compare. 现在,您可以使用其他班级的答案进行比较。

Edit 2: 编辑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
           }
       });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM