⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 238-242.html

📁 java game programming e-book
💻 HTML
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Creating Customizable Games with the AWT</TITLE>
<!-- HEADER --><STYLE type="text/css">  <!-- A:hover  { 	color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) {         var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></script><SCRIPT><!--function popUp(url) {        var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) {  /* get the query value */  var i = escape(fm.query.value);  if (i == "") {      alert('Please enter a search word or phrase');      return false;  }                  /* query is blank, dont run the .jsp file */  else return true;  /* execute the .jsp file */}//--></script></HEAD><BODY> 
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
    <font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
    <br>
    <font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
    <br>
    Sams,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</td>
</tr>
</table>
<P>

<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=7//-->
<!--PAGES=238-242//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="231-238.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="242-246.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Defining the Action Handler in the Container</FONT></H4>
<P>The ButtonTest applet contains the Button object that the user clicks. The following action() method, defined in the ButtonTest class, will catch button clicks.
</P>
<!-- CODE SNIP //-->
<PRE>
public boolean action(Event e,Object o) &#123;
  if (e.target instanceof Button) &#123;
    System.out.println("Button Click!");
    return true;        // don't pass event
  &#125;
  else return false;   // else pass event to parent container
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>instanceof</I> operator checks if the event target is actually a button. If it is, action() returns <I>true</I> to prevent the action event from propagating further. If action() returns <I>false, </I>the action event passes to the parent container. The same protocol applies to the event handlers we discussed in Chapter 4, Adding Interactivity, such as mouseUp(). Figure 7-5 illustrates this protocol.</P>
<P><A NAME="Fig5"></A><A HREF="javascript:displayWindow('images/07-05.jpg',561,381 )"><IMG SRC="images/07-05t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-05.jpg',561,381)"><FONT COLOR="#000077"><B>Figure 7-5</B></FONT></A>&nbsp;&nbsp;Action event handling</P>
<P>You will notice that action() takes two arguments: the event itself and an Object. The particular action event and Object that is passed depends on the component that triggered the action. You&#146;ll find a table of action events at the end of this chapter.
</P>
<P>Since actions are events, they can also be processed by the handleEvent() method (which is covered in Chapter 4). For example, the following is equivalent to the action() method above:</P>
<!-- CODE //-->
<PRE>
public boolean handleEvent(Event e) &#123;
  // if action event occurs
  if (e.id == Event.ACTION_EVENT) &#123;
    if (e.target instanceof Button) &#123;
    System.out.println("Button Click!");
    return true;       // don't pass event
    &#125;
  &#125;
  else return false;   // else pass event to parent container
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading7"></A><FONT COLOR="#000077">Defining the Action Handler in the Component</FONT></H4>
<P>There is another place where the action handler can be defined. You can define the action() (or handleEvent()) method in the Component itself by creating a new subclass. For example, let&#146;s derive MyButton from the Button class. MyButton will echo a button click to the screen:
</P>
<!-- CODE //-->
<PRE>
public class MyButton extends Button &#123;
  public MyButton(String s) &#123;
    super(s);
  &#125;

  public boolean action(Event e,Object o) &#123;
    if (e.target == this) &#123; // if this object is target
      System.out.println("Button Click!");
      return true;           // don't pass event
    &#125;
    else return false;      // else pass event to parent container
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>You can instantiate MyButton in an applet. This button will echo clicks automatically, without an event handler in the applet. If the button needs to pass the event to its parent container (the applet), its action() method should return <I>false</I>. Defining the event handler in the MyButton allows you to create many buttons that exhibit the same behavior.</P>
<P>Although the AWT contains many possible widgets, layouts, and containers, you will be using the same pattern, seen in these examples, to create your own interfaces and actions!</P>
<P>Now, let&#146;s cover the components, containers, and layout managers of the AWT. We&#146;ll use these classes to create the customization dialog for Alien Landing.</P>
<H3><A NAME="Heading8"></A><FONT COLOR="#000077">Using Components, LayoutManagers, and Containers</FONT></H3>
<P>This is a quick tour of the basic components, layout managers, and containers you&#146;ll find useful in creating graphical game interfaces. The Quick AWT Reference section at the end of this chapter lists the classes and methods described here.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Components</FONT></H4>
<P>Let&#146;s discuss the following components: Button, Checkbox, CheckboxGroup, Label, and TextField. Each of these can be seen in Figure 7-6.
</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/07-06.jpg',667,475 )"><IMG SRC="images/07-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-06.jpg',667,475)"><FONT COLOR="#000077"><B>Figure 7-6</B></FONT></A>&nbsp;&nbsp;Basic components</P>
<H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Buttons</FONT></H4>
<P>You can create a Button with a label on it, as you have already seen, or one without a label:
</P>
<!-- CODE SNIP //-->
<PRE>
Button unlabeled = new Button();
Button OK = new Button("OK");  // labeled with OK
</PRE>
<!-- END CODE SNIP //-->
<P>There are two Button methods you can use to access the label:
</P>
<!-- CODE SNIP //-->
<PRE>
String s = OK.getLabel();      //  returns the label
unlabeled.setLabel("No Way"); //  sets the label
</PRE>
<!-- END CODE SNIP //-->
<P>Clicking on a Button creates an action event, with the button label (a String) as the argument to action().The Button methods are listed in Table 7-l.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-1</B> Button methods
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Class
<TH WIDTH="70%" ALIGN="LEFT" VALIGN="BOTTOM">Method
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>Button
<TD>public Button();
<TR>
<TD>
<TD>public Button(String label);
<TR>
<TD>
<TD>public String getLabel();
<TR>
<TD>
<TD>public void setLabel(String label);
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Checkboxes</FONT></H4>
<P>A Checkbox records a binary choice. You can create labeled as well as unlabeled checkboxes:
</P>
<!-- CODE SNIP //-->
<PRE>
Checkbox unlabeled = new Checkbox();
Checkbox chicken = new Checkbox("I like chicken.");
</PRE>
<!-- END CODE SNIP //-->
<P>As with buttons, you can access the Checkbox label using getLabel() and setLabel(String). To access the state of a Checkbox, use the methods setState(boolean) and getState():
</P>
<!-- CODE SNIP //-->
<PRE>
Boolean b = chicken.getState()  // true if chicken is selected
chicken.setState(false);        // deselect chicken
</PRE>
<!-- END CODE SNIP //-->
<P>Clicking on a Checkbox causes an action event, with Checkbox&#146;s state as the argument to action(). The Checkbox methods are listed in Table 7-2.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-2</B> Checkbox methods
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Class
<TH WIDTH="70%" ALIGN="LEFT" VALIGN="BOTTOM">Method
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>Checkbox
<TD>public Checkbox();
<TR>
<TD>
<TD>public Checkbox(String label);
<TR>
<TD>
<TD>public Checkbox(String label, CheckboxGroup group,boolean state);
<TR>
<TD>
<TD>public String getLabel();
<TR>
<TD>
<TD>public boolean getState();
<TR>
<TD>
<TD>public void setLabel(String label);
<TR>
<TD>
<TD>public void setState(boolean state);
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">Checkbox Groups</FONT></H4>
<P>Sometimes you will want to group checkboxes so that only one can be selected out of the entire group. These are also known as <I>radio buttons</I>.</P>
<P>Here&#146;s an example. Let&#146;s create a CheckboxGroup that allows users to vote for their favorite Internet guru. The first step is to declare a CheckboxGroup:</P>
<!-- CODE SNIP //-->
<PRE>
CheckboxGroup vote = new CheckboxGroup();
</PRE>
<!-- END CODE SNIP //-->
<P>Then create the constituent checkboxes, using <I>vote</I> as the second argument and the initial state of the checkbox as the third. Only one Checkbox in a CheckboxGroup can be selected at a time.</P>
<!-- CODE SNIP //-->
<PRE>
Checkbox gosling = new Checkbox("James Gosling", vote, false);
Checkbox lee = new Checkbox("Tim Berners-Lee", vote, false);
Checkbox clark = new Checkbox("Jim Clark", vote, false);
Checkbox gates = new Checkbox("Bill Gates", vote, false);
Checkbox bird = new Checkbox("Big Bird", vote, true);
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="231-238.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="242-246.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -