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

📄 ch23.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<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;23</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Windows and Menu Bars</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#DisplayingaWindow">Displaying a Window</A><UL><LI><A HREF="#ExampleDisplayingaWindowinanApplet">Example: Displaying a Window in an Applet</A><LI><A HREF="#ExampleCreatingaWindowClass">Example: Creating a Window Class</A><LI><A HREF="#ExampleAddingComponentstoaWindow">Example: Adding Components to a Window</A></UL><LI><A HREF="#UsingMenuBars">Using Menu Bars</A><UL><LI><A HREF="#CreatingandSettingaMenuBarObject">Creating and Setting a MenuBar Object</A><LI><A HREF="#AddingMenustoaMenuBar">Adding Menus to a Menu Bar</A><LI><A HREF="#AddingMenuItemstoMenus">Adding Menu Items to Menus</A><LI><A HREF="#ExampleUsingaMenuBarinaFrameWindow">Example: Using a Menu Bar in a Frame Window</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>So far in this book, when you created and displayed an applet,you had a single window on the screen. This window was eitherAppletviewer or the browser you were using to display the applet.However, applets can create additional windows. Although you probablywon't use this feature a lot, it's nice to know it's around incase you need it. Moreover, when you create a window in Java,you can add a full-featured menu bar that can contain commands,checked options, and separator objects. In this chapter, you learnto display windows and their menu bars, as well as how to respondto menu commands.<H2><A NAME="DisplayingaWindow"><FONT SIZE=5 COLOR=#Ff0000>Displaying a Window</FONT></A></H2><P>Java's libraries include a class called <TT>Frame</TT> that representsa frame window that you can create and display from within anapplication. To create a frame window, you call the <TT>Frame</TT>class's constructor, like this:<BLOCKQUOTE><PRE>Frame frame = new Frame(&quot;Frame Window&quot;);</PRE></BLOCKQUOTE><P>This constructor's single argument is the window's title, whichwill appear in the window's title bar.<P>When you have the window created, you can display it by callingthe window's <TT>show(&nbsp;)</TT> method. To remove the windowfrom the screen, you call the <TT>hide(&nbsp;)</TT> method. Youcan even size the window by calling <TT>resize(&nbsp;)</TT> orposition the window by calling <TT>move(&nbsp;)</TT>.<H3><A NAME="ExampleDisplayingaWindowinanApplet">Example: Displaying a Window in an Applet</A></H3><P>To demonstrate the basics of using the <TT>Frame</TT> class, Listing23.1 is the source code for an applet that can display a framewindow. When you click the applet's button, the applet displaysa frame window. When you click the button a second time, the appletremoves the window from the screen. Figure 23.1 shows the appletand its frame window. Notice that, when the button is clicked,the button's label switches between &quot;Show Window&quot; and&quot;Hide Window.&quot;<P><A HREF="f23-1.gif"><B> Figure 23.1 : </B><I>Your Java applets can display additional windows.</I></A><P><HR><BLOCKQUOTE><B>Listing 23.1&nbsp;&nbsp;FrameApplet.java: Displaying a FrameWindow.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class FrameApplet extends Applet{    Frame frame;    Button button;    public void init()    {          frame = new Frame(&quot;Frame Window&quot;);          button = new Button(&quot;Show Window&quot;);          add(button);    }    public boolean action(Event evt, Object arg)    {        boolean visible = frame.isShowing();        if (visible)        {            frame.hide();            button.setLabel(&quot;Show Window&quot;);        }        else        {            frame.show();            frame.resize(200, 100);            button.setLabel(&quot;Hide Window&quot;);        }        return true;    }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Tell Java that the applet uses the classes in the <TT>awt</TT>package.<BR>Tell Java that the applet uses the classes in the <TT>applet</TT>package.<BR>Derive the <TT>FrameApplet</TT> class from Java's <TT>Applet </TT>class.<BR>    Declare the frame-window and button objects.<BR>    Override the<TT> init(&nbsp;) </TT>method.<BR>        Create the frame window.<BR>        Create the button component.<BR>        Add the button to the applet.<BR>    Override the <TT>action(&nbsp;)</TT> method.<BR>        Determine whether the window is visible.<BR>        If the window is visible...<BR>            Hide the window.<BR>            Change the button's label to &quot;Show Window.&quot;<BR>        Else if the window is hidden...<BR>            Show the window.<BR>            Set the window's size.<BR>            Change the button's label to &quot;Hide Window.&quot;<BR>        Tell Java that the message was handled okay.</BLOCKQUOTE><H3><A NAME="ExampleCreatingaWindowClass">Example: Creating a Window Class</A></H3><P>When you decide that your applet needs to display a frame window,you're usually better off creating a special class for the window.That way, you have complete control over how the window is constructedand displayed. To create a custom window class, you simply deriveyour window class from Java's <TT>Frame</TT> class. Listing 23.2is a rewritten version of FrameApplet, called FrameApplet2, thatgives the frame window its own class. This new frame window alsotakes advantage of having its own class by overriding the <TT>paint(&nbsp;)</TT>method in order to display text in the window. Figure 23.2 showsFrameApplet2 running under Appletviewer.<P><A HREF="f23-2.gif"><B> Figure 23.2 : </B><I>This is FrameApplet2 running under Appletviewer.</I></A><P><HR><BLOCKQUOTE><B>Listing 23.2&nbsp;&nbsp;FrameApplet2.java: Creating a Frame-WindowClass.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class FrameApplet2 extends Applet{    CustomFrame frame;  Button button;    public void init()    {          frame = new CustomFrame(&quot;Custom Frame Window&quot;);          button = new Button(&quot;Show Window&quot;);          add(button);    }    public boolean action(Event evt, Object arg)    {        boolean visible = frame.isShowing();        if (visible)        {            frame.hide();            button.setLabel(&quot;Show Window&quot;);        }        else        {            frame.show();            button.setLabel(&quot;Hide Window&quot;);        }        return true;    }}

⌨️ 快捷键说明

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