1. Trang chủ
  2. » Công Nghệ Thông Tin

Java By Example PHẦN 7 ppt

59 195 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 59
Dung lượng 1,9 MB

Nội dung

public class FrameApplet2 extends Applet { CustomFrame frame; Button button; public void init() { frame = new CustomFrame("Custom Frame Window"); button = new Button("Show Window"); add(button); } public boolean action(Event evt, Object arg) { boolean visible = frame.isShowing(); if (visible) { frame.hide(); button.setLabel("Show Window"); } else { frame.show(); button.setLabel("Hide Window"); } return true; } } class CustomFrame extends Frame { CustomFrame(String title) { super(title); } public void paint(Graphics g) { resize(200, 100); g.drawString("This is a custom window.", 30, 30); } } Tell Java that the applet uses the classes in the awt package. Tell Java that the applet uses the classes in the applet package. Derive the FrameApplet2 class from Java's Applet class. Declare the custom frame-window and button objects. Override the init( ) method. Create the custom frame window. Create the button component. Add the button to the applet. Override the action( ) method. Determine whether the window is visible. If the window is visible Hide the window. Change the button's label to "Show Window." Else if the window is hidden Show the window. Change the button's label to "Hide Window." Tell Java that the message was handled okay. Derive the CustomFrame class from Java's Frame class. Define the class's constructor. Pass the title string on to the Frame class. Override the window's paint( ) method. Resize the window. Display a message in the window. NOTE When you compile FrameApplet2, notice that, although both the FrameApplet2 and CustomFrame classes are defined in the same file, the Java compiler creates two class files called FrameApplet2.class and CustomFrame.class. Example: Adding Components to a Window Frame windows are just like any other window you see when you create an applet. That is, you can add components organized into a variety of layouts and respond to the user's selections of these components. In fact, adding layouts and components to a frame window is not unlike doing the same thing with your applet's main window, which you did in the previous chapter. First you create and set the layout manager, and then you add the components as appropriate for the layout manager you've chosen. Listing 23.3 is an applet called FrameApplet3 that not only creates a custom frame window, but also creates a simple layout for the window. This layout contains only a single button; however, you can create as sophisticated a layout as you like. Feel free to experiment further with this applet. Figure 23.3 shows FrameApplet3 running under Appletviewer, after the user has displayed the frame window. As you can see in the figure, the window has a single button labeled "Close Window." When you click this button, the frame window's action( ) method responds by calling the dispose( ) method, which not only removes the window from the screen, but also destroys the window in memory. Figure 23.3 : This is FrameApplet3 running under Appletviewer. Listing 23.3 FrameApplet3.java: Adding Components to a Window. import java.awt.*; import java.applet.*; public class FrameApplet3 extends Applet { CustomFrame frame; Button button; public void init() { frame = new CustomFrame("Custom Frame Window"); button = new Button("Show Window"); add(button); } public boolean action(Event evt, Object arg) { boolean visible = frame.isShowing(); if (visible) { frame.hide(); button.setLabel("Show Window"); } else { frame.show(); button.setLabel("Hide Window"); } return true; } } class CustomFrame extends Frame { Button button; CustomFrame(String title) { super(title); FlowLayout layout = new FlowLayout(); setLayout(layout); button = new Button("Close Window"); add(button); } public void paint(Graphics g) { resize(200, 100); g.drawString("This is a custom window.", 30, 50); } public boolean action(Event evt, Object arg) { if (arg == "Close Window") dispose(); return true; } } Table 23.1 shows some useful methods you can use to manipulate a frame window. Some of these methods are defined in the Frame class, whereas others are inherited from the class's superclasses, such as Window and Container. Table 23.1 Useful Frame-Window Methods. Methods Description void add( ) Adds components to the window. void dispose( ) Deletes the window from memory. int getCursorType( ) Returns the window's cursor type. Image getIconImage( ) Returns the window's icon object. LayoutManager getLayout( ) Returns the window's layout manager. MenuBar getMenuBar( ) Returns the window's menu bar object. String getTitle( ) Returns the window's title. void hide( ) Removes the window from the screen. Boolean isResizable( ) Returns true if the window is resizable. void remove( ) Removes components from the window. void removeAll( ) Removes all components from the window. void setCursor(int cursorType) Sets the window's cursor type. void setIconImage(Image image) Sets the window's icon object. void setLayout( ) Sets the window's layout manager. void setMenuBar(MenuBar mb) Sets the window's menu bar. void setResizable(boolean Sets the window's resizable resizable)attribute. void setTitle(String title) Sets the window's title. void show( ) Displays the window on the screen. Using Menu Bars Most Windows applications have menu bars, which enable the user to more easily locate and select the various commands and options supported by the program. The frame windows you create from within your applets can also have menu bars. To create a menu bar in a window, you must follow a series of steps: 1. Create an object of the MenuBar class. 2. Call the window's setMenuBar( ) method to give the menu bar to the window. 3. Create objects of the Menu class for each menu you want in the menu bar. 4. Call the MenuBar object's add( ) method to add each menu object to the menu bar. 5. Create objects of the MenuItem or CheckboxMenuItem classes for each item you want to appear in the menus. 6. Call the menus' add( ) methods in order to add each item to its appropriate menu. Each of the above steps is covered in the sections that follow. Creating and Setting a MenuBar Object The first step in adding a menu bar to a frame window is to create the MenuBar object that'll hold all the menus and commands. The menu bar in a window is the horizontal area near the top that contains the names of each of the menus in the menu bar. To create the MenuBar object, call the MenuBar class's constructor, like this: MenuBar menuBar = new MenuBar( ); As you can see, the MenuBar( ) constructor requires no arguments. After you've created the MenuBar object, you have to tell Java to associate the menu bar with the frame window. You do this by calling the window's setMenuBar( ) method: setMenuBar(menuBar); At this point, you have an empty menu bar associated with the window. In the next steps, you add menus to the menu bar. Adding Menus to a Menu Bar A menu bar is the horizontal area near the top of a window that contains the names of the menus contained in the menu bar. After creating and setting the MenuBar object, you have the menu bar, but it contains no menus. To add these menus, you first create objects of the Menu class for each menu you want in the menu bar, like this: Menu fileMenu = new Menu("File"); Menu editMenu = new Menu("Edit"); Menu optionMenu = new Menu("Options"); The Menu class's constructor takes a single argument, which is the string that'll appear as the menu's name on the menu bar. The example lines above create three menus for the menu bar. After creating the Menu objects, you have to add them to the menu bar, which you do by calling the MenuBar object's add( ) method, like this: menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(optionMenu); After Java executes the above three lines, you have a menu bar with three menus, as shown in Figure 23.4. Note, however, that at this point the menus contain no commands. If you were to click on the menu names, no pop-up menus would appear. Figure 23.4 : This window's menu bar contains three empty menus. Adding Menu Items to Menus You may have empty menus at this point, but you're about to remedy that problem. To add items to your menus, you first create objects of the MenuItem or CheckboxMenuItem classes for each menu item you need. To add items to the Options menus you made previously, you might use Java code something like this: MenuItem option1 = new MenuItem("Option 1"); MenuItem option2 = new MenuItem("Option 2"); MenuItem option3 = new MenuItem("Option 3"); The MenuItem constructor takes as its single argument the string that'll be displayed in the menu for this item. If you're thinking that, after you create the menu items, you must call the appropriate Menu object's add( ) method, you're be exactly right. Those lines might look like this: optionMenu.add(option1); optionMenu.add(option2); optionMenu.add(option3); Now, when you display the frame window sporting the menu bar you've just created, you'll see that the Options menu contains a number of selections from which the user can choose, as shown in Figure 23.5. Figure 23.5 : Now the Options menu contains menu items. TIP Sometimes, you may have several groups of related commands that you'd like to place under a single menu. You can separate these command groups by using menu separators, which appear as horizontal lines in a pop-up menu. To create a menu separator, just create a regular MenuItem object with a string of "-". That is, the string should contain a single hyphen. Example: Using a Menu Bar in a Frame Window Now that you have this menu bar business mastered, it's time to put what you've learned to work. Listing 23.4 is an applet called MenuBarApplet. This applet displays a single button, which, when selected, displays a frame window with a menu bar. This menu bar contains a single menu with three items. The first two items are regular MenuItem objects. The third item is CheckboxMenuItem, which is a menu item that can display a check mark. Figure 23.6 shows MenuBarApplet with its frame window displayed and the Test menu visible. (Notice the menu separator above the checked item.) Figure 23.6 : This is MenuBarApplet's frame window and menu bar. [...]... mouse event in Java programs (and any other program written for a graphical user interface) is the MOUSE_DOWN event, which is generated whenever the user clicks within an applet It's the MOUSE_DOWN event, for example, that lets Java know when an on-screen button component has been clicked You don't have to worry about clicks on on-screen buttons (usually), because they're handled by Java However, you... q q q q q q The Event Object The Mouse r Handling Mouse Clicks r Example: Using Mouse Clicks in an Applet r Handling Mouse Movement r Example: Responding to Mouse Movement in an Applet The Keyboard r Responding to Key Presses r Predefined Key Constants r Key Modifiers r Example: Using Key Presses in an Applet Handling Events Directly r Example: Overriding handleEvent() in an Applet Summary Review Questions... In this chapter, you learn the secrets of mouse and keyboard handling in Java applets The Event Object In order to understand how to respond to various types of events, you need to know more about Java' s Event class, an object of which is passed to any event-handling method When you want to respond to a Java button control, for example, you override the action() method, whose first argument is an Event... event, which is handled by the mouseDown() method, is caused when the user presses the mouse button MOUSE_UP-This event, which is handled by the mouseUp() method, is caused when the user releases the left mouse button MOUSE_MOVE-This event, which is handled by the mouseMove() method, occurs when the user moves the mouse pointer on the screen MOUSE_DRAG-This event, which is handled by the mouseDrag() method,... pointer while holding down the left mouse button MOUSE_ENTER-This event, which is handled by the mouseEnter() method, is sent when the mouse pointer enters the area owned by an applet or component MOUSE_EXIT-This event, which is handled by the mouseExit() method, occurs when the mouse pointer leaves the area owned by an applet or a component In the sections that follow, you'll learn more about the most... dialog's test field Tell Java to repaint the frame window Tell Java that the event was handled Define the ShowDialogBox() method Create the new dialog box and set its layout Create and add components to the dialog box Display and resize the dialog box NOTE In addition to normal dialog boxes, Java supports file dialog boxes for loading and saving files The file dialogs are represented by the FileDialog class... drawing program, it gives you some idea of how you might use a mouse to accomplish other similar tasks Figure 25.2 : This applet draws by tracking the movement of the mouse Listing 25.2 MouseApplet2 .java: An Applet That Tracks Mouse Movement import java. awt.*; import java. applet.*; public class MouseApplet2 extends Applet { Point startPoint; Point points[]; int numPoints; boolean drawing; public void...Listing 23.4 MenuBarApplet .java: An Applet That Uses a Menu Bar import java. awt.*; import java. applet.*; public class MenuBarApplet extends Applet { MenuBarFrame frame; Button button; public void init() { frame = new MenuBarFrame("MenuBar Window"); button = new... Command 2"; else if (arg == "Check") str = "You selected the Check item"; repaint(); return true; } else return false; } } Tell Java that the applet uses the classes in the awt package Tell Java that the applet uses the classes in the applet package Derive the MenuBarApplet class from Java' s Applet class Declare the custom frame-window and button objects Override the init( ) method Create the custom frame... Overriding handleEvent() in an Applet Summary Review Questions Review Exercises Up until now, your applets have responded to events generated by Java components like buttons, text fields, and list boxes You've yet to examine how to respond to events generated by the most basic of a computer's controls, the mouse and the keyboard Because virtually every computer has these important hardware controls, . 30, 30); } } Tell Java that the applet uses the classes in the awt package. Tell Java that the applet uses the classes in the applet package. Derive the FrameApplet2 class from Java& apos;s Applet. FrameApplet3 running under Appletviewer. Listing 23.3 FrameApplet3 .java: Adding Components to a Window. import java. awt.*; import java. applet.*; public class FrameApplet3 extends Applet { CustomFrame. MenuBarApplet's frame window and menu bar. Listing 23.4 MenuBarApplet .java: An Applet That Uses a Menu Bar. import java. awt.*; import java. applet.*; public class MenuBarApplet extends Applet { MenuBarFrame

Ngày đăng: 12/08/2014, 19:21