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

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

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="238-242.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="246-250.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>To access or change the selected checkbox of a group, use the CheckboxGroup methods getCurrent() and setCurrent(Checkbox box):
</P>
<!-- CODE SNIP //-->
<PRE>
// gets label of current selection
String s = vote.getCurrent().getLabel();

// changes current selection
vote.setCurrent(bird);
</PRE>
<!-- END CODE SNIP //-->
<P>The CheckboxGroup methods are listed in Table 7-3.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-3</B> CheckboxGroup methods
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Class
<TH WIDTH="70%" ALIGN="LEFT" VALIGN="BOTTOM">Methods
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>CheckboxGroup
<TD>public CheckboxGroup();
<TR>
<TD>
<TD>public Checkbox getCurrent();
<TR>
<TD>
<TD>public synchronized void
<TR>
<TD>
<TD>setCurrent(Checkbox box);
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Labels</FONT></H4>
<P>A Label displays text on a single line. Use labels to create headings or to annotate Components, such as CheckboxGroups, that don&#146;t have labels. Use a text field if you require user input.
</P>
<P>You can specify the alignment of the label using the class constants CENTER, LEFT, and RIGHT. Here are a few examples:</P>
<!-- CODE SNIP //-->
<PRE>
// default: left justify
Label l1 = new Label("Junipers and irises");

// a centered label
Label l2 = new Label("Iodine with jujube", Label.CENTER);
</PRE>
<!-- END CODE SNIP //-->
<P>You can access the text of the label with getText() and setText(String). Furthermore, you can get and set the alignment of the label as well, using getAlignment() and setAlignment(int). For example:
</P>
<!-- CODE SNIP //-->
<PRE>
// right justify l1
l1.setAlignment(Label.RIGHT);
</PRE>
<!-- END CODE SNIP //-->
<P>The Label methods are listed in Table 7-4.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-4</B> Label methods
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Class
<TH WIDTH="70%" ALIGN="LEFT" VALIGN="BOTTOM">Methods
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>Label
<TD>public Label();
<TR>
<TD>
<TD>public Label(String label);
<TR>
<TD>
<TD>public Label(String label,int alignment);
<TR>
<TD>
<TD>public int getAlignment();
<TR>
<TD>
<TD>public String getText();
<TR>
<TD>
<TD>public void setAlignment(int alignment);
<TR>
<TD>
<TD>public void setText(String label);
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Text Fields</FONT></H4>
<P>A <I>text field</I> allows the user to input text from the keyboard. There are a few TextField constructors available, which allow you the option of specifying the width of the field and the String that it shows once it is initialized. Here are some examples:</P>
<!-- CODE SNIP //-->
<PRE>
// empty text field holding 0 characters
TextField t0 = new TextField();

// empty text field holding 13 characters
TextField t1 = new TextField(13);

// initialized to the string, holding 17 characters
Textfield t2 = new TextField("I love jellybeans",17);
</PRE>
<!-- END CODE SNIP //-->
<P>You can access the contents of the TextField using getText() and setText(String):
</P>
<!-- CODE SNIP //-->
<PRE>
t1.setText("Chocolate");
String s = t2.getText();
</PRE>
<!-- END CODE SNIP //-->
<P>The TextField methods are listed in Table 7-5.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-5</B> TextField methods
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Class
<TH WIDTH="70%" ALIGN="LEFT" VALIGN="BOTTOM">Methods
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>TextField
<TD>public Textfield();
<TR>
<TD>
<TD>public Textfield(int size);
<TR>
<TD>
<TD>public Textfield(String text,int size);
<TR>
<TD>
<TD>public String getText();  // inherited from TextComponent
<TR>
<TD>
<TD>public setText(String text); //
<TR>
<TD>
<TD>inherited from TextComponent
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>Now let&#146;s see how to place these widgets on the screen by using LayoutManagers.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">LayoutManagers</FONT></H4>
<P>LayoutManagers are responsible for arranging Components within a Container, such as an Applet. To use a LayoutManager, you need methods defined in Container. The basic Container methods you&#146;ll need are listed in Table 7-6.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-6</B> Basic Container methods
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="60%" ALIGN="LEFT">Container Method
<TH WIDTH="40%" ALIGN="LEFT" VALIGN="BOTTOM">Purpose
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>public Component add(Component c);
<TD>Adds the specified component
<TR>
<TD VALIGN="TOP">public Component add(String s,Component c);
<TD>Adds component with String argument
<TR>
<TD VALIGN="TOP">public void setLayout(LayoutManager m);
<TD>Uses the specified layout manager
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>Here&#146;s the sequence of events. First, tell the Container to use a particular LayoutManager:
</P>
<!-- CODE SNIP //-->
<PRE>
setLayout(LayoutManager);
</PRE>
<!-- END CODE SNIP //-->
<P>Then, add the Components to the Container:
</P>
<!-- CODE SNIP //-->
<PRE>
add(Component);
</PRE>
<!-- END CODE SNIP //-->
<P>For example, the following statement adds a Button:
</P>
<!-- CODE SNIP //-->
<PRE>
add (new Button("A Button"));
</PRE>
<!-- END CODE SNIP //-->
<P>The LayoutManager determines the onscreen placement of the Components within the Container.
</P>
<P>Let&#146;s discuss the three LayoutManagers we&#146;ll use: FlowLayout, BorderLayout, and GridLayout.</P>
<H4 ALIGN="CENTER"><A NAME="Heading16"></A><FONT COLOR="#000077">FlowLayout</FONT></H4>
<P>This LayoutManager places Components in rows from left to right, in the same way that a word processor lays out words. If the Components do not all fit in a single row, FlowLayout starts a new row below. FlowLayout supports three alignment modes&#151;CENTER, LEFT, and RIGHT&#151;and also allows you to control the horizontal and vertical gaps between each Component. For example:
</P>
<!-- CODE SNIP //-->
<PRE>
setLayout(new FlowLayout(FlowLayout.RIGHT));
</PRE>
<!-- END CODE SNIP //-->
<P>will align the components along the right border of the Container. Figure 7-7 shows a group of buttons arranged this way.
</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/07-07.jpg',603,231 )"><IMG SRC="images/07-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-07.jpg',603,231)"><FONT COLOR="#000077"><B>Figure 7-7</B></FONT></A>&nbsp;&nbsp;FlowLayout with RIGHT alignment</P>
<P>The following centers components, with gaps in between of 17 pixels horizontally and 33 pixels vertically:
</P>
<!-- CODE SNIP //-->
<PRE>
setLayout(new FlowLayout(FlowLayout.CENTER,17,33));
</PRE>
<!-- END CODE SNIP //-->
<P>Figure 7-8 shows where the buttons appear with this layout.
</P>
<P><A NAME="Fig8"></A><A HREF="javascript:displayWindow('images/07-08.jpg',678,352 )"><IMG SRC="images/07-08t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-08.jpg',678,352)"><FONT COLOR="#000077"><B>Figure 7-8</B></FONT></A>&nbsp;&nbsp;FlowLayout with CENTER alignment and gaps</P>
<P>Table 7-7 shows the FlowLayout alignments.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-7</B> FlowLayout alignments
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">Constant in FlowLayout
<TH WIDTH="50%" ALIGN="LEFT" VALIGN="BOTTOM">Meaning
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>FlowLayout.CENTER
<TD>Center the components
<TR>
<TD>FlowLayout.LEFT
<TD>Left-justify the components
<TR>
<TD>FlowLayout.RIGHT
<TD>Right-justify the components
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="238-242.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="246-250.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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