import javax.swing.JFrame;
import java.awt.event.*;
//想测试每个键的keycode值,但是为什么报错呢?
class MyFrame extends JFrame{

MyFrame mf=null;
public MyFrame(){
mf=new MyFrame();
mf.setTitle("测试监听");
mf.addKeyListener(new KeyAdapter(){
public void KeyPressed(final KeyEvent e){
System.out.println(e.getKeyCode());
}

});
}
}
public class TestListener {
public static void main(String args[]){
new MyFrame();
}
}
我想实现当我按一个键的时候打印出他的keycode值。
但是运行要报错,请问怎么修改?

解决方案 »

  1.   

    不停的new MyFrame(); 不会死循环么?在MyFrame 里应该用this吧
      

  2.   

    哦,我改成这个了:
    import javax.swing.JFrame;
    import java.awt.event.*;
    //想测试没个键的keycode值,但是为什么报错呢?
    class MyFrame extends JFrame{

    //MyFrame mf=new MyFrame();
    public MyFrame(){
    //mf=new MyFrame();
    this.setTitle("测试监听");
    this.setSize(240, 320);
    this.addKeyListener(new KeyAdapter(){
    public void KeyPressed(final KeyEvent e){
    System.out.println(e.getKeyCode());
    }

    });
    this.setVisible(true);
    }
    }
    public class TestListener {
    public static void main(String args[]){
    new MyFrame();
    }
    }
    可是我想实现的功能还是没有实现,哪里还有问题?
      

  3.   

    参考:http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.htmlpublic class TestKeyListenerFrame extends JFrame {    public static void main(String[] args) {
            new TestKeyListenerFrame().setVisible(true);
        }    public TestKeyListenerFrame() throws HeadlessException {
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setSize(200, 100);
            this.setLocation(200, 200);        this.addKeyListener(new KeyListener() {
                public void keyTyped(KeyEvent e) {
                    displayInfo(e, "KEY TYPED: ");
                }            public void keyPressed(KeyEvent e) {
                    displayInfo(e, "KEY PRESSED: ");
                }            public void keyReleased(KeyEvent e) {
                    displayInfo(e, "KEY RELEASED: ");
                }
            });
        }    private void displayInfo(KeyEvent e, String keyStatus) {        //You should only rely on the key char if the event
            //is a key typed event.
            int id = e.getID();
            String keyString;
            if (id == KeyEvent.KEY_TYPED) {
                char c = e.getKeyChar();
                keyString = "key character = '" + c + "'";
            } else {
                int keyCode = e.getKeyCode();
                keyString = "key code = " + keyCode
                        + " ("
                        + KeyEvent.getKeyText(keyCode)
                        + ")";
            }        int modifiersEx = e.getModifiersEx();
            String modString = "extended modifiers = " + modifiersEx;
            String tmpString = KeyEvent.getModifiersExText(modifiersEx);
            if (tmpString.length() > 0) {
                modString += " (" + tmpString + ")";
            } else {
                modString += " (no extended modifiers)";
            }        String actionString = "action key? ";
            if (e.isActionKey()) {
                actionString += "YES";
            } else {
                actionString += "NO";
            }        String locationString = "key location: ";
            int location = e.getKeyLocation();
            if (location == KeyEvent.KEY_LOCATION_STANDARD) {
                locationString += "standard";
            } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
                locationString += "left";
            } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
                locationString += "right";
            } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
                locationString += "numpad";
            } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
                locationString += "unknown";
            }        System.out.println("------- " + keyStatus + " -------\n" +
                    keyString + "\n" +
                    modString + "\n" +
                    actionString + "\n" +
                    locationString);
        }
    }