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

📄 ch4.htm

📁 JAVA Developing Professional JavaApplets
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dispose();&nbsp;&nbsp;//Erase frame<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returntrue;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case e.ACTION_EVENT:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Process menu selection...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(e.target instanceof MenuItem) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Get the name of the menu selection..<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringmenuName = e.arg.toString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Dispose of frame if quit is chosen...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(menuName.equals(&quot;Quit&quot;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Otherwise, set the cursor...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(menuName.equals(&quot;Default&quot;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setCursor(Frame.DEFAULT_CURSOR);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(menuName.equals(&quot;Wait&quot;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setCursor(Frame.WAIT_CURSOR);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(menuName.equals(&quot;Hand&quot;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setCursor(Frame.HAND_CURSOR);<BR>&nbsp;&nbsp;&nbsp;&nb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nbsp;returntrue;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnfalse;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>}</TT></BLOCKQUOTE><HR><P>A MenuItem can be removed from a Menu with the <TT>remove()</TT>method, much as it is in the MenuBar class. A separator, dividingmenu items, can be added with the <TT>addSeparator()</TT>method of the Menu class. This is useful for menus that have differentcategories of options.<P>The current example also illustrates how to process menu selections.When a menu item is selected, an <TT>ACTION_EVENT</TT>is issued. The <TT>instanceof</TT>operator can be used in the handler to verify that the targetof the action is a menu. By taking the Event argument (the <TT>arg</TT>variable) and converting it to a string, the program can get thename of the menu item. Through the name, the appropriate courseof action can then be taken. In this example, the Quit menu itemforces the frame to shut down by using the <TT>dispose()</TT>method. The other menu choices result in a new state for the cursor.<P>It should be mentioned that the CheckboxMenuItem class can beimplemented for menus that need to use checkmarks to indicatewhether the item has been selected. This is handy for menu itemsthat toggle.<H3><A NAME="Dialogs">Dialogs</A></H3><P>Like the Frame class, the Dialog class is a subclass of Window.Dialogs differ from Frames in a couple of subtle ways, however.The most important of these differences is that Dialogs can be<I>modal</I>. When a modal Dialog is displayed, input to otherwindows in the Applet is blocked until the Dialog is disposed.This feature points to the general purpose of Dialogs, which isto give the user a warning or a decision to be made before theprogram can continue. Although non-modal, or <I>modeless,</I>Dialogs are supported, most Dialogs are modal.<P>There are two constructors for the Dialog class. Both take a Frameobject as a parameter; they also take a boolean flag, which indicateswhether the dialog should be modal. If the flag is set to true,the dialog is modal. The constructors differ only in a parameterthat specifies whether the dialog should have a title caption.It is this constructor that is used in the example that follows.<P>Figure 4.2 shows a variation of the previous applet, except thata Dialog is used to change the cursor state. Listing 4.3 showsthe code for the Dialog class, called ChangeCursorDialog. TheFrame class (still called FrameMenuCursor from the previous example)declares the dialog and instantiates it as follows:<P><A HREF="f4-2.gif" ><B>Figure 4.2 </B>: <I>Using Dialog to change the state of the cursor</I></A><BLOCKQUOTE><TT>ChangeCursorDialog dlg;<BR>dlg = new ChangeCursorDialog(this,true,&quot;Change the cursor&quot;);</TT></BLOCKQUOTE><P>The menu for this application is different from the previous example.The frame constructs the menu as follows:<BLOCKQUOTE><TT>// First create the menu bar<BR>MenuBar mbar = new MenuBar();<BR>setMenuBar(mbar); // Attach to the frame...<BR><BR>// Add the File submenu...<BR>Menu m = new Menu(&quot;File&quot;);<BR>mbar.add(m);&nbsp;&nbsp;// Add to menu bar<BR>// Add Dialog to the submenu...<BR>m.add(new MenuItem(&quot;Cursor Dialog&quot;));<BR>// Add a separator<BR>m.addSeparator();<BR>// Add Quit to the submenu...<BR>m.add(new MenuItem(&quot;Quit&quot;));<BR></TT>Note that a separator is added to divide the two menuitems.<BR>When the Cursor Dialog menu item is chosen, the dialog box ispresented with the following code:<BR><TT>if (menuName.equals(&quot;Cursor Dialog&quot;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;dlg.show(); // Make the dialog visible...<BR></TT>Listing 4.3. Code for a Dialog to change the cursorstate.<BR><TT>import java.awt.*;<BR>import java.lang.*;<BR>import java.applet.*;<BR><BR>// Dialog that presents a grid of buttons<BR>// for choosing the Frame cursor. A Cancel<BR>// button exits the dialog...<BR>class ChangeCursorDialog extends Dialog {<BR>FrameMenuCursor fr;<BR>// Create the dialog and store the title string...<BR>public ChangeCursorDialog(Frame parent,boolean modal,String title){<BR> // Create dialog with title<BR> super(parent,title,modal);<BR> fr = (FrameMenuCursor)parent;<BR> // The layout is Grid layout...<BR> setLayout(new GridLayout(3,2));<BR> // Add the button options<BR> add(new Button(&quot;Default&quot;));<BR> add(new Button(&quot;Wait&quot;));<BR> add(new Button(&quot;Hand&quot;));<BR> add(new Button(&quot;Move&quot;));<BR> add(new Button(&quot;Text&quot;));<BR> add(new Button(&quot;Cancel&quot;));<BR> // Pack and size for display...<BR> pack();<BR> resize(300,200);<BR>}<BR>// Look for button selections to<BR>// change the cursor...<BR>public boolean action(Event e,Object arg) {<BR>&nbsp;&nbsp;&nbsp;&nbsp; // If button was selected then exit dialog..<BR>if (e.target instanceof Button) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// And possiblychange the cursor...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (arg.equals(&quot;Default&quot;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fr.setCursor(Frame.DEFAULT_CURSOR);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (arg.equals(&quot;Wait&quot;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fr.setCursor(Frame.WAIT_CURSOR);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (arg.equals(&quot;Hand&quot;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fr.setCursor(Frame.HAND_CURSOR);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (arg.equals(&quot;Move&quot;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fr.setCursor(Frame.MOVE_CURSOR);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (arg.equals(&quot;Text&quot;))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fr.setCursor(Frame.TEXT_CURSOR);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;return false;<BR>}<BR>}</TT></BLOCKQUOTE><P>Another AWT class called FileDialog is a subclass of Dialog. Itis used for creating stock Load and Save dialog boxes. The upcomingtutorial has examples of the FileDialog class.<H3><A NAME="Colors">Colors</A></H3><P>The Color class is used to set an object's colors. This classrepresents a color as a combination of RGB values. RGB is a 24-bitrepresentation of a color composed of red, green, and blue bytevalues. This 24-bit representation is virtual since many platformsdo not support 24-bit color. In these situations, Java maps thecolor to the appropriate value; this will usually be an indexinto a palette.<P>The Color class defines a set of stock color values, implementedas static variables. For example, Color.red is available withan RGB value of 255,0,0. Other colors supported are white, black,gray, green, blue, yellow, magenta, and cyan. Various Color constructorscan also be used to define other colors. For example, the statement<BLOCKQUOTE><TT>Color col = new Color(0,0,120);</TT></BLOCKQUOTE><P>produces a dark blue color. Other methods, such as <TT>darker()</TT>and <TT>brighter()</TT>, can alsobe used to create a new Color object from an existing one.<P>There are generally two approaches to tying a color to an object.The first approach is to use methods provided by the Componentclass. The <TT>setBackground()</TT>and <TT>setForeground()</TT> methodsare used to set the colors of an object in the two respectivepositions. For example, the following code creates a Panel objectand sets its background to white:<BLOCKQUOTE><TT>Panel p = new Panel();<BR>p.setBackground(Color.white);</TT></BLOCKQUOTE><P>You can use the respective methods prefixed by &quot;get&quot;to return the background and foreground colors of a component.<P>The other approach to setting a color occurs in the <TT>paint()</TT>method. Recall that a Graphics object is passed to this method.The <TT>setColor()</TT> method canthen be used to set the color of the next items to be painted.For example, the following code paints a blue framed rectangle:<BLOCKQUOTE><TT>public synchronized void paint (Graphicsg)<BR>g.setColor(Color.blue);<BR>g.drawRect(0,0,100,40);</TT></BLOCKQUOTE><P>The tutorial in this section gives more examples of using color.A dialog box presents a choice of colors through the use of coloredcomponents. The spreadsheet is modified to use these chosen colorswhen it is painted.<H3><A NAME="Fonts">Fonts</A></H3><P>Fonts are a critical aspect of graphics-based programming. Notonly are they important for making an application attractive,they are a seminal part of programming a visual interface. Oftenhow a component is sized or placed turns on what font is beingused. The spreadsheet tutorial in this chapter, for example, usesthe current dimensions of the font to determine how the spreadsheetcell should be sized and located. Therefore, there are two aspectsof font programming: tying fonts to display components and gettingfont information to program how graphical objects are positioned.The former aspect generally falls in the domain of the Font class;the latter is tied to the FontMetrics class, although Font isinvolved here also.<P>To construct a Font object, you need a font name, style, and size.The font name represents a family of fonts, such as Courier. Thefont families available to an applet depend on where the programis running, and so decisions about what font is to be used shouldbe made dynamically. The java.awt.Toolkit has a method called<TT>getFontList()</TT> that returnsa string array of font names on the host system. An example ofhow this method is used is found in the next section and in thefont dialog in the tutorial. The family of a Font can be retrievedthrough the <TT>getFamily()</TT> method.<P>The size of a font is its point size, such as 8 for a small fontand 72 for a large one. There are three styles defined as constantsin the Font class: <TT>PLAIN</TT>,<TT>BOLD</TT>, or <TT>ITALIC</TT>.The latter two can be combined to produce a font with both features.For example, you could create a Helvetica font that has a 36-pointsize and is both bold and italicized as follows:

⌨️ 快捷键说明

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