我想添加 KeyListener 以通过键盘获取输入,所以我添加了 keyListener 并实现了它的方法,但是当我运行它并按下任何键时它可以工作,但在控制台中它会这样写:
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
at Calculator.keyReleased(Calculator.java:520)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
在这个keylistener工作正常之后,但是当我点击它停止工作的任何按钮时,我按下键但没有任何反应。我的代码:
@Override
public void keyPressed(KeyEvent e) {
try{
switch (e.getKeyCode())
{
case KeyEvent.VK_NUMPAD0:
case KeyEvent.VK_0:
num0();
break;
case KeyEvent.VK_NUMPAD1:
case KeyEvent.VK_1:
num1();
break;
case KeyEvent.VK_NUMPAD2:
case KeyEvent.VK_2:
num2();
break;
case KeyEvent.VK_NUMPAD3:
case KeyEvent.VK_3:
num3();
break;
case KeyEvent.VK_NUMPAD4:
case KeyEvent.VK_4:
num4();
break;
case KeyEvent.VK_NUMPAD5:
case KeyEvent.VK_5:
num5();
break;
case KeyEvent.VK_NUMPAD6:
case KeyEvent.VK_6:
num6();
break;
case KeyEvent.VK_NUMPAD7:
case KeyEvent.VK_7:
num7();
break;
case KeyEvent.VK_NUMPAD8:
case KeyEvent.VK_8:
num8();
break;
case KeyEvent.VK_NUMPAD9:
case KeyEvent.VK_9:
num9();
break;
case KeyEvent.VK_BACK_SPACE:
back();
break;
case KeyEvent.VK_DELETE:
clear();
break;
case KeyEvent.VK_ENTER:
equals();
break;
case KeyEvent.VK_ADD:
bAdd();
break;
case KeyEvent.VK_DECIMAL:
bDot();
break;
case KeyEvent.VK_MULTIPLY:
bMul();
break;
case KeyEvent.VK_SUBTRACT:
bSub();
break;
case KeyEvent.VK_DIVIDE:
bDiv();
break;
default:
}
}
catch(UnsupportedOperationException ex){}
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");// line 520
}