基本信息
文件名称:《Java语言程序设计》第9章 Java图形用户界面-教学课件(非AI生成).ppt
文件大小:9.92 MB
总页数:108 页
更新时间:2025-05-16
总字数:约2.42万字
文档摘要

窗口监听器WindowAdapter,用空方法实现了所有接口中的方法,我们只需要重写相应的方法。publicabstractclassWindowAdapterimplementsWindowListener,WindowStateListener,WindowFocusListener{ publicvoidwindowOpened(WindowEvente){} publicvoidwindowClosing(WindowEvente){} publicvoidwindowClosed(WindowEvente){} ……}frame1.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});*键盘监听器KeyEvent:按键事件,敲击键盘时产生常用方法://returnthecharacterdefinedforthiskeyeventpublicchargetKeyChar()//returntheintegercodeforanactualkeypublicintgetKeyCode()//KeyCodeVK_0~VK_9、VK_A~VK_Z、VK_F1~K_F12//同时按键VK_RIGHTevent.isShiftDown()*键盘监听器KeyListener(略)KeyAdapterpublicabstractclassKeyAdapterimplementsKeyListener{publicvoidkeyPressed(KeyEvente){}publicvoidkeyReleased(KeyEvente){}publicvoidkeyTyped(KeyEvente){} //最常用}*键盘监听器为一个文本区域注册键盘监听器:textArea.addKeyListener(newKeyAdapter(){publicvoidkeyTyped(KeyEvente){textArea.append(e.getChar());}});*鼠标监听器MouseEvent:鼠标点击事件,点击时产生常用方法:if(e.getButton()==MouseEvent.BUTTON1)//左键点击处理程序;if(e.getButton()==MouseEvent.BUTTON2)//中键点击处理程序;if(e.getButton()==MouseEvent.BUTTON3)//右键点击处理程序;//点击次数publicintgetClickCount()*鼠标监听器MouseListener,MouseMotionListener(略)MouseAdapter,MouseMotionAdapterpublicvoidmouseClicked(MouseEvente){}publicvoidmousePressed(MouseEvente){}publicvoidmouseReleased(MouseEvente){}publicvoidmouseEntered(MouseEvente){}publicvoidmouseExited(MouseEvente){}publicvoidmouseDragged(MouseEvente){}publicvoidmouseMoved(MouseEvente){}*鼠标监听器为一个文本区域注册键盘监听器:frame.addMouseListener(newMouseAdapter(){publicvoidmouseClicked(MouseEvente){if(e.getButton()==MouseEvent.BUTTON3) //右键点击窗口时,弹出工具栏}