关键事件无效 - java

时间:2014-05-31 03:42:16

标签: java swing awt keylistener keyevent

我正在学习一些教程,但几天来一直困在keyEvents上。我确定这与我如何实现它有关,因为我甚至无法获得打印声明。

这是我令人不安的片段

static class Keyboard extends JPanel {

public Keyboard() {
   addKeyListener(new KeyAdapter() {

       public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub
            System.out.println("pressed");
            switch (e.getKeyCode()) {
            case KeyEvent.VK_DOWN: 
                y += 10;break;
            case KeyEvent.VK_UP:
                y -= 10;break;
            case KeyEvent.VK_LEFT:
                x -= 10;break;
            case KeyEvent.VK_RIGHT:
                x += 10;break;
            }
            repaint();
        }
    });
}
}

这是完整的代码。它只是一个简单的awt演示,您可以使用现有按钮移动和放大圆圈。

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

public class ShapeTest1 extends JFrame {

   private JButton enlarge = new JButton("enlarge"); 
   private JButton up = new JButton("up"); 
   private JButton right = new JButton("right"); 
   private JButton down = new JButton("down"); 
   private JButton left = new JButton("left"); 
   BoardPanel bp;
   public ShapeTest1()
   {
  bp = new BoardPanel();

  // Create a separate panel and add all the buttons 
  JPanel panel = new JPanel();
  panel.add(enlarge); 
  panel.add(up); 
  panel.add(right); 
  panel.add(down); 
  panel.add(left); 

 // add Action listeners to all button events
 enlarge.addActionListener(bp);
  up.addActionListener(bp);
  down.addActionListener(bp);
  left.addActionListener(bp);
  right.addActionListener(bp);

 // add panels to frame
 add (bp, BorderLayout.CENTER);             
  add (panel,BorderLayout.SOUTH);
}      

public static void main(String args[]) throws Exception
{
   ShapeTest1 shape = new ShapeTest1();
   shape.setTitle("Draw Shape");
   shape.setSize(700,700);
   shape.setLocationRelativeTo(null);  // center the frame
   shape.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   shape.setVisible(true);
}
}

class BoardPanel extends JPanel implements ActionListener {

private Graphics gr;
protected static int x;
protected static int y;
protected int radius = 50;

public BoardPanel()
{
  x = 300;
  y = 300;
  radius = 50;

}

/* responds to various button clicked messages */ 
public void actionPerformed(ActionEvent e)
{
 if (e.getActionCommand().compareTo("enlarge") == 0)
    radius += 10;
 else if (e.getActionCommand().compareTo("left") == 0)
    x -= 10;
 else if (e.getActionCommand().compareTo("right") == 0)
    x += 10;
 else if (e.getActionCommand().compareTo("up") == 0)
    y -= 10;
 else if (e.getActionCommand().compareTo("down") == 0)
    y += 10;
  repaint();
}

static class Keyboard extends JPanel {

public Keyboard() {
   addKeyListener(new KeyAdapter() {

       public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub
            System.out.println("pressed");
            switch (e.getKeyCode()) {
            case KeyEvent.VK_DOWN: 
                y += 10;break;
            case KeyEvent.VK_UP:
                y -= 10;break;
            case KeyEvent.VK_LEFT:
                x -= 10;break;
            case KeyEvent.VK_RIGHT:
                x += 10;break;
            }
            repaint();
        }
    });
}
}

/* Redraws the board and the pieces
 * Called initially and in response to repaint()
 */
protected void paintComponent(Graphics gr)
{
    super.paintComponent(gr);
    gr.fillOval(x-radius, y-radius, 2*radius, 2*radius);
}   
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您似乎没有在代码中的任何位置调用new Keyboard(),因此永远不会创建或注册侦听器。从设计中我可以想象你打算使用这个面板上的按钮,所以改变面板的类型如下:

// Create a separate panel and add all the buttons 
JPanel panel = new Keyboard();