Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何删除由jtextfield创建的附加行_Java_Swing - Fatal编程技术网

Java 如何删除由jtextfield创建的附加行

Java 如何删除由jtextfield创建的附加行,java,swing,Java,Swing,上面的一个在GUI jtextarea JFrame中,如何删除“AI:你好!”后面的最后一个“[You]hi”?只有一个“嗨”是我写的jtextfield,但它回答了2个“嗨”。或者对于我来说,有没有一种更简单的方法来编写一个可以选择多个其他类“函数”的代码? 下面是我的代码 AI: Enter 1 to add question to chatbot. AI: Enter 2 to start chatting with bot. AI: Enter 3 to end chatbot. [Y

上面的一个在GUI jtextarea JFrame中,如何删除“AI:你好!”后面的最后一个“[You]hi”?只有一个“嗨”是我写的jtextfield,但它回答了2个“嗨”。或者对于我来说,有没有一种更简单的方法来编写一个可以选择多个其他类“函数”的代码? 下面是我的代码

AI: Enter 1 to add question to chatbot.
AI: Enter 2 to start chatting with bot.
AI: Enter 3 to end chatbot.
[You] 1
[You] hi
AI: Hello there!
[You] hi

你的问题有几个原因

首先,您必须两次声明
userinput.addActionListener
,这会导致每当文本框提交值时调用
textarea()
方法两次

其次,在
chatfunction()
方法中,
ActionListener
的末尾不重置
userinput
。这就是为什么它会将原始输入打印两次

最后,每当调用
textarea()
方法时,您都会检索userinput的值并将其附加到聊天中,不管它是什么。考虑添加if语句以首先检查是否有要追加的输入。e、 g.
if(!stringinput.equals(“”)scriptarea.append(“[You]”+stringinput+”\n”)

正如Pshemo上面所说的,代码流中还有其他问题需要解决,即从另一个内部调用新的
ActionListener

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class GUItest2 extends JFrame {

    private JTextField userinput = new JTextField();
    private JTextArea scriptarea = new JTextArea();
    private String stringinput;

    public GUItest2() {
        // Frame Attributes:
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 600);
        this.setVisible(true);
        this.setResizable(false);
        this.setLayout(null);
        this.setTitle("Java AI");
        setLocationRelativeTo(null);

        userinput.setLocation(2, 540);
        userinput.setSize(590, 30);

        scriptarea.setLocation(15, 5);
        scriptarea.setSize(560, 510);
        scriptarea.setEditable(false);

        this.add(userinput);
        this.add(scriptarea);

        selectionfunction();
        // chatfunction();
    }

    public void textarea() {
        // userinput.addActionListener(new ActionListener(){
        // public void actionPerformed(ActionEvent arg0){
        stringinput = userinput.getText();
        scriptarea.append("[You] " + stringinput + "\n");
        // }
        // });
    }

    public void selectionfunction() {
        botreply("Enter 1 to add question to chatbot.");
        botreply("Enter 2 to start chatting with bot.");
        botreply("Enter 3 to end chatbot.");
        userinput.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textarea();
                if (stringinput.contains("1")) {
                    chatfunction();
                }
                userinput.setText("");
            }
        });
    }

    public void chatfunction() {
        userinput.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textarea();
                if (stringinput.contains("hi")) {
                    botreply("Hello there!");
                } else if (stringinput.contains("how are you")) {
                    int decider = (int) (Math.random() * 2 + 1);
                    if (decider == 1) {
                        botreply("I'm doing well, thanks");
                    } else if (decider == 2) {
                        botreply("Not too bad");
                    }
                } else {
                    int decider = (int) (Math.random() * 3 + 1);
                    if (decider == 1) {
                        botreply("I didn't get that");
                    } else if (decider == 2) {
                        botreply("Please rephrase that");
                    } else if (decider == 3) {
                        botreply("???");
                    }
                }
            }
        });
    }

    public void botreply(String s) {
        scriptarea.append("AI: " + s + "\n");
    }

    public static void main(String[] args) {
        new GUItest2();
    }

}