使代码从执行的同一位置继续

时间:2018-08-11 11:14:25

标签: java swing jbutton

    //boutton
    ent.setBounds(490,400,100,50);      
    ent.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {   
            String text;
            text=chatbox.getText().toLowerCase();
            chatarea.append("\t\t\t\t"+text+" <- YOU \n");
            chatbox.setText("");
            if(text.contains("hii") || text.contains("hey") ||  text.contains("hi"))
            {
                bot("hey there");
                bot("how can i help you?");
            }
            else if(text.contains("info") || text.contains("help") || text.contains("i need information") || text.contains("help me"))
            {
                bot("sure i am here to help you");
                bot("what you want to know about?");
                bot("1 info about anything?");
                bot("2 info about place?");
                bot("enter your choice : ");
                if(text.contains("1")|| text.contains("info about anything") || text.contains("number 1") || text.contains("first one") || text.contains("first"))
                {
                    bot("sure which blood group info you are looking for?");
                }else if(text.contains("2") || text.contains("info about place") || text.contains("number 2") || text.contains("second one") || text.contains("second"))
                else
                {
                    bot("I Don't Understand you");
                }
            }
        }
    });
}
private void bot(String string)
{
    chatarea.append("BOT -> "+string+"\n");
}
public static void main(String[] args)
{
    new bot();
}
} 

我正在制作一个聊天机器人,当我单击JButton时,它将从JTextField中获取值并将其发布到JTextArea上。但是问题是我希望我的代码继续执行而不是停止,但是每次单击按钮时,它就会从头开始。

当我第二次单击按钮时,如何使代码从同一位置继续执行?

1 个答案:

答案 0 :(得分:0)

在您的bot类中添加一个布尔成员以跟踪状态(第一次/不是第一次)并将其初始化为true

boolean isFirstTimeButtonWasPressed = true

public void actionPerformed(ActionEvent e) {
    if (isFirstTimeButtonWasPressed) {
        //do stuff that should only happens the firs time   
        //...

        isFirstTimeButtonWasPressed = false;
    } 
    //do stuff that should be done every time the button is pressed
});