poketis
poketis

Reputation: 5

How can i get my program to reply with an answer

I'm working on a program that you can have a conversation with so I could ask it hello and it would reply.

But when typing in the text field I can't seem to get it to display the answer in the other text field.

here is my code so far any help is useful

public class Gui extends JFrame {

    private static final long serialVersionUID = 1L;

    private JTextField input, output;

    private String answer;
    private JPanel contentpanel;
    boolean opchosen = false;

    public Gui() {
        super("Vixen");
        input = new JTextField(null, 20);
        output = new JTextField(null, 20);

        question q = new question();

        input.addActionListener(q);

        contentpanel = new JPanel();
        contentpanel.setBackground(Color.lightGray);
        contentpanel.setLayout(new FlowLayout());

        contentpanel.add(input, BorderLayout.NORTH);
        input.setEditable(true);

        contentpanel.add(output, BorderLayout.SOUTH);
        output.setEditable(false);

        this.setContentPane(contentpanel);
    }

    private class question implements ActionListener {

        public void actionPerformed(ActionEvent Event) {
            JTextField input = (JTextField) Event.getSource();
            if (input.equals("whats you name")) {
                if (opchosen == false) {
                    if (answer == null) {
                        answer = "My name is Vixen!";
                    }
                }
            }

            if (opchosen == false) {
                output.setText(answer);
            }

        }
    }
}
}

Okay that problem is fixed but when i try to ask another question my output box wont display the new answer its just stuck on My name is Vixen

Upvotes: -1

Views: 1017

Answers (1)

trashgod
trashgod

Reputation: 205875

Use your JTextField for input only. In the text field's actionPerformed() implementation, append() the input text and the response to an adjacent JTextArea. This example illustrated the basic approach. In the example, responses come from another socket; yours will come from code that handles canned responses.

Upvotes: 1

Related Questions