簡體   English   中英

按鍵事件不起作用-Java

[英]Key Event not working - java

我正在研究一些教程,但已經在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);
}   
}

任何幫助將非常感激。

您似乎沒有在代碼中的任何地方調用new Keyboard() ,因此永遠不會創建或注冊偵聽器。 從設計的角度來看,我想您打算在此面板上使用其按鈕,因此請按以下方式更改面板的類型:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM