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

📄 246-250.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=246-250//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="242-246.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="250-255.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">BorderLayout</FONT></H4>
<P>BorderLayout allows you to insert components along the four borders of the Container&#151;&#147;North,&#148; &#147;South,&#148; &#147;East,&#148; &#147;West&#148;&#151;as well as the &#147;Center,&#148; which is the space that&#146;s remaining. You can also control the horizontal and vertical gaps that exist between the components. For example, the following uses a BorderLayout with a horizontal space of 17 pixels and a vertical space of 13 pixels between Components.
</P>
<!-- CODE SNIP //-->
<PRE>
setLayout(new BorderLayout(17,13));
</PRE>
<!-- END CODE SNIP //-->
<P>To add a Component <I>comp</I> to the North border, you need to use another form of the add() method:</P>
<!-- CODE SNIP //-->
<PRE>
add("North",comp);
</PRE>
<!-- END CODE SNIP //-->
<P>Figure 7-9 shows the result of using BorderLayout to arrange five labels, one in each region. Table 7-8 shows the BorderLayout alignments.
</P>
<P><A NAME="Fig9"></A><A HREF="javascript:displayWindow('images/07-09.jpg',400,287 )"><IMG SRC="images/07-09t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-09.jpg',400,287)"><FONT COLOR="#000077"><B>Figure 7-9</B></FONT></A>&nbsp;&nbsp;BorderLayout alignments</P>
<TABLE WIDTH="100%">
<CAPTION ALIGN=LEFT><B>Table 7-8</B> BorderLayout alignments
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">String Argument to Add (String, Component)
<TH WIDTH="50%" ALIGN="LEFT" VALIGN="BOTTOM">Meaning
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>&#147;Center&#148;
<TD>Place component in center
<TR>
<TD>&#147;East&#148;
<TD>Place at right border
<TR>
<TD>&#147;North&#148;
<TD>Place component at top border
<TR>
<TD>&#147;South&#148;
<TD>Place component at bottom border
<TR>
<TD>&#147;West&#148;
<TD>Place component at left border
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="CENTER"><A NAME="Heading18"></A><FONT COLOR="#000077">GridLayout</FONT></H4>
<P>The GridLayout partitions the Container into a grid of equally sized rectangles. For example, to create a grid of two rows and four columns, use
</P>
<!-- CODE SNIP //-->
<PRE>
setLayout(new GridLayout(2,4));
</PRE>
<!-- END CODE SNIP //-->
<P>As with the other LayoutManagers you have seen, you can also specify horizontal and vertical gaps. The following creates a GridLayout with gaps of 13 pixels horizontally and vertically between the grid rectangles:
</P>
<!-- CODE SNIP //-->
<PRE>
setLayout(new GridLayout(2,4,13,13));
</PRE>
<!-- END CODE SNIP //-->
<P>As you add Components, GridLayout will arrange them left to right, top to bottom in the grid. Figure 7-10 shows the result of inserting labels from 1 to 8 in consecutive order, using the GridLayout we just defined.
</P>
<P><A NAME="Fig10"></A><A HREF="javascript:displayWindow('images/07-10.jpg',505,267 )"><IMG SRC="images/07-10t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-10.jpg',505,267)"><FONT COLOR="#000077"><B>Figure 7-10</B></FONT></A>&nbsp;&nbsp;Labels arranged by GridLayout (2,4,13,13)</P>
<H4 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">Other Layout Managers</FONT></H4>
<P>The AWT supports two other LayoutManagers that we won&#146;t discuss here: CardLayout and GridBagLayout. GridBagLayout, in particular, is the most powerful and complicated of all the LayoutManagers, and it allows the most precision in the arrangement of Components. Look at Appendix C, Sources of Java Information, for sources of information about these LayoutManagers.
</P>
<P>Now let&#146;s cover the Container classes in the AWT.</P>
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">Containers</FONT></H4>
<P>In this section, you will learn about the Containers in the AWT: Panel, Frame, and Dialog. You&#146;ll use these containers for the new interface to Alien Landing. In particular, you will put the Alien Landing applet into a Frame, with a custom menu bar and a crosshair cursor. The player will use a Dialog box to alter the difficulty of the game. And to create proper layouts in the Dialog and Frame, you&#146;ll need to use Panels.
</P>
<P>First, let&#146;s discuss Panel, the simplest Container.</P>
<H4 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Panels</FONT></H4>
<P>A Panel is a Container that does not create its own window. For example, Applet is a subclass of Panel, so an applet appears inside the window of the Web browser or the appletviewer, instead of its own window. A Panel may hold Components, including other Panels. Each Panel can use a different LayoutManager, so by nesting Panels, you can create more elaborate layouts.
</P>
<P>Here&#146;s an example. The following applet, shown in Listing 7-2, uses a GridLayout of two rows and three columns. One of the components added is a Panel, which uses FlowLayout to arrange a series of buttons.</P>
<P><B>Listing 7-2</B> PanelExample applet</P>
<!-- CODE //-->
<PRE>
public class PanelExample extends Applet&#123;
  public void init() &#123;
    setLayout(new GridLayout(2,3,13,13));
    Panel p = new Panel();    // define a panel

    // set panel layout
    p.setLayout(new FlowLayout(FlowLayout.RIGHT));

    // insert buttons into panel
    p.add(new Button("Buttons"));
    p.add(new Button("in"));
    p.add(new Button("Panel"));

    // add labels
    add(new Label("Label 1", Label.CENTER));
    add(new Label("Label 2", Label.CENTER));
    add(new Label("Label 3", Label.CENTER));
    add(new Label("Label 4", Label.CENTER));
    add(new Label("Label 5", Label.CENTER));

    // add panel
    add(p);

  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>Figure 7-11 shows the appearance of this applet.
</P>
<P><A NAME="Fig11"></A><A HREF="javascript:displayWindow('images/07-11.jpg',416,591 )"><IMG SRC="images/07-11t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-11.jpg',416,591)"><FONT COLOR="#000077"><B>Figure 7-11</B></FONT></A>&nbsp;&nbsp;PanelExample</P>
<P>Now let&#146;s move on to Frames.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading22"></A><FONT COLOR="#000077">Frames</FONT></H4>
<P>Frame is the Container that provides the greatest functionality. Unlike a Panel, which appears inside another window, a Frame provides a separate, resizable, iconifiable window with a title and menu bar. In addition, you can change the cursor within the bounds of the Frame. Figure 7-12 shows what a minimal Frame looks like.
</P>
<P><A NAME="Fig12"></A><A HREF="javascript:displayWindow('images/07-12.jpg',356,317 )"><IMG SRC="images/07-12t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-12.jpg',356,317)"><FONT COLOR="#000077"><B>Figure 7-12</B></FONT></A>&nbsp;&nbsp;FrameTest</P>
<P>Let&#146;s write an applet that creates the Frame of Figure 7-12. This Frame will also use a crosshair cursor. The first step is to construct the Frame, in the applet&#146;s init() method:
</P>
<!-- CODE SNIP //-->
<PRE>
public class FrameExample extends Applet &#123;
  public void init() &#123;
    // create new Frame, with "FrameTest" as the title
    Frame f = new Frame("FrameTest");
</PRE>
<!-- END CODE SNIP //-->
<P>Now let&#146;s set the size of the Frame:
</P>
<!-- CODE SNIP //-->
<PRE>
f.resize(113,117);
</PRE>
<!-- END CODE SNIP //-->
<P>Let&#146;s also make this Frame unresizable to the user:
</P>
<!-- CODE SNIP //-->
<PRE>
f.setResizable(false);
</PRE>
<!-- END CODE SNIP //-->
<P>Now let&#146;s change the cursor:
</P>
<!-- CODE SNIP //-->
<PRE>
f.setCursor(Frame.CROSSHAIR_CURSOR);
</PRE>
<!-- END CODE SNIP //-->
<P>CROSSHAIR_CURSOR is one of several constants in Frame that define cursors. Other commonly used cursors are HAND_CURSOR and DEFAULT_CURSOR (the usual arrow). You&#146;ll find the complete list of cursors in the Quick AWT Reference section at the end of this chapter.
</P>
<P>Finally, let&#146;s pop up the Frame, by calling show():</P>
<!-- CODE SNIP //-->
<PRE>
f.show();
</PRE>
<!-- END CODE SNIP //-->
<P>The opposite of show() is hide(), which will cause the Frame to disappear from view.
</P>
<P>Listing 7-3 shows the complete applet.</P>
<P><B>Listing 7-3</B> FrameExample applet</P>
<!-- CODE //-->
<PRE>
public class FrameExample extends Applet &#123;
  public void init() &#123;
    // create new Frame, with "FrameTest"  as the title
    Frame f = new Frame("FrameTest");
    f.resize(113,117);
    f.setResizable(false);
    f.setCursor(Frame.CROSSHAIR_CURSOR);
    f.show();
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="242-246.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="250-255.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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