2013-12-11 62 views
1

我有一個JSwing應用程序詢問用戶一個問題,回答這個問題,然後問另一個問題。我的問題是,在回答第一個問題之後,出現第二個問題(來自actionPerformed方法),而下一個方法(檢查方法)需要將新答案分配給響應變量並開始if語句,似乎沒有初始化。這裏是完整的代碼:正在初始化另一個方法

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseAdapter; 

public class hello extends JApplet implements ActionListener{ 
JTextArea questions; 
JTextField answers; 
JPanel panel; 
String response; 

public void init(){ 

    questions = new JTextArea("Hello. State your name: ", 15, 65); 
    questions.setEditable(false); 
    questions.setLineWrap(true); 
    questions.setWrapStyleWord(true); 
    questions.setBackground(Color.black); 
    questions.setForeground(Color.green); 
    questions.setFont(new Font("Monaco", Font.PLAIN, 12)); 

    answers = new JTextField("Type here", 65); 
    answers.setBackground(Color.black); 
    answers.setForeground(Color.green); 
    answers.setFont(new Font("Monaco", Font.PLAIN, 12)); 
    answers.addActionListener(this); 

    panel = new JPanel(); 
    panel.add(questions); 
    panel.add(answers); 
    panel.setSize(480, 280); 
    panel.setBackground(Color.black); 

    getContentPane().add(panel, BorderLayout.CENTER); 

    answers.addMouseListener(new MouseAdapter(){ 
     public void mouseClicked(MouseEvent e){ 
      answers.setText(""); 
     } 
    }); 

} 
    public void actionPerformed(ActionEvent e){ 
     response = answers.getText(); 
     questions.setText("How are you " + response + "?"); 
     answers.setText(""); 
    } 
    public void checker(ActionEvent f){ 
    response = answers.getText(); 
    if(response.equals("well")){ 
    questions.setText("glad to hear it"); 
    } 
    else{ 
     questions.setText("i'm sorry to hear that"); 
    } 
    } 
    } 

任何建議將不勝感激。

回答

1

看起來你很困惑如何調用actionPerformed並創建了一個永遠不會被調用的方法checker

您已註冊您的hello類以實現ActionListener界面。
也就是說,通過在JTextField上撥打setActionListener(this)actionPerformed將在用戶按下回車鍵時被調用。

我假設你想要用戶在輸入「well」後第二次按Enter鍵,並且將調用checker。但是,您的JTextField不承認checker也不是有興趣調用它。
您可以檢查用戶是否正在回答actionPerformed方法中的第二個問題;甚至可能創建一個枚舉來檢查當前問題的狀態。

因此,像:

private enum Question { FIRST, SECOND }; 
private Question current = FIRST; //can also initiate (but not declare) in init() 
... 
public void actionPerformed(ActionEvent e) { 
    if(current == FIRST) { 
     questions.setText("How are you " + answers.getText() + "?"); 
     answers.setText(""); 
     current = SECOND; 
    } else if(current == SECOND) { 
     if(answers.getText().equals("well")) 
      questions.setText("glad to hear it"); 
     else 
      questions.setText("i'm sorry to hear that"); 
    } 
} 
+0

這似乎不起作用。同樣的事情正在發生,顯示問題的地方,並且對用戶答案的​​迴應永遠不會出現。它只是繼續替換爲JTextField輸入的內容爲「你好嗎」+ answers.getText()... – guarrana

+0

你描述的內容聽起來不太可能:如果你只輸入了一次'actionPerformed',那麼'current'就變成'SECOND',並且它不能再次將文本重置爲「你好嗎......」。除非你有'current = FIRST'的地方繼續被叫。 – itchy23

+0

經過一番擺弄之後,它似乎在工作。謝謝 – guarrana

1

[...]但接下來的方法(方格法),這是需要 分配新的答案響應變量和開始的if else語句 ,似乎沒有初始化。

那麼你永遠不會調用checker()方法在你的代碼,然後它從未執行:)

一些題外話提示:

  • checker()方法並不需要一個ActionEvent作爲參數,不是?
  • 也許您想將答案附加到文本區域,而不是覆蓋用戶每次輸入中的文本。看看JTextArea.append()的方法。
  • 關於MouseListener附加到answers,如果目標是清除文本,當用戶把焦點放在這個文本字段,那麼我建議你附加一個FocusListener並清除focusGained()方法內的文本。
1
panel = new JPanel(); 
    panel.setLayout(new BorderLayout(0, 0)); 
    panel.add(questions, BorderLayout.CENTER); 
    panel.add(answers, BorderLayout.SOUTH); 
    panel.setSize(480, 280); 
    panel.setBackground(Color.black); 

嘗試。面板的默認佈局是流佈局。而你的answersquestions太大而無法顯示。通過將佈局設置爲borderLayout可以調整它們的大小。

+0

感謝您的這個提示。 – guarrana