Advertisement
TermSpar

Chatbot Tutorial

Oct 4th, 2016
9,685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JTextArea;
  6. import javax.swing.JTextField;
  7.  
  8.  
  9. public class Bot extends JFrame {
  10.  
  11.     //Typing Area:
  12.     private JTextField txtEnter = new JTextField();
  13.    
  14.     //Chat Area:
  15.     private JTextArea txtChat = new JTextArea();
  16.    
  17.     public Bot() {
  18.         //Frame Attributes:
  19.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20.         this.setSize(600, 600);
  21.         this.setVisible(true);
  22.         this.setResizable(false);
  23.         this.setLayout(null);
  24.         this.setTitle("Java AI");
  25.        
  26.         //txtEnter Attributes:
  27.         txtEnter.setLocation(2, 540);
  28.         txtEnter.setSize(590, 30);
  29.        
  30.         //txtEnter Action Event:
  31.         txtEnter.addActionListener(new ActionListener(){
  32.             public void actionPerformed(ActionEvent arg0) {
  33.                 String uText = txtEnter.getText();
  34.                 txtChat.append("You: " + uText + "\n");
  35.                
  36.                 if(uText.contains("hi")){
  37.                     botSay("Hello there!");
  38.                 }
  39.                 else if(uText.contains("how are you")){
  40.                     int decider = (int) (Math.random()*2+1);
  41.                     if(decider == 1){
  42.                         botSay("I'm doing well, thanks");
  43.                     }
  44.                     else if(decider == 2){
  45.                         botSay("Not too bad");
  46.                     }
  47.                 }
  48.                 else{
  49.                     int decider = (int) (Math.random()*3+1);
  50.                     if(decider == 1){
  51.                         botSay("I didn't get that");
  52.                     }
  53.                     else if(decider == 2){
  54.                         botSay("Please rephrase that");
  55.                     }
  56.                     else if(decider == 3){
  57.                         botSay("???");
  58.                     }
  59.                 }
  60.                 txtEnter.setText("");
  61.             }
  62.         });
  63.        
  64.         //txtChat Attributes:
  65.         txtChat.setLocation(15, 5);
  66.         txtChat.setSize(560, 510);
  67.         txtChat.setEditable(false);
  68.        
  69.         //Add Items To Frame:
  70.         this.add(txtEnter);
  71.         this.add(txtChat);
  72.     }
  73.    
  74.     public void botSay(String s){
  75.         txtChat.append("AI: " + s + "\n");
  76.     }
  77.    
  78.     public static void main(String[] args){
  79.         new Bot();
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement