1 / 20

GUI - V

GUI - V. Overview Using No Layout Manager Message Dialog Boxes Running a Program as an Applet and an Application More on Handling Mouse Events More on Handling Keyboard Events Example Keyboard Events Demo. Using No Layout Manager.

ingo
Télécharger la présentation

GUI - V

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GUI - V Overview • Using No Layout Manager • Message Dialog Boxes • Running a Program as an Applet and an Application • More on Handling Mouse Events • More on Handling Keyboard Events • Example Keyboard Events Demo GUI 5Lecture 7

  2. Using No Layout Manager Java enables you to place components in a container without using a layout manager. In this case, the component must be palced using the component’s instance method setBounds(), as follows: public void setBounds(int x, int y, int width, int height); This sets the location and size for the components as in the following example: JButton jbt =new JButton(“Help”); jbt.setBounds(10,10,40,20,);The upper-left corner of the Help button, is placed at(10,10); the button width is 40, and the height is 20 GUI 5Lecture 7

  3. Using No Layout Manager You perform the following steps in order not to use a layout manager: 1-Use the following statements to specify no layout manager: setLayout(null); 2- Add the component to the container: add(component); 3-Specify the location where the component is to be placed, using the setBounds() method as follows: JButton jbt =new JButton(“Help”); jbt.setBounds(10,10,40,20) GUI 5Lecture 7

  4. Using No Layout Manager(Example) GUI 5Lecture 7

  5. Using No Layout Manager • If you run the previous example on Windows with 640 x 480 resolution, the layout size is just right. When the program is run on Windows with a higher resolution, the components appear very small and clump together. When it is run on Windows with a lower resolution, the components cannot be shown in their entirety. • If you resize the window, you will see that the location and size of the components are not changes • With no layout, the components size and positions are fixed, and can only be changed in the frame with a layout manager. • * If you use this approach, your program may look fine on one machine and be useless on others. For this reason, it is advisable to use the layout managers to develop a platform-independent graphical user interface. GUI 5Lecture 7

  6. Message Dialog Boxes A dialog box is normally used as a temporary window to receive additonal information form the user or to provide notification that some event has occurred. You can build a variety of dialog boxes in Java. To display a message dialog box, use the static showMessageDialog method in the JOptionPane class public static void showMessageDialog(Component parentComponent,Object message, String title, int messageType) The parentComponent is the parent component of the dialog box, form which the dialog box is launched. The message is the object to display. Often you use a string for message. The title is the title of the dialog box. The messageType determines the type of message to be displayed. GUI 5Lecture 7

  7. Message Dialog Boxes There are five message type : 1- ERROR_MESSAGE 2-INFORMATION_MESSAGE 3-WARNING_MESSAGE 4-QUESTION_MESSAGE 5-PLAIN_MESSAGE Each type, except for the PLAIN_MESSAGE type, has an associated icon. You can use the following method to supply your own icons: public static void showMessageDialog(Componet parentComponent, Object message, String title, int messageType, Icon icon) GUI 5Lecture 7

  8. Example of Dialog Box GUI 5Lecture 7

  9. Example of Dialog Box GUI 5Lecture 7

  10. Example of Dialog Box The message dialog box is modal, which means that no other window can be accessed before the message dialog is dismissed GUI 5Lecture 7

  11. Running a Program as an Applet and an Application You can implement a main method in an applet that will run as an application or as an applet using the same program. Suppose you have an applet named TestApplet. To enable it to run as an application, all you need to do is add a main method in the applet with the implementation, as follows: public static void main(String[] args) {//create a frame JFrame frame=new JFrame(“Running a program”); // create an instance of TestApplet TestApplet applet =new TestApplet(); //Add the applet instance to the frame Frame.getContentPane().add(applet, BorderLayout.CENTER); //Invoke init and start Applet.init(); Applet.start(); // display the frame Frame.setSize(300,300); Frame.setVisible(true);} GUI 5Lecture 7

  12. More on Handling Mouse Events • Java provides two listener interfaces, MouseListenerandMouseMotionListener, to handle mouse events. • The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked. • The MouseMotionListener listens foractions such as dragging or moving themouse. The following example can run as an applet and as an application. GUI 5Lecture 7

  13. Example Moving message using Mouse GUI 5Lecture 7

  14. MoveMessageDemo GUI 5Lecture 7

  15. More Handling Keyboard Events • Handling Keyboard Events To process a keyboard event, use the following handlers in the KeyListenerinterface: • keyPressed(KeyEvent e) Called when a key is pressed. • keyReleased(KeyEvent e) Called when a key is released. • keyTyped(KeyEvent e) Called when a key is pressed and thenreleased GUI 5Lecture 7

  16. The KeyEvent Class • The keys captured in the event are integers rpresenting Unicode character values, Every keyboard event has an associated key character or key code that is returned by the getKeyChar() method getKeyCode() method Key Constants: Home VK_HOME End VK_End Page Up VK_PGUP Page Down VK_PGDN The enter key VK_ENTER The right-arrow key VK_RIGHT etc... GUI 5Lecture 7

  17. Example Keyboard Events Demo • Objective: Display a user-input character. The user can also move the character up, down, left, and right using the arrow keys. GUI 5Lecture 7

  18. Example Keyboard Event (Cont) GUI 5Lecture 7

  19. Example Keyboard Event (Cont) GUI 5Lecture 7

  20. Example Keyboard Event (Cont GUI 5Lecture 7

More Related