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

📄 ch18.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>developer.com - Reference</TITLE><META NAME="Author" CONTENT="developer.com"><META NAME="Keywords" CONTENT="javascript, ActiveX, VRML, Push, channel,push channel, applet, directory, news, community, developer, internet,intranet, java, java applet, J++, javabeans, java products, tools, javatools, JDK, java development kit, java development environment, JIT,JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, API,browser, java documentation, CDF, Common Desktop Format, RDF, ResourceDescription Framework, Netcaster, Netcast, Castanet, meta-language, metadata,solaris, Solaris, SUN, CGI, Perl, Oracle, database, network, Netscape API,Apache, apache, SSL, mSQL, Netscape, Fasttrack, FastTrack,industry standard, ActiveX bridge, web, internet, site, sitedesign, microsoft, active server pages, asp, iis, nt, win95, 95, html, cgi,frontpage, business, corporate, vbscript, javascript, activex, isapi,database, access, sql, server, www, 3.0, 3.01, 3.02, 3.03, 3.04, 4.0,4.0b2, 4.01, 4.03, 4.04, connection, online, commerce, publisher,publishing, publish, webmaster, dynamic html, dhtml, windows 98, netscape,netscape software, shareware, magazines, jobs, japan, free, freeware, freestuff"><META NAME="description" CONTENT="Developer.com is the leading onlineservice for developers. It includes Gamelan, the official directory for Java, as wellas news, information, tutorials, and directories for other Internet technologiesincluding ActiveX, JavaScript, perl, VRML, Java Beans, push technologies, andother Internet and intranet technologies. Developer.com is also the home ofDeveloper Direct, the Professional Developer's Store, where developers canpurchase and download thousands of Web, authoring and other developmentproducts at some of today's lowest prices."><META NAME="distribution" CONTENT="global"><META NAME="resource-type" CONTENT="document"><META NAME="language" CONTENT="en"></HEAD><BODY BACKGROUND="/images/curve_r-wide.gif" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#DD0000" VLINK="#999999" ALINK="#FF0000"><TABLE><TR><TD VALIGN=TOP WIDTH=120><A HREF="/" ><IMG SRC="/images/masthead_r.gif" WIDTH="100" HEIGHT="78" BORDER="0"ALT="Developer.com Logo"></a></TD><TD><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCBAN.REF/DT=null"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCBAN.REF/DT=null" HEIGHT="60" WIDTH="468" ALT="Click here to support our advertisers"><BR><CENTER>Click here to support our advertisers</CENTER></a></TD></TR></TABLE><!--outer table--><TABLE><!--outer table--><TR><!--outer table--><TD VALIGN="TOP"><!--navbar info --><table><tr><td><A HREF="http://www.developerdirect.com/" ><IMG SRC="/images/button_s.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>SHOPPING</b></font></a></td></tr><tr><td><A HREF="/community/jobs/c_jobs.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>JOB BANK</B></font></a></td></tr><tr><td><A HREF="http://www.classifieds2000.com/cgi-cls/display.exe?Developer+computer+searchcomputercategories"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>CLASSIFIEDS</B></font></a></td></tr><tr><td><A HREF="/directories/directories.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>DIRECTORIES</B></font></a></td></tr><tr><td><A HREF="/reference/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>REFERENCE</B></font></a><br><a href="/reference/r_library.html"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">Online Library</a></font></td></tr><tr><td><A HREF="/classroom/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>LEARNING CENTER</B></font></a></td></tr><tr><td><A HREF="/journal/"><IMG SRC="/images/button_n.gif" width="15" height="11" BORDER="0"><B><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">JOURNAL</font></B></a></td></tr><tr><td><A HREF="/news/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>NEWS CENTRAL</b></font></a></td></tr><tr><td><A HREF="/downloads/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>DOWNLOADS</b></font></a></td></tr><tr><td><A HREF="/community/" ><IMG SRC="/images/button_c.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>COMMUNITY</B></font></a></td></tr><tr><td><A HREF="/calendar/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>CALENDAR</b></font></a></td></tr><tr><td><A HREF="/about/"><IMG SRC="/images/button_a.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>ABOUT US</b></a></font></td></tr></table><table><tr><td><form action="/subscribe/list.cgi" method=post ><img src="/images/rule.gif" width="100" height="2"><br><B><FONT FACE="Arial, Helvetica" SIZE="-1">Journal:</b></font><br><input name='email' value='your e-mail' size="11"><input type='hidden'  name='state' value='Subscribe'></td></tr><tr><td><input type='submit' value='Subscribe'><br><font size="-1">Get the weekly email highlights from the most popular journal for developers!<br><a href="http://www.developer.com/subscribe.html">Current issue</a><img src="/images/rule.gif" width="100" height="2"><input type='hidden' name='name' value='New Subscriber'></FORM></td></tr></table><!--/navbar--><font size="-1"><A HREF="http://www.developer.com">developer.com</a><BR><A HREF="http://www.developerdirect.com">developerdirect.com</a><BR><A HREF="http://www.htmlgoodies.com">htmlgoodies.com</a><BR><A HREF="http://www.javagoodies.com">javagoodies.com</a><br><A HREF="http://www.jars.com">jars.com</a><BR><a href="http://www.intranetjournal.com">intranetjournal.com</a><br><a href="http://www.javascripts.com">javascripts.com</a><br></font><!--outer table--></TD><!--outer table--><TD VALIGN="TOP"><BLOCKQUOTE><P><img src="/images/header_r.gif" width="440" height="50"><P><MAP NAME ="header_r2.gif.map"><AREA SHAPE ="RECT"      HREF ="/reference/author.html"      COORDS ="361,0,440,29"><AREA SHAPE ="RECT"      HREF ="/reference/basic.html"      COORDS ="254,0,360,29"><AREA SHAPE ="RECT"      HREF ="/reference/help.html"      COORDS ="183,0,251,29"><AREA SHAPE ="RECT"      HREF ="/reference/cool.html"      COORDS ="100,0,181,29"><AREA SHAPE ="RECT"      HREF ="/reference/mybook.html"      COORDS ="0,0,100,29"></MAP><MAP NAME ="header_r1.gif.map"><AREA SHAPE ="RECT"      HREF ="/reference/personalize.html"      COORDS ="339,0,439,29"></MAP><p><font face="Arial,Helvetica" size="-1" color="#006666"><A HREF="/reference/r_library.html"><B>All Categories</A> :</B><b><A HREF="/reference/r_java.html">Java</A></b></font><p><META><H1><FONT SIZE=6 COLOR=#Ff0000>Chapter&nbsp;18</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Label and Button Controls</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#Labels">Labels</A><UL><LI><A HREF="#ExampleCreatingaLabel">Example: Creating a Label</A><LI><A HREF="#MethodsoftheLabelClass">Methods of the Label Class</A></UL><LI><A HREF="#Buttons">Buttons</A><UL><LI><A HREF="#ExampleAddingaButtontoanApplet">Example: Adding a Button to an Applet</A><LI><A HREF="#HandlingMultipleButtonEvents">Handling Multiple-Button Events</A><LI><A HREF="#ExampleHandlingMultipleButtonsinanApplet">Example: Handling Multiple Buttons in an Applet</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>Many computer applications require that the user enter informationthat the program needs to perform its tasks. In a windowed, graphicalenvironment, getting information from the user presents a challenge.This is because the currently executing application doesn't controlthe entire screen and so can't just take control of the keyboardor other input device in order to obtain information from theuser. That's why the developers of graphical user interfaces createdthe various controls-such as text boxes, buttons, and menus-thatenable applications to interact with the user without bringingthe rest of the system to a halt.<P>Because Java applets must run under many different windowed operatingsystems, the Java Developer's Kit provides classes for creatingthe basic controls needed by most graphical user interfaces. Thesecontrols include labels, text fields, buttons, radio buttons,check boxes, and choice menus. In this chapter, you learn to programand manage the label and button controls. Other controls are coveredin succeeding chapters.<H2><A NAME="Labels"><FONT SIZE=5 COLOR=#Ff0000>Labels</FONT></A></H2><P>Labels are the simplest of Java's controls, being little morethan text strings you can place anywhere on your applet's displayarea. You create a label by calling the <TT>Label</TT> class'sconstructor, like this:<BLOCKQUOTE><PRE>Label label = new Label(str, align);</PRE></BLOCKQUOTE><P>The <TT>Label</TT> class's constructor takes two arguments, whichare the text to display and an alignment value. The alignmentvalue can be <TT>Label.LEFT</TT>, <TT>Label.CENTER</TT>, or <TT>Label.RIGHT</TT>.After creating the label, you add it to the applet by using the<TT>add()</TT> method, like this:<BLOCKQUOTE><PRE>add(label);</PRE></BLOCKQUOTE><H3><A NAME="ExampleCreatingaLabel">Example: Creating a Label</A></H3><P>Suppose that you want a centered label that displays the text<TT>Java does labels!</TT> To do this, you use a line of Javacode something like this:<BLOCKQUOTE><PRE>Label label = new Label(&quot;Java does labels!&quot;, Label.CENTER);</PRE></BLOCKQUOTE><P>Of course, you can also store the text to display in a <TT>String</TT>object, like this:<BLOCKQUOTE><PRE>String str = &quot;Java does Labels!&quot;;Label label = new Label(str, Label.CENTER);</PRE></BLOCKQUOTE><P>One cool thing about labels is that they automatically retaintheir alignment when the size of an applet's display area changes.For example, Figure 18.1 shows an applet displaying the centeredlabel created in the previous example. In Figure 18.2, the userhas increased the size of the Appletviewer window. The label adjuststo the new space, automatically staying centered.<P><A HREF="f18-1.gif"><B> Figure 18.1 : </B><I>Labels are great for creating text strings that align automatically.</I></A><P><P><A HREF="f18-2.gif"><B> Figure 18.2 : </B><I>Here, the label has repositioned itself so that, in spite of the enlarged window, the label stays centered.</I></A><P><H3><A NAME="MethodsoftheLabelClass">Methods of the Label Class</A></H3><P>After you create a <TT>Label</TT> object, you can use the class'smethods to manipulate the label. Specifically, you can get orset the label's text and alignment, as shown by the methods listedin Table 18.1.<BR><P><CENTER><B>Table 18.1&nbsp;&nbsp;Methods of the </B><I>Label</I><B>class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=272><I><B>Method</B></I></TD><TD WIDTH=251><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=272><TT>int getAlignment()</TT></TD><TD WIDTH=251>Retrieves a label's alignment setting.</TD></TR><TR VALIGN=TOP><TD WIDTH=272><TT>String getText()</TT></TD><TD WIDTH=251>Retrieves a label's test string.</TD></TR><TR VALIGN=TOP><TD WIDTH=272><TT>setAlignment(int align)</TT></TD><TD WIDTH=251>Sets a label's alignment.</TD></TR><TR VALIGN=TOP><TD WIDTH=272><TT>void setText(String label)</TT></TD><TD WIDTH=251>Sets a label's text string.</TD></TR></TABLE></CENTER><P><P>The <TT>getAlignment()</TT> and <TT>getText()</TT> methods haveno arguments. The argument for the <TT>setAlignment()</TT> isthe alignment value <TT>(Label.LEFT, Label.CENTER, or Label.RIGHT)</TT>,and the argument for <TT>setText()</TT> is the new text for thelabel.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>A label's text is displayed using the currently set font. You can create labels that use different fonts by creating and setting the font before creating the label.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="Buttons"><FONT SIZE=5 COLOR=#Ff0000>Buttons</FONT></A></H2><P>In a few previous applets, you used buttons to enable the userto manipulate some feature of the applet. Buttons are a greatway to trigger events in your applet because they're easy to createand manage, and, most importantly, they're easy for the user touse. To create a button, you first call the <TT>Button</TT> class'sconstructor, like this:<BLOCKQUOTE><PRE>Button button = new Button(str);</PRE></BLOCKQUOTE>

⌨️ 快捷键说明

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