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

📄 ch21.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 3 页
字号:
    new Scrollbar(Scrollbar.HORIZONTAL, 50, 0, 1, 100);</PRE></BLOCKQUOTE><P>Figure 21.3 shows the scrollbar produced by either of the aboveexamples. Notice that the scroll box is now centered on 50. Whenthe scrollbox is at its maximum position now, it indicates a valueof 100.<P><A HREF="f21-3.gif"><B> Figure 21.3 : </B><I>This scrollbar enables the user to select any value within its minimum and maximum range.</I></A><P><H3><A NAME="RespondingtoaScrollbar">Responding to a Scrollbar</A></H3><P>Because of the many different types of events a scrollbar generates,you need to capture its events in the class's <TT>handleEvent()</TT>method, rather than in <TT>action()</TT>, as you did with othercontrols. What's <TT>handleEvent()</TT>? Like <TT>action()</TT>,<TT>handleEvent()</TT> is a method that's defined in one of yourapplet's superclasses (in this case, <TT>Component</TT>). The<TT>handleEvent()</TT> method handles all the specific event messagesthat are generated by the typical windowing system. Table 21.1lists some of the events to which the <TT>handleEvent()</TT> methodcan respond.<BR><P><CENTER><B>Table 21.1&nbsp;&nbsp;Most Common Events That Can BeHandled by </B><I>handleEvent().</I></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=190><I><B>Event Message</B></I></TD><TD WIDTH=350><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=190>ACTION_EVENT</TD><TD WIDTH=350>An event that can be handled by <TT>action()</TT>.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>GOT_FOCUS</TD><TD WIDTH=350>The component received the focus.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>KEY_PRESS</TD><TD WIDTH=350>A key on the keyboard was pressed.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>KEY_RELEASE</TD><TD WIDTH=350>A key on the keyboard was released.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>LIST_DESELECT</TD><TD WIDTH=350>An item in a list was deselected.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>LIST_SELECT</TD><TD WIDTH=350>An item in a list was selected.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>LOST_FOCUS</TD><TD WIDTH=350>The component lost the focus.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>MOUSE_DOWN</TD><TD WIDTH=350>The user pressed a mouse button.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>MOUSE_DRAG</TD><TD WIDTH=350>The user dragged the mouse pointer.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>MOUSE_ENTER</TD><TD WIDTH=350>The mouse pointer entered an area.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>MOUSE_EXIT</TD><TD WIDTH=350>The mouse pointer left an area.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>MOUSE_MOVE</TD><TD WIDTH=350>The user moved the mouse.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>MOUSE_UP</TD><TD WIDTH=350>The released a mouse button.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>SCROLL_ABSOLUTE</TD><TD WIDTH=350>The user moved a scrollbar's scroll box.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>SCROLL_LINE_DOWN</TD><TD WIDTH=350>The user clicked a scrollbar's down arrow.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>SCROLL_LINE_UP</TD><TD WIDTH=350>The user clicked a scrollbar's up arrow.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>SCROLL_PAGE_DOWN</TD><TD WIDTH=350>The user clicked in a scrollbar below the scroll box.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>SCROLL_PAGE_UP</TD><TD WIDTH=350>The user clicked in a scrollbar above the scroll box.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>WINDOW_DEICONIFY</TD><TD WIDTH=350>The window has been restored from an icon state.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>WINDOW_DESTROY</TD><TD WIDTH=350>The window has been destroyed.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>WINDOW_EXPOSE</TD><TD WIDTH=350>The window has been activated.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>WINDOW_ICONIFY</TD><TD WIDTH=350>The window has been reduced to an icon.</TD></TR><TR VALIGN=TOP><TD WIDTH=190>WINDOW_MOVED</TD><TD WIDTH=350>The window has been moved.</TD></TR></TABLE></CENTER><P><P>As you can see from the list in Table 21.1, there are five eventmessages associated with a scrollbar. These messages are <TT>SCROLL_ABSOLUTE</TT>,<TT>SCROLL_LINE_DOWN</TT>, <TT>SCROLL_LINE_UP</TT>, <TT>SCROLL_PAGE_DOWN</TT>,and <TT>SCROLL_PAGE_UP</TT>. You can respond to these event messageswhen you want to customize how the scrollbar functions. (You learnhow to handle event messages in <A HREF="ch25.htm" >Chapter 25</A>, &quot;Mouse and KeyboardEvents.&quot;) However, you don't need to get into such detailswhen you just need to know where the user set the scrollbar. Instead,you can override <TT>handleEvent()</TT> and check for the scrollbarobject in that method. If the user generates an event with thescrollbar, you can then call the scrollbar's methods to determinewhat change was made. Table 21.2 lists the most useful methodsof the <TT>Scrollbar</TT> class:<BR><P><CENTER><B>Table 21.2&nbsp;&nbsp;Most Useful Methods of the ScrollbarClass.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=262><I><B>Method</B></I></TD><TD WIDTH=328><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>int getLineIncrement()</TT></TD><TD WIDTH=328>Returns the line increment.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>int getMaximum()</TT></TD><TD WIDTH=328>Returns the maximum value.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>int getMinimum()</TT></TD><TD WIDTH=328>Returns the minimum value.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>int getOrientation()</TT></TD><TD WIDTH=328>Returns the orientation.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>int getPageIncrement()</TT></TD><TD WIDTH=328>Returns the page increment.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>int getValue()</TT></TD><TD WIDTH=328>Returns the currently set value.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>int getVisible()</TT></TD><TD WIDTH=328>Returns the page size.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>setLineIncrement(int inc)</TT></TD><TD WIDTH=328>Sets the line increment.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>setPageIncrement(int inc)</TT></TD><TD WIDTH=328>Sets the page increment.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>setValue(int value)</TT></TD><TD WIDTH=328>Sets the selected value.</TD></TR><TR VALIGN=TOP><TD WIDTH=262><TT>setValues(int value, int </TT></TD><TD WIDTH=328>Sets all the slider's values.pgsize, int min, int max)</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleUsingaScrollbarinanApplet">Example: Using a Scrollbar in an Applet</A></H3><P>Now that you know how to use a scrollbar, you can put togetheran applet that demonstrates the concepts involved. Listing 21.1is just such an applet. Called ScrollbarApplet, this applet enablesyou to manipulate a scrollbar with your mouse and see the resultson the screen. Figure 21.4 shows the applet when it first startsup.<P><A HREF="f21-4.gif"><B> Figure 21.4 : </B><I>This is ScrollbarApplet running under Appletviewer.</I></A><P><HR><BLOCKQUOTE><B>Listing 21.1&nbsp;&nbsp;ScrollbarApplet.java: An Applet ThatUses a Scrollbar.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class ScrollbarApplet extends Applet{    Scrollbar scrollbar;    String s;    public void init()    {        BorderLayout layout = new BorderLayout();        setLayout(layout);        scrollbar = new Scrollbar(Scrollbar.HORIZONTAL,            50, 0, 1, 100);        add(&quot;North&quot;, scrollbar);        s = &quot;50&quot;;        Font font = new Font(&quot;TimesRoman&quot;, Font.BOLD, 72);        setFont(font);        resize(200, 200);    }    public void paint(Graphics g)    {        g.drawString(s, 60, 120);    }    public boolean handleEvent(Event evt)    {        if (evt.target instanceof Scrollbar)        {            scrollbar = (Scrollbar)evt.target;            int value = scrollbar.getValue();            s = String.valueOf(value);            repaint();            return true;        }        else        {            boolean result = super.handleEvent(evt);            return result;        }    }}</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>ScrollbarApplet</TT> class from Java's <TT>Applet</TT>class.<BR>    Declare the scrollbar and display string.<BR>    Override the <TT>init()</TT> method.<BR>        Create and set the layout.<BR>        Create and add the scrollbar.<BR>        Initialize the display string, the font, and the appletsize.<BR>    Override the <TT>paint()</TT> method.<BR>        Draw the display string.<BR>    Override the <TT>handleEvent()</TT> method.<BR>            If the toolbar caused the event...<BR>            Cast the target object to a scrollbar.<BR>            Get the scrollbar's current setting.<BR>            Convert the setting to a string.<BR>            Force Java to redraw the applet's display area.<BR>            Tell Java that the event was handled.<BR>        Else if this is not a scrollbar event...<BR>            Send the event on to the superclass's <TT>handleEvent()</TT>.<BR>            Return the result.</BLOCKQUOTE><P>There are two things that you should be sure to notice in ScrollbarApplet.First, notice that the scrollbar isn't just added to the applet.It is, instead, added to the applet after the applet's layoutmanager has been set. This is because the size of the controlbar is dependent upon the active layout manager and how the controlis added to the manager. (For more information on layout managers,see <A HREF="ch22.htm" >Chapter 22</A>, &quot;Panels and the Layout Manager.&quot; Bycreating a BorderLayout manager and adding the horizontal scrollbar

⌨️ 快捷键说明

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