看板 java 關於我們 聯絡資訊
※ [本文轉錄自 Programming 看板] 作者: h5u (h5u) 看板: Programming 標題: [請益] Java keyListener內方法的執行順序 時間: Fri Nov 6 21:52:53 2009 想請問各位先進 關於鍵盤事件偵測的順序 正確按鍵 現在我想要用一個說明頁面 ┌-------------┐-----------------┐ \/ | | [歡迎光臨...]---------->[畫灰色圓]----->[變綠色]---->[不閃爍停留相同位置] 空白鍵(release) 停200ms 錯誤按鍵 這樣我同時使用keyReleased keyTyped兩者是否有時間上的衝突 因為我發現沒有跟我所設定的亮起位置一致 完整程式碼 http://www.badongo.com/cn/cfile/18254889 =====================部份程式碼如下==================== wjp.addKeyListener(new java.awt.event.KeyAdapter(){ public void keyReleased(java.awt.event.KeyEvent e){ if (count <= trialNum) { // trial進行中 if (count == 0){ // 指導語畫面 if (e.getKeyCode() == 32){ started = System.currentTimeMillis(); count = count+1; // 進行第一個trial Graphics k = wjp.getGraphics(); k.clearRect(0, 0, width, height); k.setColor(Color.gray); k.fillOval(width/2-290, height/2-40, 80, 80); k.fillOval(width/2-190, height/2-40, 80, 80); new ThreadUtils().sleep(200); t_1 = System.currentTimeMillis(); } }else{ switch (pos[count-1]) { case 1: if (e.getKeyCode()==83){ Graphics k = wjp.getGraphics(); k.clearRect(0, 0, width, height); k.setColor(Color.gray); k.fillOval(width/2-290, height/2-40, 80, 80); (略) new ThreadUtils().sleep(200); } break; case 2: (略) break; } } // 參照pos的位置亮起 Graphics u = wjp.getGraphics(); u.clearRect(0, 0, width, height); if (pos[count-1]==1){ u.setColor(Color.green); } else {u.setColor(Color.gray);} u.fillOval(width/2-290, height/2-40, 80, 80); (略) System.out.println("目標在"+pos[count-1]+"的位置"); switch (e.getKeyCode()){ case 83: if (pos[count-1]==1){ t_1 = System.currentTimeMillis(); //開始計時 count = count+1; } else { ER[count-1] = ER[count-1]+1; } break; (其他case略) } } else { //做完所有trial Graphics gg = wjp.getGraphics(); gg.clearRect(0, 0, width, height); long sum = 0; for (int i=0; i<trialNum; ++i){ sum += RT[i]; } String result = "平均反應時間:"+(sum/trialNum); gg.setColor(Color.white); gg.drawString(result, 100, 100); stopped = System.currentTimeMillis(); System.out.print("實驗總共耗時"+(stopped-started)+"毫秒"); } }; public void keyPressed(java.awt.event.KeyEvent e){ if (e.getKeyCode()!=32){ record =System.currentTimeMillis()-t_1; if ((count-1) <trialNum){ RT[count-1] = record; System.out.println("第幾個"+(count)+"trial, 反應時間: "+RT[count-1]); } } }; }); ======================== 程式執行結果 555555 333331 444444 332465 664531 222134 .... 指導語結束 目標在5的位置第幾個1trial, 反應時間: 547 目標在5的位置第幾個2trial, 反應時間: 312 目標在5的位置第幾個3trial, 反應時間: 390 目標在5的位置第幾個4trial, 反應時間: 391 目標在5的位置第幾個5trial, 反應時間: 360 目標在5的位置第幾個6trial, 反應時間: 343 目標在5的位置第幾個7trial, 反應時間: 328 目標在3的位置第幾個7trial, 反應時間: 1313 目標在3的位置第幾個8trial, 反應時間: 344 目標在3的位置第幾個9trial, 反應時間: 344 目標在3的位置第幾個10trial, 反應時間: 359 目標在3的位置第幾個11trial, 反應時間: 328 目標在3的位置第幾個12trial, 反應時間: 360 目標在1的位置第幾個12trial, 反應時間: 1063 目標在1的位置第幾個13trial, 反應時間: 266 目標在4的位置第幾個13trial, 反應時間: 922 ........ 跟設定的pos矩陣不一樣 找很久還是沒發現哪裡出了問題....Orz 希望有好心人指點一二 感謝<(_ _)> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.218.22 ※ 編輯: h5u 來自: 140.123.218.22 (11/06 21:55) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.218.22
gatboyha: ◢███◣ 11/07 04:23
AmosYang:(因為懶得去等那45秒)我沒有看整個程式,不過,通常在 UI 11/07 20:55
AmosYang: thread上sleep就是在自找麻煩… (但我卻等了60秒推文CD) 11/07 20:57
h5u:找到問題了!! 我把前後要抓的e.getKeyCode分開就可以了Orz 11/11 02:49