KeyListener没有反应或被检测到[JAVA]

时间:2016-01-29 04:09:03

标签: java keylistener

今天我决定开发一个简单的控制台类型程序并使用KeyListener进行实验,我查看了一些教程和指南并让它适用于我的实验的很大一部分,但是在大约5日编译代码时尝试它随机停止工作,我无法让它再次工作。任何提示或我只是遗漏了一些非常重要的东西?

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 个答案:

没有答案