0

Today I decided to work on a simple console type program and experiment with KeyListener, I looked at a few tutorials and guides and had it working for a good portion of my experiment, however when compiling the code on about the 5th try it randomly stopped working and I have not been able to get it working again. Any tips or am I just missing something REALLY important?

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

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

@SuppressWarnings("serial")
public class core extends JFrame implements KeyListener
{
public JTextArea output;
public JTextField input;
public String stringName = "Spencer";
public core()
{
    setSize(750, 400);
    setDefaultCloseOperation(3);
    setLayout(null);
    setResizable(false);
    setVisible(true);

    input = new JTextField("");
    input.setBounds(-1, 350, 750, 25 );
    input.setEditable(true);
    input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    getContentPane().add(input);

    output = new JTextArea("Hello "+ stringName + ", how are you doing today?");
    output.setBounds(0,0,750,350);
    output.setEditable(false);
    getContentPane().add(output);

    repaint();

}

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

@Override
public void keyPressed(KeyEvent a)
{
    if (a.getKeyCode() == KeyEvent.VK_ENTER)
    {

        System.out.println("Hi");
        if(input.getText().equals("I am doing great!"))
        {
            output.setText("That's good, anything interesting happen?");
        }
        else if(input.getText().equals("I'm good, how about you?"))
        {
            output.setText("I am doing great! Anything interesting happen today?");
        }
        repaint();
    }
    else
    {

    }
}

@Override
public void keyReleased(KeyEvent a)
{

}

@Override
public void keyTyped(KeyEvent a)
{

}
}

0 Answers0