What is the proper use of KeyListener? [closed]

给你一囗甜甜゛ 提交于 2019-12-13 05:05:17

问题


So I have been working on trying to extract the data in my jTextFields and this error message keeps popping up referring to an:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: calcu.turkey (<--my class) cannot be cast to java.awt.event.KeyListener

      jTextField2.addKeyListener(new java.awt.event.KeyListener() {

        @Override
        public void keyTyped(KeyEvent ke) {
            //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void keyPressed(KeyEvent ke) {
          //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void keyReleased(KeyEvent ke) {
            //To change body of generated methods, choose Tools | Templates.
        }
    });
    jTextField2.addKeyListener((KeyListener) this);
    jTextField2.setFont(new java.awt.Font("Times New Roman", 0, 10)); // NOI18N
    jTextField2.setText("0");
    getContentPane().add(jTextField2);
    jTextField2.setBounds(250, 40, 70, 20);

Most importantly this line: jTextField2.addKeyListener((KeyListener) this); as it views the (KeyListener) as an error. I was wondering how do you properly add the KeyListener so it grab the number in the textfield.


回答1:


You don't use a KeyListener. Rarely do you have a need to do this. There are better API's to be used.

For example when using a text component you would probably use a DocumentListener (see How to Write a Document Listener) or DocumentFilter (see Implementing a Document Filter).

I was wondering how do you properly add the KeyListener so it grab the number in the textfield.

Normally you would grab the number on some unrelated event. For example you have a "Submit" button on a form. Then in that case you would add an ActionListener to the button and then use the getText() method of the text field when you want to submit the form.

Also, the Swing tutorial that I referenced above has a section on How to Write a Key Listener.




回答2:


  1. You get the error because your class does not implement the KeyListener interface. You will need to have a class declaration like public class turkey implements KeyListener and then in your class you need to implement all the methods defined by the KeyListener interface.
  2. The correct way is not to use a KeyListener for JTextField (or any JTextComponent) for that matter. Use a DocumentListener or DocumentFilter (depends a bit on your use-case)
  3. Not sure why you added the anonymous KeyListener to your text field and then want to add your class instance as KeyListener as well



回答3:


jTextField2.addKeyListener((KeyListener) this);

The above line is the culprit. Seems like your class is not implementing a KeyListener Infact, I think you don't need that line at all, as you already attached an KeyListener to jTextField2 by doing jTextField2.addKeyListener(new ...

Inside any of the below 3 methods, you can use ke.getKeyCode() or ke.getKeyChar() to retrieve the Key that was pressed.

@Override
    public void keyTyped(KeyEvent ke) {
        //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void keyPressed(KeyEvent ke) {
      //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void keyReleased(KeyEvent ke) {
        //To change body of generated methods, choose Tools | Templates.
    }


来源:https://stackoverflow.com/questions/17097750/what-is-the-proper-use-of-keylistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!