📄 ch20.htm
字号:
Font font = new Font("TimesRoman", Font.BOLD, 24); int height = font.getSize(); g.setFont(font); g.setColor(color); g.drawString("This text is drawn in", 32, 75); g.drawString("the color selected from", 32, 75+height); g.drawString("the above choice menu.", 32, 75+2*height); } public boolean action(Event evt, Object arg) { if (evt.target instanceof Choice) HandleMenu(arg); return true; } protected void HandleMenu(Object item) { if (item == "Black") color = Color.black; else if (item == "Red") color = Color.red; else if (item == "Green") color = Color.green; else color = Color.blue; repaint(); }}</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>ChoiceApplet</TT> class from Java's <TT>Applet</TT>class.<BR> Declare menu and color objects.<BR> Override the <TT>init()</TT> method.<BR> Create the menu object.<BR> Add commands to the menu object.<BR> Add the menu to the applet.<BR> Initialize the starting color for the text.<BR> Override the <TT>paint()</TT> method.<BR> Create and set the display font.<BR> Set the selected color.<BR> Display text in the selected color.<BR> Override the <TT>action()</TT> method.<BR> If the menu caused the event, call the <TT>HandleMenu()</TT>method.<BR> Tell Java that the event was handled okay.<BR> Declare the <TT>HandleMenu()</TT> method.<BR> Set the color field based on the menu selection.<BR> Tell Java to repaint the applet's display area.</BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 20.3 CHOICEAPPLET.htmL: ChoiceApplet's HTMLDocument.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>Applet Test Page</title><h1>Applet Test Page</h1><applet code="ChoiceApplet.class" width=300 height=150 name="ChoiceApplet"></applet></PRE></BLOCKQUOTE><HR><H2><A NAME="ScrollingLists"><FONT SIZE=5 COLOR=#Ff0000>Scrolling Lists</FONT></A></H2><P>Whereas choice menus usually display a set of commands or options,scrolling lists are best used to display a list of items fromwhich the user can choose. For example, you might use a scrollinglist to enable the user to choose a state when filling out anaddress form. The scrolling list in this case would contain all50 states. The user would only need to double-click on his orher state in order to complete that section of the form. Scrollinglists not only make it easy for users to enter information, butalso ensure that the user makes a choice from a valid set of responses.<P>To create a scrolling list, you first call the <TT>List</TT> class'sconstructor, like this:<BLOCKQUOTE><PRE>List list = new List(num, false);</PRE></BLOCKQUOTE><P>The constructor's two arguments are the number of visible linesin the list and a boolean value indicating whether the list willsupport multiple selections. The list created in the precedingwill not allow the user to select more than one item simultaneously.<P>When you have the list object created, you can add items to thelist. You do this by calling the <TT>List</TT> object's <TT>addItem()</TT>method:<BLOCKQUOTE><PRE>list.addItem(str);</PRE></BLOCKQUOTE><P>Here, <TT>str</TT> is the text string for the item to add to thelist.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>You can add as many items as you like to a list. You are not limited to the number of items given as the <TT>List</TT> constructor's first argument. That value specifies only the size of the list box; that is, the value determines how many items are visible on the screen at one time.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleCreatingaSingleSelectionList">Example: Creating a Single-Selection List</A></H3><P>Suppose that you need to create a list that contains musical artistsfrom which the user must select only one. First, you create thelist object, after which you add the names of the artists youwant to include. Finally, you add the list to the applet. Listing20.4 shows the Java code that performs these tasks. Figure 20.4shows the resultant list box.<P><A HREF="f20-4.gif"><B> Figure 20.4 : </B><I>In this list, only one item can be selected at a time.</I></A><P><HR><BLOCKQUOTE><B>Listing 20.4 LST20_4.TXT: Creating a Single-SelectionList Box.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>List list = new List(10, false);list.addItem("Pearl Jam");list.addItem("Dream Theater");list.addItem("Joe Satriani");list.addItem("Oasis");list.addItem("Alanis Morissette");list.addItem("Soul Asylum");list.addItem("The Rembrandts");list.addItem("Smashing Pumpkins");list.addItem("Joan Osborne");list.addItem("Bjork");add(list);</PRE></BLOCKQUOTE><HR><P>Because the <TT>List</TT> constructor's first argument is <TT>10</TT>and there are only 10 items in the list, all of the items arevisible on the screen. Moreover, because the <TT>List</TT> constructor'ssecond parameter is <TT>false</TT>, in the list created by Listing20.4, the user can select only a single artist at a time.<H3><A NAME="ExampleCreatingaMultipleSelectionList">Example: Creating a Multiple-Selection List</A></H3><P>When you display a list of musical artists such as that createdby Listing 20.4, you may want the user to select more than one.In this case, you can create a multiple-selection list, just bychanging the <TT>List</TT> constructor's second argument to <TT>true</TT>,like this:<BLOCKQUOTE><PRE>List list = new List(10, true);</PRE></BLOCKQUOTE><P>As Figure 20.5 shows, the new list enables the user to selectas many artists as he or she likes.<P><A HREF="f20-5.gif"><B> Figure 20.5 : </B><I>Now the user can select more than one artist at a time.</I></A><P><H3><A NAME="ExampleCreatingaScrollingList">Example: Creating a Scrolling List</A></H3><P>You may have noticed that in this chapter's title, the list controlsare called "scrolling lists." So far, though, none ofyour lists have scrolled. In fact, they haven't even had scrollbars. Whenever the size of the list box (given as the constructor'sfirst argument) is greater than or equal to the number of itemsin the list, the list doesn't need to scroll, so no scroll barappears. However, as soon as the number of items exceeds the sizeof the list box, Java enables scrolling.<P>As an example, suppose that you were to change the first lineof Listing 20.4 to this:<BLOCKQUOTE><PRE>List list = new List(5, false);</PRE></BLOCKQUOTE><P>Now, you have a list that can display five items at a time. However,there are 10 items in your list, which means that, in order forthe user to be able to see all then items, the list must scroll.Figure 20.6 shows the resultant scrolling list.<P><A HREF="f20-6.gif"><B> Figure 20.6 : </B><I>The user must scroll this list to see all the items.</I></A><P><H3><A NAME="MethodsoftheIListIClass">Methods of the <I>List</I> Class</A></H3><P>Because there's so much you can do with a scrolling list control,the <TT>List</TT> class has a large set of public methods thatyou can call to manipulate a list. The most useful of these methodsare listed in Table 20.2. Although there are a lot of methodsto learn in the table, the most important are <TT>addItem()</TT>,<TT>getSelectedIndex()</TT>, <TT>getSelectedIndexes()</TT>, <TT>getSelectedItem()</TT>,and <TT>getSelectedItems()</TT>. Using these five methods, youcan create a basic scrolling list and enable the user to makeselections from it. In the next section, in fact, you'll see howto determine which item the user selected.<BR><P><CENTER><B>Table 20.2 Most Useful Methods of the </B><I>List</I><B>Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=295><I><B>Method</B></I></TD><TD WIDTH=295><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void addItem(String item)</TT></TD><TD WIDTH=295>Adds an item to the end of the list.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void addItem(String item, int index) </TT></TD><TD WIDTH=295>Adds an item to a specific position in the list.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>boolean allowsMultipleSelections()</TT></TD><TD WIDTH=295>Returns a <TT>boolean</TT> value indicating whether the list supports multiple selection.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void clear()</TT></TD><TD WIDTH=295>Clears all items from the list.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>int countItems()</TT></TD><TD WIDTH=295>Returns the number of items in the list.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void delItem(int position)</TT></TD><TD WIDTH=295>Deletes the item at the given position from the list.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void delItems(int start, int end)</TT></TD><TD WIDTH=295>Deletes a group of items from the list.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void deselect(int index)</TT></TD><TD WIDTH=295>Deselects the item at the given index.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>String getItem(int index)</TT></TD><TD WIDTH=295>Returns the item at the given index.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>int getRows()</TT></TD><TD WIDTH=295>Returns the size of the list box.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>int getSelectedIndex()</TT></TD><TD WIDTH=295>Gets the index of the selected item.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>int[] getSelectedIndexes()</TT> </TD><TD WIDTH=295>Gets the indexes of a multiple selection.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>String getSelectedItem()</TT></TD><TD WIDTH=295>Returns the selected item in a list.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>String[] getSelectedItems()</TT></TD><TD WIDTH=295>Returns all the items in a multiple selection.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>int getVisibleIndex()</TT></TD><TD WIDTH=295>Returns the index of the last item that was made visible.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>boolean isSelected(int index)</TT></TD><TD WIDTH=295>Returns a <TT>boolean</TT> value indicating whether the item at the given index is selected.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void makeVisible(int index)</TT></TD><TD WIDTH=295>Ensures that the item at the given index is visible.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void replaceItem(String</TT></TD><TD WIDTH=295>Replaces the item at the given <TT>newValue, int index)</TT>index with a new item.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void select(int index)</TT></TD><TD WIDTH=295>Selects the item at the given index.</TD></TR><TR VALIGN=TOP><TD WIDTH=295><TT>void setMultipleSelections(boolean v)</TT></TD><TD WIDTH=295>Toggles the multiple-selection mode.</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleUsingaScrollingListinanApplet">Example: Using a Scrolling List in an Applet</A></H3><P>By now, you've gotten used to working with the list of musicalartists that I've used in the previous few examples. In this example,you put that list to the test by not only creating and displayingthe list in an applet, but also by displaying the user's selection.Listing 20.5 is the source code for the ListApplet applet.<HR><BLOCKQUOTE><B>Listing 20.5 ListApplet.java: An Applet with a ScrollingList.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class ListApplet extends Applet{ List list; public void init() { list = new List(5, false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -