📄 ch19.htm
字号:
add(checkbox1);add(checkbox2);add(checkbox3);</PRE></BLOCKQUOTE><P>Now, when the applet appears, the user sees a list something likethat shown in Figure 19.3. In the figure, the first option isselected. If the user decides to click a different option, thefirst option becomes unselected and the new one selected. Noticethat exclusive checkboxes are round rather than square.<P><A HREF="f19-3.gif"><B> Figure 19.3 : </B><I>Only one exclusive checkbox can<r>be selected simultaneously.</I></A><P><H3><A NAME="ICheckboxIMethods"><I>Checkbox</I> Methods</A></H3><P>Just like other controls supported by Java, the <TT>Checkbox</TT>class features a number of methods that you can call in orderto manipulate the control or obtain information about it. Table19.1 lists the public methods for the <TT>Checkbox</TT> class.<BR><P><CENTER><B>Table 19.1 Public Methods of the </B><I>Checkbox</I><B>Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=289><I><B>Method</B></I></TD><TD WIDTH=301><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>CheckboxGroup getCheckboxGroup()</TT></TD><TD WIDTH=301>Returns the checkbox's group object.</TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>String getLabel()</TT></TD><TD WIDTH=301>Returns the checkbox's label.</TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>boolean getState()</TT></TD><TD WIDTH=301>Returns the checkbox's state.</TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>void setCheckboxGroup(CheckboxGroup g)</TT></TD><TD WIDTH=301>Sets the checkbox's group object.</TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>void setLabel(String label)</TT></TD><TD WIDTH=301>Sets the checkbox's label.</TD></TR><TR VALIGN=TOP><TD WIDTH=289><TT>void setState(boolean state)</TT></TD><TD WIDTH=301>Sets the checkbox's state.</TD></TR></TABLE></CENTER><P><P>The <TT>get</TT> methods listed in Table 19.1 requires no argumentsand return objects of the appropriate type. The <TT>setCheckboxGroup()</TT>requires a reference to a <TT>CheckboxGroup</TT> object as itssingle argument, whereas <TT>setLabel()</TT> and <TT>setState()</TT>require a text string and a <TT>boolean</TT> value, respectively,as their single argument.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>Checkboxes that are set to exclusive mode are also known as <I>radio buttons</I> because, like the station-selection buttons on a radio, only one can be selected at a time.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleHandlingCheckboxesinanApplet">Example: Handling Checkboxes in an Applet</A></H3><P>Depending on what your applet needs to do, checkboxes can be handledin a couple of ways. The easiest way to handle checkboxes is touse their methods to determine the information you need in anapplet. Listing 19.3, for example, is an applet that tracks thestate of a set of checkboxes, displaying their current statesevery time there are changes. Listing 19.4 is the applet's HTMLdocument, and Figure 19.4 shows the applet running under Appletviewer.<P><A HREF="f19-4.gif"><B> Figure 19.4 : </B><I>CheckboxApplet running under Appletviewer.</I></A><P><HR><BLOCKQUOTE><B>Listing 19.3 CheckboxApplet.java: Handling Checkboxesin an Applet.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class CheckboxApplet extends Applet{ Checkbox checkbox1; Checkbox checkbox2; Checkbox checkbox3; public void init() { checkbox1 = new Checkbox("Option 1", null, true); checkbox2 = new Checkbox("Option 2", null, false); checkbox3 = new Checkbox("Option 3", null, false); add(checkbox1); add(checkbox2); add(checkbox3); } public void paint(Graphics g) { Font font = g.getFont(); FontMetrics fontMetrics = g.getFontMetrics(font); int height = fontMetrics.getHeight(); boolean checked = checkbox1.getState(); if (checked) g.drawString("Option1 selected", 20, 120); else g.drawString("Option1 not selected", 20, 120); checked = checkbox2.getState(); if (checked) g.drawString("Option2 selected", 20, 120 + height); else g.drawString("Option2 not selected", 20, 120 + height); checked = checkbox3.getState(); if (checked) g.drawString("Option3 selected", 20, 120 + 2 * height); else g.drawString("Option3 not selected", 20, 120 + 2 * height); } public boolean action(Event evt, Object arg) { repaint(); 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>CheckboxApplet</TT> class from Java's <TT>Applet</TT>class.<BR> Declare three checkbox objects.<BR> Override the <TT>init()</TT> method.<BR> Create the three checkboxes.<BR> Add the checkboxes to the applet's display.<BR> Override the <TT>paint()</TT> method.<BR> Get the height of the active font.<BR> Get the first checkbox's state and display the state.<BR> Get the second checkbox's state and display the state.<BR> Get the third checkbox's state and display the state.<BR> Override the <TT>action()</TT> method.<BR> Force Java to redraw the applet's display.<BR> Tell Java that the event was handled okay.</BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 19.4 CHECKBOXAPPLET.htmL: The HTML DocumentThat Runs CheckboxApplet.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>Applet Test Page</title><h1>Applet Test Page</h1><applet code="CheckboxApplet.class" width=135 height=220 name="CheckboxApplet"></applet></PRE></BLOCKQUOTE><HR><H3><A NAME="RespondingtoaCheckboxEvent">Responding to a Checkbox Event</A></H3><P>Most of Java's user-interface controls generate events when they'reclicked. The checkbox controls are no different. However, unlikebutton controls, which send both a reference to the control andthe control's label as parameters to the <TT>action()</TT> method,checkboxes send only a reference to the control, with the second<TT>action()</TT> parameter always being <TT>true</TT>. This anomalymakes it a little more difficult to handle checkbox controls whenyou need to respond directly to the event generated by the control.<P>To respond to a checkbox event, you must use the <TT>Event</TT>object's <TT>target</TT> field to call the checkbox's methodsin order to determine which checkbox caused the event. If youdon't remember, the <TT>Event</TT> object is passed as the <TT>action()</TT>method's first argument.<P>First, you obtain a reference to the checkbox, like this:<BLOCKQUOTE><PRE>Checkbox checkbox = (Checkbox)evt.target;</PRE></BLOCKQUOTE><P>Then, with a reference to the checkbox in hand, you can call whatever<TT>Checkbox</TT> class members you need in order to determinewhich checkbox caused the event and to deal with the event asappropriately. Probably the best way to determine which checkboxyou're dealing with is to get the object's label, like this:<BLOCKQUOTE><PRE>String label = checkbox.getLabel();</PRE></BLOCKQUOTE><P>You can then compare the returned string to the labels for eachcheckbox object.<H3><A NAME="ExampleHandlingCheckboxEventsinanApplet">Example: Handling Checkbox Events in an Applet</A></H3><P>To demonstrate how to use the previously presented technique inan actual programming situation, you'll now examine the CheckboxApplet2.The source code for the applet is shown in Listing 19.5. To createan HTML document for the applet, start with Listing 19.4 and thenchange all occurrences of <TT>CheckboxApplet</TT> with <TT>CheckboxApplet2</TT>.<HR><BLOCKQUOTE><B>Listing 19.5 CheckboxApplet2.java: Responding toCheckbox Events.</B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class CheckboxApplet2 extends Applet{ Checkbox checkbox1; Checkbox checkbox2; Checkbox checkbox3; public void init() { checkbox1 = new Checkbox("Option 1", null, true); checkbox2 = new Checkbox("Option 2", null, false); checkbox3 = new Checkbox("Option 3", null, false); add(checkbox1); add(checkbox2); add(checkbox3); } public boolean action(Event evt, Object arg) { if (evt.target instanceof Checkbox) ChangeLabel(evt); repaint(); return true; } protected void ChangeLabel(Event evt) { Checkbox checkbox = (Checkbox)evt.target; String label = checkbox.getLabel(); if (label == "Option 1") checkbox.setLabel("Changed 1"); else if (label == "Option 2") checkbox.setLabel("Changed 2"); else if (label == "Option 3") checkbox.setLabel("Changed 3"); else { checkbox1.setLabel("Option 1"); checkbox2.setLabel("Option 2"); checkbox3.setLabel("Option 3"); } }}</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>CheckboxApplet2</TT> class from Java's <TT>Applet</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -