📄 eventhandlingneueng.html
字号:
<html><!-- InstanceBegin template="/Templates/template.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Java Cooperation: new Java - API</title>
<!-- InstanceEndEditable --><meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<link href="jc_style.css" rel="stylesheet" type="text/css">
<script src="klayers.js"></script>
<script>
function cancelCloseMenu(){
if(self.tm) clearTimeout(tm) // cancel delayed closing
}
function showMenu(){
if(!self.menu || !self.submenu){
menu=layer("menulayer")
submenu=layer("submenulayer")
}
cancelCloseMenu()
submenu.moveTo(menu.getAbsoluteLeft(), menu.getAbsoluteTop() + menu.getHeight()) // move second menu relatively
submenu.show()
}
function initiateHideMenu(){
tm=setTimeout("hideMenu()",400) // 1 second delay to close a submenu
}
function hideMenu(){
submenu.hide()
}
</script>
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
</head>
<body bgcolor="#ffffff" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
<div align="center">
<a name="top"></a>
<table bgcolor="#737373" border="0" cellpadding="0" cellspacing="0" width="618">
<tr>
<td rowspan="8" bgcolor="#000000"><img src="Pics/spacer.gif" width="1" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="131" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="13" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="58" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="94" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="81" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="48" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="23" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="38" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="19" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="108" height="1" border="0" alt=""></td>
<td bgcolor="#000000"><img src="Pics/spacer.gif" width="5" height="1" border="0" alt=""></td>
</tr>
<tr>
<td rowspan="5"><a href="index.html"><img name="fab_r1_c1" src="Pics/fab_r1_c1.gif" width="131" height="71" border="0" alt="Java cooperation home"></a></td>
<td rowspan="6" bgcolor="#737373"></td>
<td colspan="9" height="15" bgcolor="#737373"></td>
<td rowspan="8" bgcolor="#000000"><img src="Pics/spacer.gif" width="1" height="1" border="0" alt=""></td>
</tr>
<tr>
<td colspan="5"><div class="colontitul"><!-- InstanceBeginEditable name="Colontitul" -->Tutorial<!-- InstanceEndEditable --></div></td>
<td colspan="2" bgcolor="#737373"><img name="fab_r2_c8" src="Pics/fab_r2_c8.gif" width="57" height="19" border="0" alt="type and press Enter"></td>
<td colspan="2">
<FORM method=GET action="http://www.google.com/search">
<TABLE bgcolor="#FFFFFF" cellpadding="0" cellspacing="0" height="10px"><tr>
<td height="10" valign="middle">
<INPUT TYPE=text name=q size=18 maxlength=60 value="" style="font-size:12px; color:#000000; border-width: 0px;">
<input type=hidden name=domains value="http://javacooperation.gmxhome.de/">
<input type=hidden name=sitesearch value="http://javacooperation.gmxhome.de/" checked>
</td></tr></TABLE>
</td>
</form>
<td><img src="Pics/spacer.gif" width="1" height="19" border="0" alt=""></td>
</tr>
<tr>
<td colspan="9" bgcolor="#666666"><img name="fab_r3_c3" src="Pics/fab_r3_c3.gif" width="474" height="8" border="0" alt=""></td>
<td><img src="Pics/spacer.gif" width="1" height="8" border="0" alt=""></td>
</tr>
<tr>
<td colspan="6"><a href="TutorialStartEng.html"><img src="Pics/engpanel_01.gif" width="56" height="22" border="0"></a><a href="OnlineSpieleStartEng.html"><img src="Pics/engpanel_02.gif" width="94" height="22" border="0"></a><a href="DownloadsEng.html"><img src="Pics/engpanel_03.gif" width="76" height="22" border="0"></a><a href="LinksEng.html"><img src="Pics/engpanel_04.gif" width="52" height="22" border="0"></a><a href="KontaktEng.html"><img src="Pics/engpanel_05.gif" width="64" height="22" border="0"></a></td>
<td><img name="fab_r4_c9" src="Pics/fab_r4_c9.gif" width="19" height="22" border="0" alt=""></td>
<td width="108" height="22" bgcolor="#666666"><div align="center"><span id="menulayer" style="position: relative; background-color:#666666; width:108; height:20; vertical-align:baseline; border:1px solid #FFFFFF"><a href="#" class="text" onmouseover="showMenu(); return true" onmouseout="initiateHideMenu(); return true">Select language</a></span>
</div>
<div id="submenulayer" style="position: absolute; visibility: hidden; background-color: #666666; width:108px;">
<div class="text" style="border:1px solid #FFFFFF; border-top-width:0; padding:5px">
<a href="indexDeu.html" onmouseover="cancelCloseMenu(); return true" onmouseout="initiateHideMenu(); return true">German</a><br>
<a href="#" onmouseover="cancelCloseMenu(); return true" onmouseout="initiateHideMenu(); return true">English</a><br>
<a href="http://javacooperation.dev.juga.ru/index.html" onmouseover="cancelCloseMenu(); return true" onmouseout="initiateHideMenu(); return true">Russian</a>
</div>
</div>
</td>
<td><img name="fab_r4_c11" src="Pics/fab_r4_c11.gif" width="5" height="22" border="0" alt=""></td>
</tr>
<tr>
<td colspan="9"><img name="fab_r5_c3" src="Pics/fab_r5_c3.gif" width="474" height="7" border="0" alt=""></td>
</tr>
<tr>
<td colspan="11" style="padding:18; background-color:#333333; vertical-align:top;">
<div class="text">
<!-- InstanceBeginEditable name="EditRegion" -->
<h2>EventHandling using the new Java - API </h2>
<p align="justify">Since I got many mails that had to do with compilation warnings due to the deprecated event handling I use within the example applets I decided to write this little chapter about the newer, non deprecated version of the event handling. But the other two chapters about mouse and keyboard event handling will still be available, because on the one hand they go maybe a little bit more into the details than this one will and I still think that the eventhandlin methods I describe there might be easier to understand for an unexperienced programmer. <br>
With this chapter I want to extend the event handling to the "new" method using so called <strong>Listeners </strong> but I won't go into the details of Listeners, Interfaces... . If you want to know more and advanced stuff about this please take a look at the Java - API or a good Java book. </p>
<h3>KeyListener </h3>
<p align="justify">To handle keyboard events you'll find in the package java.awt.event a interface named <em>KeyListener </em>. If you want your applet to handle keyboard events your applet should implement this interface or use a class that implements it. This interface forces you to implement the following methods (names should be self explaining ;-): </p>
<ul>
<li>keyPressed(KeyEvent e)
<li>keyReleased(KeyEvent e)
<li>keyTyped(KeyEvent e) </li>
</ul>
<p align="justify">To allow the applet to listen to keyboard events you have to register the <em>KeyListener </em> class with the applet. This happens by calling the <em>addKeyListener(KeyListener listener) </em> method in the init() - method of the applet class. If the applet implements the interface on its own, just write <em>addKeyListener(this) </em>. More information about the actual key event (which key was pressed...) is available through the KeyEvent instance that comes with the call of the corresponding Listener method. You can find some examples of how to get those detail information of the key event in the example source code to this chapter. </p>
<h3>Mouse events </h3>
<p align="justify">Mouse events are handeled quite the same way as key events with just one exception. There are two different listener interfaces for mouse events(also within the package java.awt.event) which are <em>MouseListener </em> and <em>MouseMotionListener </em>. Both have to be added to the applet exactly the same way as key events if the applet should be able to listen to mouse clicks and mouse move events. To add the listeners to the applet just use the methods <em>addMouseListener(MouseListener listener) </em> and <em>addMouseMotionListener(MouseMotionListener listener) </em>. In details the following methods must be implemented by the listener classes, the names are again self explaining: </p>
<h4>MouseListener </h4>
<ul>
<li>mouseClicked(MouseEvent e)
<li>mouseEntered(MouseEvent e)
<li>mouseExited(MouseEvent e)
<li>mousePressed(MouseEvent e)
<li>mouseReleased(MouseEvent e) </li>
</ul>
<h4>MouseMotionListener </h4>
<ul>
<li>mouseMoved(MouseEvent e)
<li>mouseDragged(MouseEvent e) </li>
</ul>
<p align="justify">Please find details of the information you can get out of the MouseEvent if you take a look at the example applet class to this chapter. <br>
Alright, that's it with the fast run through Java event listeners and the "modern" Event API. I hope that I could help you, all those who read this and don't understand a single line please use the simpler event handling I talked about in the two chapters before and rest assured that I also needed some time to understand what all those damn listeners do ;-). Well, here comes the sourcecode for the example applet and the applet itself (as always). </p>
<p><a href="SourceCodes/EventHandling/Main.java">SourceCode download </a><br>
<a href="Applets/EventHandling/EventHandling.html">Take a look at the applet </a>
<h4>Next Chapter </h4>
<a href="DasersteSpielEng.html">The first game </a> <!-- InstanceEndEditable -->
</div>
</td>
</tr>
<tr>
<td colspan="11" style="background-color:#990000" align="center">
<table width="100%" style="padding:0"><tr>
<td width="88" bgcolor="#993300"><div align="center" style="font-size:10px; color: #FFFFFF;"> <a href="#top"> to top </a></div></td>
<td><div align="center" style="font-size:10px; color: #FFFFFF;">
<a href="mailto:javacooperation@gmx.de">Fabian Birzele</a>, 2001-2004.<br>
web-design: <a href="http://www.freehand.str.ru/">Vadim Murzagalin</a>, 2004.
</div></td>
<td width="88">
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
<!-- InstanceEnd --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -