📄 ch37.htm
字号:
<LI>You need to know a font's height so you can properly spacelines of text.<LI>You get a reference to a <TT>FontMetrics</TT> object by callingthe <TT>Graphics</TT> class's <TT>getFontMetrics()</TT> method.The <TT>Font</TT> object for which you want the metrics is themethod's single argument.<LI>Use a <TT>FontMetrics</TT> object when you want to know moredetailed information about a font. The <TT>Font</TT> class offersonly general information about a font.<LI>You can determine the width of a text string by calling the<TT>FontMetrics</TT> class's <TT>stringWidth()</TT>. The method'ssingle argument is the string to measure.<LI>A point is a unit of measurement of a font's height. A pointequals 1/72 of an inch.<LI>Leading is the amount of white space between lines of text.Ascent is the height of a character, from the baseline to thetop of the character. Descent is the size of the area that accommodatesthe descending portions of letters, such as the tail on a lowercase"g."<LI>A font's height is the sum of the font's leading, ascent,and descent.<LI>Use the <TT>new</TT> operator to call the <TT>Font</TT> class'sconstructor. The constructor's three arguments are the font name,style, and size. Styles you can use are any combination of <TT>Font.PLAIN</TT>,<TT>Font.BOLD</TT>, and <TT>Font.ITALIC</TT>.<LI>If the font you request is not available, Java substitutesa default font. This is one reason it's so important to get afont's size after you create the font.</OL><H2><A NAME="Chapter18"><FONT SIZE=5 COLOR=#Ff0000>Chapter 18</FONT></A></H2><OL><LI>The two arguments required by the <TT>Label</TT> class's constructorare the text for the label and the label's alignment value.<LI>When an applet containing labels is resized, the labels automaticallyreposition themselves as appropriate.<LI>After creating controls, you add the controls to the appletwith the <TT>add()</TT> method.<LI>The single argument needed by the <TT>Button</TT> class'sconstructor is the text label for the button.<LI>The values for setting a label's alignment are representedby the <TT>Label</TT> class's <TT>Label.LEFT</TT>, <TT>Label.CENTER</TT>,and <TT>Label.RIGHT</TT> fields.<LI>The <TT>Label</TT> class provides methods for setting thelabel's text and alignment.<LI>You can change a button's text label by calling the <TT>Button</TT>class's <TT>setLabel()</TT> method.<LI>For a button click, the <TT>action()</TT> method receivesa reference to the button object and the selected button's textlabel.<LI>You can determine which button was selected by examining thetext label passed as the <TT>action()</TT> method's second parameter.<LI>Nothing happens when a user clicks a label object, becauselabel objects do not generate events.</OL><H2><A NAME="Chapter19"><FONT SIZE=5 COLOR=#Ff0000>Chapter 19</FONT></A></H2><OL><LI>The three arguments required by the <TT>Checkbox</TT> class'sconstructor are the text for the label, a reference to the <TT>CheckboxGroup</TT>object (or <TT>null</TT>), and checkbox's state (<TT>true</TT>or <TT>false</TT>).<LI>Checkboxes that are set to exclusive mode are also calledradio buttons.<LI>The two arguments needed by the <TT>TextField</TT> class'sconstructor are the default text and the width (in characters)of the control.<LI>To change the state of a checkbox control call the <TT>Checkbox</TT>class's <TT>setState()</TT> method.<LI>When checkboxes are in nonexclusive mode, the user can selectas many checkboxes at a time as he likes. In exclusive mode, theuser can select only one checkbox at a time.<LI>To create a group of checkbox controls (in exclusive mode),you must first create an object of the <TT>CheckboxGroup</TT>class.<LI>You use echo characters whenever the information being enteredinto a textfield control should not be readable on the screen,such as when the user is entering a password.<LI>You select an echo character for a textfield control by callingthe <TT>TextField</TT> class's <TT>setEchoCharacter()</TT> method.<LI>To determine which checkbox generated an event, you cast thefirst parameter sent to the <TT>action()</TT> method to an objectof the <TT>Checkbox</TT> class. You can then call the checkboxobject's <TT>getLabel()</TT> method to get the checkbox's label.</OL><H2><A NAME="Chapter20"><FONT SIZE=5 COLOR=#Ff0000>Chapter 20</FONT></A></H2><OL><LI>The <TT>Choice</TT> class's constructor accepts no arguments.<LI>You add items to a choice menu by calling the <TT>Choice</TT>class's <TT>addItem()</TT> method.<LI>The two arguments needed by the <TT>List</TT> class's constructorare the number of visible rows in the box and a boolean valueindicating whether the control will accept multiple selections.<LI>You add items to a list by calling the <TT>List</TT> class's<TT>addItem()</TT> method.<LI>You would use a <TT>TextArea</TT> control when you need todisplay, and enable the user to edit, more than one line of text.The <TT>TextField</TT> control can display only a single line.<LI>To determine the selected item in a choice menu, examine thesecond parameter of the <TT>event()</TT> method. The selecteditem's text string gets passed to <TT>event()</TT> as that parameter.<LI>You create a multiple-selection list exactly the same wayyou create a single-selection list, except that the value of theconstructor's second argument should be <TT>true</TT> rather than<TT>false</TT>.<LI>You retrieve the selected item from a list by calling the<TT>List</TT> class's <TT>getSelectedItem()</TT> method. If youwant the index of the selected item, call <TT>getSelectedIndex()</TT>.<LI>To create a string containing multiple lines of text, addeach line of the text to the string using the concatenation operator(+). Make sure each line of text ends with the newline character(<TT>\n</TT>).<LI>To retrieve multiple selections from a scrolling list, callthe <TT>List</TT> class's <TT>getSelectedItems()</TT> method.This method returns a string array containing the selected items.<LI>Yes. You can delete items from a list by calling the <TT>List</TT>class's <TT>deleteItem()</TT> or <TT>deleteItems()</TT> methods.</OL><H2><A NAME="Chapter21"><FONT SIZE=5 COLOR=#Ff0000>Chapter 21</FONT></A></H2><OL><LI>The <TT>Scrollbar</TT> constructor's five arguments are thescrollbar's orientation, value, page size, minimum value, andmaximum value.<LI>A canvas is a blank component on which you can draw graphics.<LI>The <TT>Canvas</TT> class's constructor requires no arguments.<LI>Use a page size of zero when you want the scroll box to becentered on the selected value and when you want the user to beable to select a value from anywhere within the entire range.<LI>The easiest way to respond to a scrollbar change is to overridethe <TT>handleEvent()</TT> method. In the method, watch for acomponent target of <TT>Scrollbar</TT>. When you receive an eventmessage from the scrollbar, call the scrollbar's <TT>getValue()</TT>method to determine its setting.<LI>A scrollbar can generate <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>event messages.<LI>To create a custom canvas component, derive a new class fromJava's <TT>Canvas</TT> class.<LI>To draw a canvas's display, override the class's <TT>paint()</TT>method.</OL><H2><A NAME="Chapter22"><FONT SIZE=5 COLOR=#Ff0000>Chapter 22</FONT></A></H2><OL><LI>You can use multiple panels in order to organize sets of controlsin a display. Each panel can have its own layout manager.<LI>Java's five layout managers are <TT>FlowLayout</TT>, <TT>GridLayout</TT>,<TT>BorderLayout</TT>, <TT>CardLayout</TT>, and <TT>GridBagLayout</TT>.<LI>The default layout manager is <TT>FlowLayout</TT>.<LI>The <TT>FlowLayout</TT> manager positions components one afterthe other in rows. When a component won't fit on the current row,the layout manager starts the next row.<LI>The <TT>GridLayout</TT> constructor's four arguments are thenumber of columns and rows in the grid, and the horizontal andvertical spacing of the cells in the grid.<LI>The component positions you can use with the <TT>BorderLayout</TT>manager are North, South, East, West, and Center.<LI>To add a component to an applet using the <TT>BorderLayout</TT>manager, you call a special version of the <TT>add()</TT> methodthat has the position string (North, South, etc.) and a referenceto the component as arguments.<LI>You can use <TT>CardLayout</TT> to simulate property sheetsbecause the layout manager enables you to create "cards"that contains groups of controls. Each card can be displayed separately.<LI>When using the <TT>CardLayout</TT> manager, you can switchfrom one card to another by calling the manager's <TT>first()</TT>,<TT>next()</TT>, <TT>last()</TT>, <TT>previous()</TT>, or <TT>show()</TT>method.<LI>The constraints determine where in the layout a componentwill be placed.<LI><TT>GridBagConstraints.fill</TT> determines whether componentswill stretch vertically or horizontally to fill their cells.<LI>To add a component when using the <TT>GridBagLayout</TT> manager,you first initialize and set the constraints. You then call <TT>add()</TT>as you normally would.</OL><H2><A NAME="Chapter23"><FONT SIZE=5 COLOR=#Ff0000>Chapter 23</FONT></A></H2><OL><LI>To create a frame window, call the <TT>Frame</TT> class'sconstructor with the title of the window as the constructor'ssingle argument.<LI>To display a frame window, call the window's <TT>show()</TT>method.<LI>To determine whether a frame window is visible, call the <TT>isShowing()</TT>method, which returns <TT>true</TT> if the window is currentlyvisible and <TT>false</TT> otherwise.<LI>You use <TT>MenuItem</TT> objects for regular items in a menu,whereas you use <TT>CheckboxMenuItem</TT> objects for items thatcan be checkmarked. <LI>To create a custom frame-window class, you extend the <TT>Frame</TT>class, which itself extends the <TT>Window</TT> class.<LI>To initialize a custom frame window's superclass (<TT>Frame</TT>),you call the <TT>super()</TT> method with the window's title stringas the method's single argument.<LI>You can draw in a frame window in exactly the same way youcan draw in an applet's display area, by overriding the class's<TT>paint()</TT> method.<LI>The six steps for creating a menu bar are create the <TT>MenuBar</TT>object, call <TT>setMenuBar()</TT>, create <TT>Menu</TT> objects,add the <TT>Menu</TT> objects to the <TT>MenuBar</TT> object,create <TT>MenuItem</TT> objects, and add the <TT>MenuItem</TT>objects to the menus.<LI>To add components to a frame window, first create and seta layout manager for the window. Then create and add the componentsto the window as appropriate for the type of layout manager youchose.<LI>You respond to selected menu items by watching for their stringsin the <TT>action()</TT> method, which you must override in thewindow's class.<LI>A menu separator is just a normal <TT>MenuItem</TT> objectthat has a single hyphen as its string.</OL><H2><A NAME="Chapter24"><FONT SIZE=5 COLOR=#Ff0000>Chapter 24</FONT></A></H2><OL><LI>The <TT>Dialog</TT> constructor's three arguments are a referenceto the dialog box's parent frame window, the dialog's title, anda <TT>boolean</TT> value indicating whether the dialog is modal.<LI>You can display or hide a dialog box by calling the class's<TT>show()</TT> or <TT>hide()</TT> methods.<LI>A dialog box must have a frame window as a parent window.The first argument in <TT>Dialog</TT>'s constructor is, in fact,a reference to this frame window.<LI>Every dialog box should have at least an OK button that enablesthe user to dismiss the dialog box.<LI>Modal dialog boxes must be dismissed before the user can continuewith the program. Modeless dialog boxes do not need to be dismissedin order to continue using the program.<LI>In order to add components to a dialog box, you must firstcreate and set a layout manager for the dialog. Then, you createthe controls you need and call the dialog box's <TT>add()</TT>method to add the controls to the layout.</OL><H2><A NAME="Chapter25"><FONT SIZE=5 COLOR=#Ff0000>Chapter 25</FONT></A></H2><OL><LI>The most commonly used mouse event is <TT>MOUSE_DOWN</TT>,which indicates that the user pressed his mouse button.<LI>An applet receives more <TT>MOUSE_MOVE</TT> event messagesthan any other type of mouse event. An applet receives hundredsof these messages as the user moves his mouse over the applet.<LI>The six mouse event messages are <TT>MOUSE_DOWN</TT>, <TT>MOUSE_UP</TT>,<TT>MOUSE_MOVE</TT>, <TT>MOUSE_DRAG</TT>, <TT>MOUSE_ENTER</TT>,and <TT>MOUSE_EXIT</TT>.<LI>The two most important keyboard events are <TT>KEY_PRESS</TT>and <TT>KEY_RELEASE</TT>.<LI>To determine the type of object that generated a event, checkthe <TT>Event</TT> object's <TT>target</TT> data field.<LI>To determine the event type, check the <TT>event</TT> object's<TT>id</TT> field.<LI>The coordinates of the mouse event are passed as the secondand third arguments of the specific mouse method. If you're overriding<TT>handleEvent()</TT>, you can get the mouse coordinates fromthe <TT>Event</TT> object's <TT>x</TT> and <TT>y</TT> data fields.<LI>If the user single-clicked the mouse, the <TT>Event</TT> object's<TT>clickCount</TT> data field will be 1. With a double-click,<TT>clickCount</TT> will be 2.<LI>The two methods associated with the <TT>KEY_PRESS</TT> and<TT>KEY_RELEASE</TT> event messages are <TT>keyDown()</TT> and<TT>keyUp()</TT>, respectively.<LI>The <TT>keyDown()</TT> method receives as arguments an <TT>Event</TT>object and an integer holding the key's ASCII code.<LI>The <TT>mouseDown()</TT> event receives as arguments an <TT>Event</TT>object and the X and Y coordinates of the mouse click.<LI>You use the <TT>SHIFT_MASK</TT> and <TT>CTRL_MASK</TT> constantsto determine whether the user had the Shift or Ctrl keys pressed.<LI>To handle all events in a single method, you should overridethe <TT>handleEvent()</TT> method.</OL><H2><A NAME="Chapter26"><FONT SIZE=5 COLOR=#Ff0000>Chapter 26</FONT></A></H2><OL><LI>Parameterized applets are easier for users who want to addthe applets to their own Web pages. The parameters enable themto customize the applet to more closely fit their needs.<LI>The <TT><PARAM></TT> tag's two parts are <TT>NAME</TT>,which specifies the parameter's name, and <TT>VALUE</TT>, whichassociates a value with the parameter.<LI>To retrieve the value of a parameter, you call the <TT>getParameter()</TT>method, whose single argument is the name of the parameter youwant.<LI>You specify parameter values in the HTML document, as partof the applet's definition.<LI>No. The whole point of parameters is that your applet canchange the way it looks and acts without recompiling.<LI>You can have as many applet parameters as you need.<LI>The <TT>getParameter()</TT> method always returns the givenparameter's value as a string. Therefore, you may need to convertthe returned parameter to an integer or some other data type.<LI>If you fail to define default values for all parameters, yourapplet may generate errors as it tries to use nonexistent or invalidparameter values.</OL>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -