It seems you are confused as to how actionPerformed
is being called and created a checker
method that never really gets called.
You've registered your hello
class to implement the ActionListener
interface.
That is, by calling setActionListener(this)
on your JTextField
, actionPerformed
will be called when the user presses enter.
I'm assuming that you want the user to press enter a second time after entering "well" for instance and that checker
will be called. However, your JTextField
doesn't recognize checker
nor is it interested in calling it.
You could check to see if the user is answering the second question in your actionPerformed
method; perhaps even create an enum to check the state of the current question.
So something like:
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");
}
}