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

📄 ch23.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 3 页
字号:
class CustomFrame extends Frame{    CustomFrame(String title)    {        super(title);    }    public void paint(Graphics g)    {        resize(200, 100);        g.drawString(&quot;This is a custom window.&quot;, 30, 30);    }}</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>FrameApplet2 </TT>class from Java's <TT>Applet</TT>class.<BR>    Declare the custom frame-window and button objects.<BR>    Override the <TT>init(&nbsp;)</TT> method.<BR>        Create the custom 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>            Change the button's label to &quot;Hide Window.&quot;<BR>        Tell Java that the message was handled okay.<BR>Derive the <TT>CustomFrame</TT> class from Java's <TT>Frame</TT>class.<BR>        Define the class's constructor.<BR>            Pass the title string on to the <TT>Frame </TT>class.<BR>        Override the window's <TT>paint(&nbsp;)</TT> method.<BR>            Resize the window.<BR>            Display a message in the window.<BR></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>When you compile FrameApplet2, notice that, although both the FrameApplet2 and CustomFrame classes are defined in the same file, the Java compiler creates two class files called FrameApplet2.class and CustomFrame.class.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleAddingComponentstoaWindow">Example: Adding Components to a Window</A></H3><P>Frame windows are just like any other window you see when youcreate an applet. That is, you can add components organized intoa variety of layouts and respond to the user's selections of thesecomponents. In fact, adding layouts and components to a framewindow is not unlike doing the same thing with your applet's mainwindow, which you did in the previous chapter. First you createand set the layout manager, and then you add the components asappropriate for the layout manager you've chosen.<P>Listing 23.3 is an applet called FrameApplet3 that not only createsa custom frame window, but also creates a simple layout for thewindow. This layout contains only a single button; however, youcan create as sophisticated a layout as you like. Feel free toexperiment further with this applet. Figure 23.3 shows FrameApplet3running under Appletviewer, after the user has displayed the framewindow. As you can see in the figure, the window has a singlebutton labeled &quot;Close Window.&quot; When you click this button,the frame window's <TT>action(&nbsp;)</TT> method responds bycalling the <TT>dispose(&nbsp;)</TT> method, which not only removesthe window from the screen, but also destroys the window in memory.<P><A HREF="f23-3.gif"><B> Figure 23.3 : </B><I>This is FrameApplet3 running under Appletviewer.</I></A><P><HR><BLOCKQUOTE><B>Listing 23.3&nbsp;&nbsp;FrameApplet3.java: Adding Componentsto a Window.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class FrameApplet3 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;    }}class CustomFrame extends Frame{    Button button;    CustomFrame(String title)    {        super(title);        FlowLayout layout = new FlowLayout();        setLayout(layout);        button = new Button(&quot;Close Window&quot;);        add(button);    }    public void paint(Graphics g)    {        resize(200, 100);        g.drawString(&quot;This is a custom window.&quot;, 30, 50);    }    public boolean action(Event evt, Object arg)    {        if (arg == &quot;Close Window&quot;)            dispose();        return true;    }}</PRE></BLOCKQUOTE><HR><P>Table 23.1 shows some useful methods you can use to manipulatea frame window. Some of these methods are defined in the <TT>Frame</TT>class, whereas others are inherited from the class's superclasses,such as <TT>Window</TT> and <TT>Container</TT>.<BR><P><CENTER><B>Table 23.1&nbsp;&nbsp;Useful Frame-Window Methods.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=295><I><B>Methods</B></I></TD><TD WIDTH=295><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void add(&nbsp;)</TT></TD><TD WIDTH=295>Adds components to the window.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void dispose(&nbsp;)</TT></TD><TD WIDTH=295>Deletes the window from memory.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>int getCursorType(&nbsp;)</TT></TD><TD WIDTH=295>Returns the window's cursor type.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>Image getIconImage(&nbsp;)</TT></TD><TD WIDTH=295>Returns the window's icon object.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>LayoutManager getLayout(&nbsp;)</TT></TD><TD WIDTH=295>Returns the window's layout manager.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>MenuBar getMenuBar(&nbsp;)</TT></TD><TD WIDTH=295>Returns the window's menu bar object.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>String getTitle(&nbsp;)</TT></TD><TD WIDTH=295>Returns the window's title.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void hide(&nbsp;)</TT></TD><TD WIDTH=295>Removes the window from the screen.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>Boolean isResizable(&nbsp;)</TT></TD><TD WIDTH=295>Returns <TT>true</TT> if the window is resizable.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void remove(&nbsp;)</TT></TD><TD WIDTH=295>Removes components from the window.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void removeAll(&nbsp;)</TT></TD><TD WIDTH=295>Removes all components from the window.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void setCursor(int cursorType)</TT></TD><TD WIDTH=295>Sets the window's cursor type.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void setIconImage(Image image)</TT></TD><TD WIDTH=295>Sets the window's icon object.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void setLayout(&nbsp;)</TT></TD><TD WIDTH=295>Sets the window's layout manager.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void setMenuBar(MenuBar mb)</TT></TD><TD WIDTH=295>Sets the window's menu bar.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void setResizable(boolean </TT></TD><TD WIDTH=295>Sets the window's resizable <TT>resizable)</TT>attribute.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void setTitle(String title)</TT></TD><TD WIDTH=295>Sets the window's title.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void show(&nbsp;)</TT></TD><TD WIDTH=295>Displays the window on the screen.</TD></TR></TABLE></CENTER><P><H2><A NAME="UsingMenuBars"><FONT SIZE=5 COLOR=#Ff0000>Using Menu Bars</FONT></A></H2><P>Most Windows applications have menu bars, which enable the userto more easily locate and select the various commands and optionssupported by the program. The frame windows you create from withinyour applets can also have menu bars. To create a menu bar ina window, you must follow a series of steps:<OL><LI>Create an object of the <TT>MenuBar</TT> class.<LI>Call the window's <TT>setMenuBar(&nbsp;)</TT> method to givethe menu bar to the window.<LI>Create objects of the <TT>Menu</TT> class for each menu youwant in the menu bar.<LI>Call the <TT>MenuBar</TT> object's <TT>add(&nbsp;)</TT> methodto add each menu object to the menu bar.<LI>Create objects of the <TT>MenuItem</TT> or <TT>CheckboxMenuItem</TT>classes for each item you want to appear in the menus.<LI>Call the menus' <TT>add(&nbsp;)</TT> methods in order to addeach item to its appropriate menu.</OL><P>Each of the above steps is covered in the sections that follow.<H3><A NAME="CreatingandSettingaMenuBarObject">Creating and Setting a MenuBar Object</A></H3><P>The first step in adding a menu bar to a frame window is to createthe <TT>MenuBar</TT> object that'll hold all the menus and commands.The menu bar in a window is the horizontal area near the top thatcontains the names of each of the menus in the menu bar. To createthe <TT>MenuBar</TT> object, call the <TT>MenuBar</TT> class'sconstructor, like this:<BLOCKQUOTE><PRE>MenuBar menuBar = new MenuBar(&nbsp;);</PRE></BLOCKQUOTE><P>As you can see, the <TT>MenuBar(&nbsp;)</TT> constructor requiresno arguments.<P>After you've created the <TT>MenuBar</TT> object, you have totell Java to associate the menu bar with the frame window. Youdo this by calling the window's <TT>setMenuBar(&nbsp;)</TT> method:<BLOCKQUOTE><PRE>setMenuBar(menuBar);</PRE></BLOCKQUOTE><P>At this point, you have an empty menu bar associated with thewindow. In the next steps, you add menus to the menu bar.<H3><A NAME="AddingMenustoaMenuBar">Adding Menus to a Menu Bar</A></H3><P>A menu bar is the horizontal area near the top of a window thatcontains the names of the menus contained in the menu bar. Aftercreating and setting the <TT>MenuBar</TT> object, you have themenu bar, but it contains no menus. To add these menus, you firstcreate objects of the <TT>Menu</TT> class for each menu you wantin the menu bar, like this:<BLOCKQUOTE><PRE>Menu fileMenu = new Menu(&quot;File&quot;);Menu editMenu = new Menu(&quot;Edit&quot;);Menu optionMenu = new Menu(&quot;Options&quot;);</PRE></BLOCKQUOTE><P>The <TT>Menu</TT> class's constructor takes a single argument,which is the string that'll appear as the menu's name on the menubar. The example lines above create three menus for the menu bar.<P>After creating the <TT>Menu</TT> objects, you have to add themto the menu bar, which you do by calling the <TT>MenuBar</TT>object's <TT>add(&nbsp;)</TT> method, like this:<BLOCKQUOTE><PRE>menuBar.add(fileMenu);menuBar.add(editMenu);menuBar.add(optionMenu);</PRE></BLOCKQUOTE><P>After Java executes the above three lines, you have a menu barwith three menus, as shown in Figure 23.4. Note, however, thatat this point the menus contain no commands. If you were to clickon the menu names, no pop-up menus would appear.<P><A HREF="f23-4.gif"><B> Figure 23.4 : </B><I>This window's menu bar contains three empty menus.</I></A><P><H3><A NAME="AddingMenuItemstoMenus">Adding Menu Items to Menus</A></H3><P>You may have empty menus at this point, but you're about to remedythat problem. To add items to your menus, you first create objectsof the <TT>MenuItem</TT> or <TT>CheckboxMenuItem</TT> classesfor each menu item you need. To add items to the Options menusyou made previously, you might use Java code something like this:

⌨️ 快捷键说明

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