Java如何制作Keylistener?

时间:2013-12-25 02:43:59

标签: java swing jpanel keypress keylistener

我正在尝试为我制作的游戏制作一个高分屏幕,我想使用一个keylistener,但没有任何反应。

这是我的代码中需要修复的部分。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import java.io.*;

public class score extends JPanel {
   public static char highchar[] = new char[10];
   public static int i = 0;
   public static boolean flag = true;

   public static void scr() {
      JFrame f = new JFrame("HighScores");
      score c = new score();
      f.setContentPane(c);
      f.setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize()
            .getWidth() - 13) / 2 - 200, (int) (Toolkit.getDefaultToolkit()
            .getScreenSize().getHeight() - 63) / 2 - 100);
      c.setPreferredSize(new Dimension(400, 200));
      f.pack();
      f.setResizable(true);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane();
      f.setVisible(true);
      while (flag) {
         c.repaint();
      }
   }

   public score() {
      addKeyListener(new KeyAdapter() {

         public void keyPressed(KeyEvent e) {
            System.out.println("hello");
            if (flag) {
               if (e.getKeyCode() == KeyEvent.VK_A) {
                  highchar[i] = 'A';
               }
               if (e.getKeyCode() == KeyEvent.VK_B) {
                  highchar[i] = 'B';
               }
               if (e.getKeyCode() == KeyEvent.VK_C) {
                  highchar[i] = 'C';
               }
               if (e.getKeyCode() == KeyEvent.VK_D) {
                  highchar[i] = 'D';
               }
               if (e.getKeyCode() == KeyEvent.VK_E) {
                  highchar[i] = 'E';
               }
               if (e.getKeyCode() == KeyEvent.VK_F) {
                  highchar[i] = 'F';
               }
               if (e.getKeyCode() == KeyEvent.VK_G) {
                  highchar[i] = 'G';
               }
               if (e.getKeyCode() == KeyEvent.VK_H) {
                  highchar[i] = 'H';
               }
               if (e.getKeyCode() == KeyEvent.VK_I) {
                  highchar[i] = 'I';
               }
               if (e.getKeyCode() == KeyEvent.VK_J) {
                  highchar[i] = 'J';
               }
               if (e.getKeyCode() == KeyEvent.VK_K) {
                  highchar[i] = 'K';
               }
               if (e.getKeyCode() == KeyEvent.VK_L) {
                  highchar[i] = 'L';
               }
               if (e.getKeyCode() == KeyEvent.VK_M) {
                  highchar[i] = 'M';
               }
               if (e.getKeyCode() == KeyEvent.VK_N) {
                  highchar[i] = 'N';
               }
               if (e.getKeyCode() == KeyEvent.VK_O) {
                  highchar[i] = 'O';
               }
               if (e.getKeyCode() == KeyEvent.VK_P) {
                  highchar[i] = 'P';
               }
               if (e.getKeyCode() == KeyEvent.VK_Q) {
                  highchar[i] = 'Q';
               }
               if (e.getKeyCode() == KeyEvent.VK_R) {
                  highchar[i] = 'R';
               }
               if (e.getKeyCode() == KeyEvent.VK_S) {
                  highchar[i] = 'S';
               }
               if (e.getKeyCode() == KeyEvent.VK_T) {
                  highchar[i] = 'T';
               }
               if (e.getKeyCode() == KeyEvent.VK_U) {
                  highchar[i] = 'U';
               }
               if (e.getKeyCode() == KeyEvent.VK_V) {
                  highchar[i] = 'V';
               }
               if (e.getKeyCode() == KeyEvent.VK_W) {
                  highchar[i] = 'W';
               }
               if (e.getKeyCode() == KeyEvent.VK_X) {
                  highchar[i] = 'X';
               }
               if (e.getKeyCode() == KeyEvent.VK_Y) {
                  highchar[i] = 'Y';
               }
               if (e.getKeyCode() == KeyEvent.VK_Z) {
                  highchar[i] = 'Z';
               }
               if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                  highchar[i] = ' ';
               }
               if (e.getKeyCode() == KeyEvent.VK_0) {
                  highchar[i] = '0';
               }
               if (e.getKeyCode() == KeyEvent.VK_1) {
                  highchar[i] = '1';
               }
               if (e.getKeyCode() == KeyEvent.VK_2) {
                  highchar[i] = '2';
               }
               if (e.getKeyCode() == KeyEvent.VK_3) {
                  highchar[i] = '3';
               }
               if (e.getKeyCode() == KeyEvent.VK_4) {
                  highchar[i] = '4';
               }
               if (e.getKeyCode() == KeyEvent.VK_5) {
                  highchar[i] = '5';
               }
               if (e.getKeyCode() == KeyEvent.VK_6) {
                  highchar[i] = '6';
               }
               if (e.getKeyCode() == KeyEvent.VK_7) {
                  highchar[i] = '7';
               }
               if (e.getKeyCode() == KeyEvent.VK_8) {
                  highchar[i] = '8';
               }
               if (e.getKeyCode() == KeyEvent.VK_9) {
                  highchar[i] = '9';
               }
               if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
                  i--;
                  highchar[i] = ' ';
                  i--;
               }
               if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                  flag = false;
               }
               i++;
               System.out.println(highchar[i]);
            }
         }
      });
   }
}

你可以在不改变scr方法的情况下修复keylistener吗?

1 个答案:

答案 0 :(得分:4)

如果你已经完成了任何搜索,你会发现KeyListeners只对某个组件有效,如果它具有焦点,对于JPanel来说,这意味着可以使其可聚焦,someJPanel.setFocusable(true)然后将焦点分配到它someJPanel.requestFocusInWindow()

您还会看到,如果可能的话,我们会避免使用KeyListeners来支持Key Bindings

作为旁注,这让我担心:

while(flag){
    c.repaint();
}

在Swing GUI中,真正的循环是一件危险的事情,因为你将占用事件线程。根本就不要这样做 - 没有理由这样做。

我看到它是你的scr方法的一部分,并且你已经声明了,

  

你可以在不改变scr方法的情况下修复keylistener吗?

教练给你的scr方法是什么?如果是这样,我就不会太相信那个教练了。