如何在Java Swing中修复此Keylistener错误

时间:2019-06-16 15:35:45

标签: java swing keylistener

我正在学习python中的GUI,我不知道为什么keylistener无法正常工作,动作监听器却可以正常工作,但是keylistner却不行。

我试图公开keylistener方法 我试图在主类中实现它(在我的例子中为“ GUIevents”)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class GUIevents extends JFrame 
{

    int buttonclicked = 0;
    JButton button;
    JTextField entry;
    JTextArea output;
    public static void main(String[] args) 
    {

    new GUIevents();    

    } // main

    public GUIevents()
    {

        // Form
        .....
        ///////

        JPanel p = new JPanel();

        button = new JButton("click for event");    

        entry = new JTextField("",15);

        output = new JTextArea(20,20);



        // Listneres 

        ListenForButton bevent = new ListenForButton();

        button.addActionListener(bevent);

        ListenForKeys kevent = new ListenForKeys();

        entry.addKeyListener(kevent);


        // adding 
         ...
        ////

        this.setVisible(true);        

    } // gui event constructor


    private class ListenForButton implements ActionListener
    {


        public void actionPerformed(ActionEvent e)
        {

            if (e.getSource() == button)
            {

                buttonclicked++;

                System.out.println("button clicked " + buttonclicked + " times");

            }

        }

    } // listenforbutton

    private class ListenForKeys implements KeyListener
    {

        public void KeyPressed(KeyEvent e)
        {
            output.append("key hit: " + e.getKeyChar() + "\n");
        }

        public void KeyReleased(KeyEvent arg0)
        {

        }

        public void KeyTyped(KeyEvent arg0)
        {

        }

    }

}

我在调试时收到此错误,谢谢.................................. ................................................... ....................(我需要添加此内容,这样我才可以发布对不起)

GUIevents.java:97: error: GUIevents.ListenForKeys is not abstract and does not override abstract method keyReleased(KeyEvent) in KeyListener
    private class ListenForKeys implements KeyListener

1 个答案:

答案 0 :(得分:1)

您没有覆盖任何KeyListener接口方法,请检查拼写。 Java方法以小写字母开头,您已在ListenForKeys类的所有方法定义中将K大写。