简体   繁体   中英

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:

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. Take that code from your constructor and put it into your 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.

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

Lets say you have this class 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.

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

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

Now you can use those answers from your other class to compare.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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