0
JPanel對事件反應很奇怪。它可以處理MouseEvent,但不會處理KeyEvent。KeyListener不響應鍵盤事件。 Ok for mouse events
JPanel類:
public class DrawPanel extends JPanel {
class DrawListener extends MouseAdapter implements KeyListener {
@Override
public void mouseDragged(MouseEvent e) {
// works fine
}
@Override
public void mouseReleased(MouseEvent e) {
// works fine
}
@Override
public void mouseClicked(MouseEvent e) {
// works fine
}
@Override
public void keyPressed(KeyEvent e) {
// Listener is NOT invoked here if anykey is pressed
}
@Override
public void keyReleased(KeyEvent e) {
// NOT invoked
}
@Override
public void keyTyped(KeyEvent e) {
// NOT invoked
}
構造面板:
// Class constructor
public DrawPanel() {
DrawListener l = new DrawListener();
addMouseListener(l);
addMouseMotionListener(l);
addKeyListener(l);
setFocusable(true);
requestFocus();
}
這怎麼可能,如果處理的MouseEvent工作得很好?哪裏可能是錯的?
像往常一樣,@ MadProgrammer的答案在5分鐘內出現,並提供確切和準確的信息。有什麼方法可以自動提升他發佈的任何內容嗎? :) – ccjmne