1
考慮下面的(部分)代碼:的KeyListener不產生任何響應
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test extends Applet implements MouseListener , KeyListener
{
private static final long serialVersionUID = 1L;
private static final int TOTAL_POINTS = 500;
private static final int THRESHOLD = 5;
// the arrays that contain the indexes of the points that the user created
private int[] m_Xindex, m_yIndex;
// The number of points that the user created
private int m_pointsCreated;
@Override
public void keyTyped(KeyEvent keyEvent)
{
char key = keyEvent.getKeyChar();
if (key == 'F')
System.out.println("123");
}
@Override
public void mouseReleased(MouseEvent arg0) {/* Empty */ }
@Override
public void mouseClicked(MouseEvent e) {/* Empty */ }
@Override
public void mouseEntered(MouseEvent e) {/* Empty */ }
@Override
public void mouseExited(MouseEvent e) {/* Empty */ }
@Override
public void mousePressed(MouseEvent myEvent) {/* Empty */ }
@Override
public void keyPressed(KeyEvent keyEvent) {}
@Override
public void keyReleased(KeyEvent keyEvent) {}
}
我打消了我的工作代碼,並只留下有問題的代碼。
當我按F
,我想打印到屏幕123
,但沒有什麼是 打印到屏幕上。
keyTyped
的代碼有什麼問題?
此外,不要忘記註冊按鍵偵聽器。還要注意,關鍵聽衆只有在註冊的組件可以獲得焦點並且擁有當前焦點的情況下才能使用。 – MadProgrammer 2013-03-26 09:49:49
@AndrewThompson:讓它成爲一個答案,所以我會選擇它作爲正確的,因爲你剛剛度過了我的一天! +1! – ron 2013-03-26 10:16:31
很高興你把它分類。 :) – 2013-03-26 13:41:50