我正在創建一個文字遊戲,玩家必須使用給定單詞中的字母儘可能多地製作儘可能多的單詞。 (例如:給出的詞是「大象」,所以可能的答案會是喘氣和幫助)。我想在文本框中顯示正確的答案。現在,唯一顯示的是空白文本區域。在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);
}
}
你在哪裏叫'checkAnswers'? –