Introduction
When running a Java application in full screen exclusive mode, the usual event handling mechanisms may not work as expected. This article explores the correct approach to handle user input from keyboard and mouse in this mode.
Problem
Passive rendering mode allows the use of KeyListener and ActionListener interfaces to handle user input. However, in full screen exclusive mode, these methods may not function as intended.
Solution
The following skeleton provides a revised example that implements event handling for mouse clicks and key presses in full screen mode:
import java.applet.Applet; 導入java.awt.color; 導入java.awt.displaymode; 導入java.awt.graphics2d; 導入java.awt.graphicsconfiguration; 導入java.awt.graphicsdevice; 導入java.aw.graphicsenvironment; 導入java.awt.Rectangle; 導入java.awt.event.ActionEvent; 導入java.awt.event.ActionListener; 導入java.awt.event.keyevent; 導入java.awt.event.mouseadapter; 導入java.awt.event.mouseevent; 導入java.awt.image.bufferstrategy; 導入javax.swing.jframe; 導入javax.swing.timer; 公共類小程序擴展了applet { 計時器計時器; jframe 框架; displayMode [] displayModes = new DisplayMode [] { 新displayMode (1280,800,32,60) }; BufferStrategy BufferStrategy; 矩形邊界; /** * */ 私有靜態最終長序列= 1L; /** * @param args */ 公共空白in(INIT(){ // ...和以前一樣 // ... //註冊關鍵事件聽眾 this.getInputMap()。 put(鍵keystroke.getKeystroke(keyevent.vk_q,0),“退出”); this.getActionMap()。 put(“退出”,新的摘要(){ @Override 公執行的共void動作(ActionEvent e){ //退出密鑰處理 } (}); //註冊鼠標活動聽眾 this.addmouseMotionListener(新MouseAdapter(){ @Override 公共mousemempasve(mouseevent e){ //鼠標運動處理 } (}); } // ...和以前一樣 // ... } [2
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3