📄 ch4.htm
字号:
is called before a window is displayed. Windows are not made visibleuntil the <TT>show()</TT> method iscalled. They are removed from the screen, and their resourcesfreed, when the <TT>dispose()</TT>method is invoked.<P>The Frame class extends Window by adding a title bar, a borderfor resizing, support for menus, and the ability to modify thesystem cursor to various states such as waiting or moving. Formost GUI platforms, the Frame's title bar will be tied to systemcontrol boxes, such as minimize, maximize, or destroy. Consequently,the Frame class has all the elements necessary to make an appletlook like a "real" application, complete with menusand system controls.<P>Figure 4.1 and Listing 4.1 present a simple Frame applet thatchanges the cursor to a state based on the button selected. Theapplet class, FrameCursorApplet, does little more than launchthe main frame, FrameCursor. The constructor for this frame beginsby calling the frame super constructor. The sole parameter inthis case is the caption displayed on the title bar. The layoutfor the Frame is then set. The default is BorderLayout, but inthis case, you want a three by two grid matrix, hence the useof GridLayout. The buttons are added to the frame next, with namesrepresenting the cursor state to be selected. After all the componentshave been added, the <TT>pack()</TT>method is invoked so that the button placement can be optimized.Since this optimized placement will result in a small frame (sixbuttons sized tightly around the label text doesn't take muchspace), the <TT>resize()</TT> methodis called to make the frame a larger size. Finally, the frameis displayed with the <TT>show()</TT>method.<P><A HREF="f4-1.gif" >Figure 4.1 : <I>Frame applets for changing the state of the cursor.</I></A><P>When a button is selected, the custom method <TT>SetCursor()</TT>is invoked. This method takes the button label and figures outwhich cursor should be displayed. The Frame <TT>setCursor()</TT>method is used to set the cursor state; its parameter is a staticinteger defined as part of the Frame class.<BR><P><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>Note</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>You might see the message <TT><FONT SIZE=1 FACE="Courier">Untrusted Java Applet Window</TT></FONT> in your Netscape browser when applet frames are displayed. However, this is not cause for alarm-it is just a security message.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><HR><BLOCKQUOTE><B>Listing 4.1. Code for Frame applet that changes the cursorstate.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>import java.awt.*;<BR>import java.lang.*;<BR>import java.applet.*;<BR><BR>// This applet simply starts up the frame used to<BR>// show different frame cursors...<BR>public class FrameCursorApplet extends Applet {<BR> public void init() {<BR> // Create the frame with atitle...<BR> new FrameCursor("FrameCursors");<BR> }<BR>}<BR><BR>// The frame for letting the user pick different<BR>// cursors to display...<BR>class FrameCursor extends Frame {<BR> // Create the frame with a title...<BR> public FrameCursor(String title) {<BR> // Call the superclass constructor...<BR> super(title);<BR> // Create a grid layout toplace the buttons...<BR> setLayout(new GridLayout(3,2));<BR> // Add the buttons for choosingthe cursor...<BR> add(new Button("Default"));<BR> add(new Button("Wait"));<BR> add(new Button("Hand"));<BR> add(new Button("Move"));<BR> add(new Button("Text"));<BR> add(new Button("SE Resize"));<BR> // Pack and display...<BR> pack();<BR> resize(300,200); // Make ita reasonable size...<BR> show();<BR> }<BR><BR> // Handle events...<BR> public boolean handleEvent(Event e) {<BR> switch(e.id) {<BR> case e.WINDOW_DESTROY:<BR> dispose(); //Erase frame<BR> returntrue;<BR> caseEvent.ACTION_EVENT:<BR> if(e.target instanceof Button)<BR> SetCursor((Button)e.target);<BR> returntrue;<BR> default:<BR> returnfalse;<BR> }<BR> }<BR><BR> // Set the cursor based on the button chosen...<BR> void SetCursor(Button btn) {<BR> // Get the label of the button...<BR> String selection = btn.getLabel();<BR> //Set the cursor based onthat label...<BR> if (selection.equals("Wait"))<BR> setCursor(Frame.WAIT_CURSOR);<BR> else if (selection.equals("Hand"))<BR> setCursor(Frame.HAND_CURSOR);<BR> else if (selection.equals("Move"))<BR> setCursor(Frame.MOVE_CURSOR);<BR> else if (selection.equals("Text"))<BR> setCursor(Frame.TEXT_CURSOR);<BR> else if (selection.equals("SEResize"))<BR> setCursor(Frame.SE_RESIZE_CURSOR);<BR> else // Just use the default...<BR> setCursor(Frame.DEFAULT_CURSOR);<BR> }<BR>}</TT></BLOCKQUOTE><HR><P>The current state of the cursor can be retrieved with the <TT>getCursorType()</TT>method, and the <TT>getTitle()</TT>and <TT>setTitle()</TT> Frame methodscan be used for getting and setting the title bar caption, respectively.Similarly, the <TT>getIconImage()</TT>and <TT>setIconImage()</TT> methodscan be used to set the image display of an iconized frame.<H3><A NAME="Menus">Menus</A></H3><P>Frames and menus are closely related since the Frame class isthe only AWT class with built-in support for menus. Frames implementthe MenuContainer interface, which is used to contain menu components.These components are defined by the MenuComponent class, the superclassof all menu components. It defines a set of methods pertinentto individual menu items. For example, the <TT>getFont()</TT>and <TT>setFont()</TT> MenuComponentmethods are used to control the font selection of the menu object.<P>Menus can be illustrated by modifying the previous example touse menus, instead of buttons, to change the state of a cursor.Listing 4.2 shows the code that performs this. The constructorof the Frame class, called FrameMenuCursor, shows how to add menusto a frame. The first step is creating a MenuBar object. The menubar is tied to the frame with the <TT>setMenuBar()</TT>method of the Frame class. This makes the MenuBar object the defaultmenu for the frame.<P>The next step is to add the individual menus to the menubar. TheMenu class is used as a container of individual menu items orother menus. In this applet, the two menus are File and Cursor.The Menu constructor takes these strings as its sole parameter,thus defining the text to be associated with the menu. The menusare then added to the menu bar through the MenuBar <TT>add()</TT>method. The <TT>remove()</TT> methodcan be used to delete a Menu from a MenuBar.<P>The final step in menu creation is to add the individual menuitems, defined by the MenuItem class. This class is a subclassof MenuComponent and provides additional operations to be performedon a menu item. MenuItem methods can be used to set the menu labeland to disable or enable an item. The constructor for the MenuItemhas as its sole parameter the item label that will be initiallydisplayed. The <TT>add()</TT> methodis used to add an instance of MenuItem to a Menu.<HR><BLOCKQUOTE><B>Listing 4.2. Code for applet that uses menus to change cursorstate.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>import java.awt.*;<BR>import java.lang.*;<BR>import java.applet.*;<BR><BR>// This applet simply starts up the frame<BR>// which provides a menu for setting cursors...<BR>public class FrameMenuCursorApplet extends Applet {<BR> public void init() {<BR> // Create the frame with atitle...<BR> new FrameMenuCursor("MenuBased Cursors");<BR> }<BR>}<BR><BR>// The frame for letting the user pick different<BR>// cursors to display...<BR>class FrameMenuCursor extends Frame {<BR> // Create the frame with a title...<BR> public FrameMenuCursor(String title) {<BR> // Call the superclass constructor...<BR> super(title);<BR><BR> // Add the menus...<BR> // First create the menu bar<BR> MenuBar mbar = new MenuBar();<BR> setMenuBar(mbar); // Attachto the frame...<BR><BR> // Add the File submenu...<BR> Menu m = new Menu("File");<BR> mbar.add(m); //Add to menu bar<BR> // Add Quit to the submenu...<BR> m.add(new MenuItem("Quit"));<BR><BR> // Add the Cursor submenu...<BR> m = new Menu("Cursor");<BR> mbar.add(m); //Add to menu bar<BR><BR> // Add the cursor selectionsto the submenu...<BR> m.add(new MenuItem("Default"));<BR> m.add(new MenuItem("Wait"));<BR> m.add(new MenuItem("Hand"));<BR> m.add(new MenuItem("Move"));<BR> m.add(new MenuItem("Text"));<BR> m.add(new MenuItem("SEResize"));<BR><BR> // Pack and display...<BR> pack();<BR> resize(300,200); // Make ita reasonable size...<BR> show();<BR> }<BR><BR> // Handle events...<BR> public boolean handleEvent(Event e) {<BR> switch(e.id) {<BR> case e.WINDOW_DESTROY:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -