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

📄 ch24.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<TR VALIGN=TOP><TD WIDTH=295><TT>void hide()</TT></TD><TD WIDTH=295>Removes the dialog box from the screen.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>Boolean isModal()</TT></TD><TD WIDTH=295>Returns <TT>true</TT> if the dialog box is modal.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>Boolean isResizable()</TT></TD><TD WIDTH=295>Returns <TT>true</TT> if the dialog box is resizable.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>Component locate(int x, int y)</TT></TD><TD WIDTH=295>Returns the component at the given location.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void remove(Component comp)</TT></TD><TD WIDTH=295>Removes a component from the dialog box.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void removeAll()</TT></TD><TD WIDTH=295>Removes all components.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void setLayout(LayoutManager mgr)</TT></TD><TD WIDTH=295>Sets the dialog's layout manager.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void setResizable(boolean</TT></TD><TD WIDTH=295>Sets the resizable attribute.<TT>resizable)</TT></TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void setTitle(String title)</TT></TD><TD WIDTH=295>Sets the dialog box's title.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void show()</TT></TD><TD WIDTH=295>Displays the dialog box.</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleADialogBoxforTextInput">Example: A Dialog Box for Text Input</A></H3><P>Your last task in this chapter is to put your newly acquired knowledgeof dialog boxes to work. Listing 24.1 is an applet that enablesyou to display a frame window. From the frame window's menu bar,you can select a command that displays a dialog box. This dialogbox contains an OK button for dismissing the dialog box and atext field for entering information. When you dismiss the dialogbox, the text you entered into the text field control appearsin the frame window. Figure 24.1 shows the applet, the frame window,and the dialog box.<P><A HREF="f24-1.gif"><B> Figure 24.1 : </B><I>This is DialogApplet running under Appletviewer.</I></A><P><HR><BLOCKQUOTE><B>Listing 24.1&nbsp;&nbsp;DialogApplet.java: An Applet That Displaysa Dialog Box.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class DialogApplet extends Applet{    DialogFrame frame;    Button button;    public void init()    {          frame = new DialogFrame(&quot;Dialog 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 DialogFrame extends Frame{    MenuBar menuBar;    Dialog dialog;    TextField textField;    String str;    DialogFrame(String title)    {        super(title);        menuBar = new MenuBar();        setMenuBar(menuBar);        Menu menu = new Menu(&quot;Test&quot;);        menuBar.add(menu);        MenuItem item = new MenuItem(&quot;Dialog box&quot;);        menu.add(item);        str = &quot;&quot;;    }    public void paint(Graphics g)    {        resize(300, 250);        g.drawString(&quot;THE TEXT YOU ENTERED IS:&quot;, 70, 50);        g.drawString(str, 70, 70);    }    public boolean action(Event evt, Object arg)    {        if (evt.target instanceof MenuItem) {            if (arg == &quot;Dialog box&quot;)                ShowDialogBox();        }        else if (evt.target instanceof Button)        {            if (arg == &quot;OK&quot;)            {                dialog.hide();                str = textField.getText();                repaint();            }        }        return true;    }    protected void ShowDialogBox()    {        dialog = new Dialog(this, &quot;Test Dialog&quot;, true);        FlowLayout layout = new FlowLayout();        dialog.setLayout(layout);        textField = new TextField(&quot;&quot;, 20);        Button button = new Button(&quot;OK&quot;);        dialog.add(button);        dialog.add(textField);        dialog.show();        dialog.resize(200, 100);    }}</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>DialogApplet</TT> class from Java's <TT>Applet</TT>class.<BR>    Declare the frame-window and button objects.<BR>    Override the <TT>init()</TT> method.<BR>        Create the frame window.<BR>        Create and add the button component.<BR>    Override the <TT>action()</TT> method.<BR>        Determine whether the frame 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.<BR>Define the frame window class.<BR>    Declare objects needed by the class.<BR>    Define the class's constructor.<BR>        Initialize the superclass (Frame).<BR>        Create the window's menu bar.<BR>        Initialize the display string.<BR>    Override the <TT>paint()</TT> method.<BR>        Resize the frame window.<BR>        Draw the display text in the window.<BR>    Override the <TT>action()</TT> method.<BR>        If the &quot;Dialog Box&quot; command was selected, createthe dialog.<BR>        Else if the OK button was selected<BR>            Hide the dialog box.<BR>            Get the contents of the dialog's test field.<BR>            Tell Java to repaint the frame window.<BR>        Tell Java that the event was handled.<BR>    Define the <TT>ShowDialogBox()</TT> method.<BR>        Create the new dialog box and set its layout.<BR>        Create and add components to the dialog box.<BR>        Display and resize the dialog box.<BR></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>In addition to normal dialog boxes, Java supports file dialog boxes for loading and saving files. The file dialogs are represented by the <TT>FileDialog</TT> class. However, because Java applets have extremely limited access to files, file dialog boxes are not covered here.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>You probably won't have much call for dialog boxes in your Javaapplets, but it's always good to know they're there when you needthem. Using dialog boxes in conjunction with a frame window, youcan inform the user of critical problems, as well as obtain informationfrom the user without cluttering your main window with controls.Because dialog boxes are much like other display windows in Java,you can set up layout managers, add components, and control thedialog box using the many methods defined in the <TT>Dialog</TT>class or inherited from the class's superclasses.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What are the three arguments accepted by the <TT>Dialog</TT>class's constructor?<LI>How do you display and hide a dialog box?<LI>Why are frame windows important to dialog boxes?<LI>What is the one control every dialog box should have?<LI>What's the difference between modal and modeless dialog boxes?<LI>How do you add components to the dialog box?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write the code needed to create and display a dialog box withthe title &quot;Test Dialog&quot; and with two button controls.<LI>Write an applet that can display a dialog box with a 2<FONT FACE="Symbol">&#180;</FONT>2grid containing four buttons.<LI>Modify DialogApplet so that the dialog box has both an OKand a Cancel button. If the user clicks the Cancel button, thedialog should be removed from the screen, but the string thatwas entered into the text field control should be ignored. Figure24.2 shows the resultant applet, called DialogApplet2. (You canfind the solution to this exercise in the CHAP24 folder of thisbook's CD-ROM.)</OL><P><A HREF="f24-2.gif"><B> Figure 24.2 : </B><I>This is the DialogApplet2 applet run-<r>ning under Appletviewer.</I></A><P><HR><HR WIDTH="100%"></P></CENTER><!-- reference library footer #1--></CENTER><IMG SRC="/images/rule.gif" WIDTH="460" HEIGHT="5" VSPACE="5"ALT="Ruler image"><br><FONT SIZE="-1">Contact <a href="mailto:reference@developer.com">reference@developer.com</a> with questions or comments.<br><a href="/legal/">Copyright 1998</a> <a href="http://www.earthweb.com" target="_top">EarthWeb Inc.</a>,  All rights reserved.<BR>PLEASE READ THE <a href="/reference/usage.html">ACCEPTABLE USAGE STATEMENT</a>.<BR>Copyright 1998 Macmillan Computer Publishing. All rights reserved.</FONT></BLOCKQUOTE><!--outer table--><TD VALIGN="TOP"><!--right side ads --><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD1.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD1.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD2.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD2.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P></td></tr></table></BODY></HTML>

⌨️ 快捷键说明

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