|
| 1 | +package ChatBot; |
| 2 | + |
| 3 | +import java.awt.Adjustable; |
| 4 | +import java.awt.BorderLayout; |
| 5 | +import java.awt.EventQueue; |
| 6 | + |
| 7 | +import javax.swing.JFrame; |
| 8 | +import javax.swing.JPanel; |
| 9 | +import javax.swing.border.EmptyBorder; |
| 10 | +import javax.swing.JTextField; |
| 11 | +import javax.swing.JButton; |
| 12 | +import java.awt.Font; |
| 13 | +import javax.swing.JEditorPane; |
| 14 | +import java.awt.event.ActionListener; |
| 15 | +import java.awt.event.ActionEvent; |
| 16 | +import java.awt.event.KeyAdapter; |
| 17 | +import java.awt.event.KeyEvent; |
| 18 | +import java.util.Random; |
| 19 | + |
| 20 | +import javax.swing.JTextArea; |
| 21 | +import javax.swing.JScrollBar; |
| 22 | +import java.awt.event.AdjustmentListener; |
| 23 | +import java.awt.event.AdjustmentEvent; |
| 24 | + |
| 25 | +public class ChatBotMain extends JFrame { |
| 26 | + |
| 27 | + private JPanel contentPane; |
| 28 | + private JTextField textField_1; |
| 29 | + private JButton btnSend; |
| 30 | + private String main=""; |
| 31 | + JTextArea textArea; |
| 32 | + private JScrollBar scrollBar; |
| 33 | + |
| 34 | + /** |
| 35 | + * Launch the application. |
| 36 | + */ |
| 37 | + public static void main(String[] args) { |
| 38 | + EventQueue.invokeLater(new Runnable() { |
| 39 | + public void run() { |
| 40 | + try { |
| 41 | + ChatBotMain frame = new ChatBotMain(); |
| 42 | + frame.setVisible(true); |
| 43 | + } catch (Exception e) { |
| 44 | + e.printStackTrace(); |
| 45 | + } |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Create the frame. |
| 52 | + */ |
| 53 | + |
| 54 | + public ChatBotMain() { |
| 55 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 56 | + setResizable(false); |
| 57 | + setBounds(100, 100, 428, 657); |
| 58 | + contentPane = new JPanel(); |
| 59 | + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); |
| 60 | + setContentPane(contentPane); |
| 61 | + contentPane.setLayout(null); |
| 62 | + |
| 63 | + |
| 64 | + textField_1 = new JTextField(); |
| 65 | + textField_1.addKeyListener(new KeyAdapter() { |
| 66 | + @Override |
| 67 | + public void keyPressed(KeyEvent e) { |
| 68 | + if(e.getKeyCode()==KeyEvent.VK_ENTER) |
| 69 | + { |
| 70 | + action(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + }); |
| 75 | + textField_1.setFont(new Font("Tahoma", Font.PLAIN, 23)); |
| 76 | + textField_1.setBounds(0, 555, 338, 60); |
| 77 | + contentPane.add(textField_1); |
| 78 | + textField_1.setColumns(10); |
| 79 | + btnSend = new JButton("Send"); |
| 80 | + btnSend.addActionListener(new ActionListener() { |
| 81 | + public void actionPerformed(ActionEvent e) { |
| 82 | + action(); |
| 83 | + } |
| 84 | + }); |
| 85 | + btnSend.setBounds(341, 555, 75, 59); |
| 86 | + contentPane.add(btnSend); |
| 87 | + textArea = new JTextArea(); |
| 88 | + textArea.setFont(new Font("Tahoma", Font.PLAIN, 19)); |
| 89 | + textArea.setEditable(false); |
| 90 | + textArea.setBounds(0, 0, 396, 554); |
| 91 | + contentPane.add(textArea); |
| 92 | + |
| 93 | + /* scrollBar = new JScrollBar(); |
| 94 | + // scrollBar.setvie |
| 95 | + scrollBar.addAdjustmentListener(new AdjustmentListener() { |
| 96 | + public void adjustmentValueChanged(AdjustmentEvent e) { |
| 97 | + } |
| 98 | + }); |
| 99 | + scrollBar.setBounds(395, 0, 27, 554); |
| 100 | + contentPane.add(scrollBar,BorderLayout.EAST); |
| 101 | + |
| 102 | + JScrollBar scrollBar = new JScrollBar(); |
| 103 | + scrollBar.addAdjustmentListener(new AdjustmentListener() { |
| 104 | + public void adjustmentValueChanged(AdjustmentEvent e) { |
| 105 | + } |
| 106 | + }); |
| 107 | + scrollBar.setBounds(395, 0, 21, 554); |
| 108 | + contentPane.add(scrollBar);*/ |
| 109 | + |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + void action() |
| 114 | + { |
| 115 | + |
| 116 | + String query=textField_1.getText(); |
| 117 | + textArea.append(" You-> "+query+"\n"); |
| 118 | + //query.trim(); |
| 119 | + query=query.toLowerCase(); |
| 120 | + if(query.contains("clear screen")||query.contains("clr")||query.contains("cls")) |
| 121 | + { |
| 122 | + textArea.setText(""); |
| 123 | + textField_1.setText(""); |
| 124 | + } |
| 125 | + else if(query.contains("hi")||query.contains("hey")||query.contains("hola")||query.contains("hello")) |
| 126 | + { |
| 127 | + Random rand=new Random(); |
| 128 | + int a=rand.nextInt(4); |
| 129 | + if(a==0) |
| 130 | + { |
| 131 | + ai("Hey,I'm Here"); |
| 132 | + } |
| 133 | + else if(a==1) |
| 134 | + { |
| 135 | + ai("Hey,Wssup?"); |
| 136 | + } |
| 137 | + else if(a==2) |
| 138 | + { |
| 139 | + ai("Hey,How u doing?"); |
| 140 | + } |
| 141 | + else if(a==3) |
| 142 | + { |
| 143 | + ai("Hello,u there?"); |
| 144 | + } |
| 145 | + else if(a==4) |
| 146 | + { |
| 147 | + ai("Hello,How u doing?"); |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + else if(query.contains("fine")||query.contains("I'm fine")||query.contains("am okay")) |
| 152 | + { |
| 153 | + Random rand=new Random(); |
| 154 | + int a=rand.nextInt(4); |
| 155 | + if(a==0) |
| 156 | + { |
| 157 | + ai("It's good to know that you are fine"); |
| 158 | + } |
| 159 | + else if(a==1) |
| 160 | + { |
| 161 | + ai("It's pleasure to know that you are fine"); |
| 162 | + } |
| 163 | + else if(a==2) |
| 164 | + { |
| 165 | + ai("Oh,great"); |
| 166 | + } |
| 167 | + else if(a==3) |
| 168 | + { |
| 169 | + ai("Oh,such a great news"); |
| 170 | + } |
| 171 | + else if(a==4) |
| 172 | + { |
| 173 | + ai("Wish,you always remain fine"); |
| 174 | + } |
| 175 | + } |
| 176 | + else if(query.contains("wssup")||query.contains("whats up")||query.contains("whatsup")||query.contains("wtsup") |
| 177 | + ||query.contains("you doing")||query.contains("u doing")) |
| 178 | + { |
| 179 | + ai("I'm fine and what about you"); |
| 180 | + } |
| 181 | + else if(query.contains("yes")||query.contains("yeah")||query.contains("ya")) |
| 182 | + { |
| 183 | + Random rand=new Random(); |
| 184 | + int a=rand.nextInt(2); |
| 185 | + if(a==0) |
| 186 | + { |
| 187 | + ai("Oh, Great"); |
| 188 | + } |
| 189 | + else if(a==1) |
| 190 | + { |
| 191 | + ai("Ohh,Great news"); |
| 192 | + } |
| 193 | + else if(a==2) |
| 194 | + { |
| 195 | + ai("Nice"); |
| 196 | + } |
| 197 | + } |
| 198 | + else if(query.contains("ntnhng")||query.contains("nothing")) |
| 199 | + { |
| 200 | + Random rand=new Random(); |
| 201 | + int a=rand.nextInt(2); |
| 202 | + if(a==0) |
| 203 | + { |
| 204 | + ai("Why?"); |
| 205 | + } |
| 206 | + else if(a==1) |
| 207 | + { |
| 208 | + ai("Why? as per I know you are a very charming person"); |
| 209 | + } |
| 210 | + else if(a==2) |
| 211 | + { |
| 212 | + ai("Why? Don't waste your time this way"); |
| 213 | + } |
| 214 | + } |
| 215 | + else{ |
| 216 | + Random rand=new Random(); |
| 217 | + int a=rand.nextInt(4); |
| 218 | + if(a==0) |
| 219 | + { |
| 220 | + ai("Sorry,I'cant get you"); |
| 221 | + } |
| 222 | + else if(a==1) |
| 223 | + { |
| 224 | + ai("Plase say it correctly"); |
| 225 | + } |
| 226 | + else if(a==2) |
| 227 | + { |
| 228 | + ai("Please rephrase that"); |
| 229 | + } |
| 230 | + else if(a==3) |
| 231 | + { |
| 232 | + ai("???"); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + |
| 238 | + void ai(String s) |
| 239 | + { |
| 240 | + textArea.append(" AI->"+s+"\n\n"); |
| 241 | + textField_1.setText(""); |
| 242 | + } |
| 243 | +} |
0 commit comments