0

所以我试图诚实地使用 KeyBoardEvent 只是为了测试目的.. 我所做的似乎并不奏效:/希望你们能告诉我我在哪里 derping。谢谢!

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();
            }
        });
     }
    }

这段代码基本上是一个测试,以确定键盘事件是否有效,我只是在按下时打印出键..但没有打印任何内容。

4

3 回答 3

0

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

包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黑色。

于 2012-06-25T06:42:32.613 回答
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();
        }
    });
    }
}
于 2012-06-25T06:30:14.540 回答
0

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

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

于 2012-06-25T06:33:31.577 回答