我正在开发一个程序,你可以与它进行对话,这样我就可以向它打招呼,它就会回复。
但是当在文本字段中输入内容时,我似乎无法让它在其他文本字段中显示答案。
这是我的代码,到目前为止任何帮助都是有用的
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);
}
}
}
}
}
好吧,这个问题已经解决了,但是当我尝试问另一个问题时,我的输出框不会显示新答案,它只是卡在我的名字是 Vixen
最佳答案
仅使用 JTextField
进行输入。在文本字段的 actionPerformed()
实现中,append()
将输入文本和响应添加到相邻的 JTextArea
。这个example说明了基本方法。在示例中,响应来自另一个套接字;您的响应将来自处理预设响应的代码。
关于java - 我怎样才能让我的程序回复答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27406547/