在Java中遇到键盘事件的问题

时间:2012-06-25 06:04:37

标签: java

所以我正在尝试使用KeyBoardEvent,只是出于测试目的..我正在做的事似乎不起作用:/希望你们能告诉我我在哪里。谢谢!

package com.dillyg10.test.Keys;

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Keys extends JPanel implements KeyListener  {
    private static final long serialVersionUID = 1L;

    public Keys(){

    }

    public static void registerEvent(Window w){
        w.setFocusTraversalKeysEnabled(false);
        w.setFocusable(true);
        w.addKeyListener(new Keys());
    }
    public void keyPressed(KeyEvent e) {
        System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
    }
    public void keyReleased(KeyEvent e) {

    }
    public void keyTyped(KeyEvent e) {

    }
    public static void gui() {

        JFrame frame = new JFrame("Keys");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        registerEvent(new Window(frame));


        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                gui();
            }
        });
     }
    }

此代码基本上是一个测试,用于确定keyboardevents是否有效,我会在按下时打印出密钥..但是没有打印任何内容。

3 个答案:

答案 0 :(得分:0)

解决方案

package com.java.tests;

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Keys extends JPanel implements KeyListener {

private static final long serialVersionUID = 1L;
public static Keys keys = new Keys();

public Keys() {
}

public void registerEvent(Window w) {
    w.addKeyListener(this);
    w.setSize(200,200);
}

@Override
public void keyPressed(KeyEvent e) {
    System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
}

@Override
public void keyReleased(KeyEvent e) {
    System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
}

@Override
public void keyTyped(KeyEvent e) {
}

public void gui() {

    JFrame frame = new JFrame("Keys");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    registerEvent(frame);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            keys.gui();
        }
    });
    }
}

答案 1 :(得分:0)

而不是w.addKeyListener(new Keys());使用w.addKeyListener(this);

希望这可以解决您的问题。

答案 2 :(得分:0)

自由地对您的代码进行一些更改并设法使其工作:

package sample.examples;

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Keys extends JPanel implements KeyListener  {
    private static final long serialVersionUID = 1L;

    public Keys(){
        this.setBackground(Color.black);
    }

    public void keyPressed(KeyEvent e) {
        System.out.println("Button Pressed: " + KeyEvent.getKeyText(e.getKeyCode()));
    }
    public void keyReleased(KeyEvent e) {       

    }
    public void keyTyped(KeyEvent e) {      

    }
    public void gui() {

        JFrame frame = new JFrame("Keys");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(500,500));
        frame.setFocusTraversalKeysEnabled(false);
        frame.setFocusable(true);
        frame.setContentPane(this);
        frame.addKeyListener(this);      


        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                Keys k = new Keys();
                k.gui();
            }
        });
     }
    }

您的一个问题似乎是,您没有为JPanel指定任何尺寸,因此,无论您从哪里点击,都从不点击JPanel。为了方便您,我为JPanel黑色着色。