1 / 99

Advantages of GUI: User-Friendly Interaction for Modern Programs

Graphical User Interface (GUI) offers a user-friendly mechanism to interact with applications, built from GUI components. Learn about GUI advantages including ease of use, creativity in design, and modern program utilization. Discover the AWT class hierarchy encompassing various components and methods. Explore event handling using the Delegation Event Model for effective processing. Dive into GUI components like labels, buttons, and text components, and understand layout managers for optimal display organization. Gain insights into applets, their life cycle, types, and creation process.

henriettac
Download Presentation

Advantages of GUI: User-Friendly Interaction for Modern Programs

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. UNIT-5

  2. Advantages of GUI over CUI • The AWT class hierarchy- Component, Frame • Event handling: Delegation Event model • Closing a frame, mouse and keyboard events • Adapter classes • User interface components: • Labels, button, scrollbars • Text components, check box, check box groups, choices • Lists panels – ScrollPane, MenuBar, graphics • Layout manager – layout manager types • Concepts of Applets, differences between applets and Applications • Life cycle of an applet • Types of applets, creating applets, passing parameters to applets

  3. Advantages of GUI over CUI • Presents a user-friendly mechanism for interacting with an application. • Built from GUI components. • It need more resources • Speed is less compare to the CUI • In a Command Line Interface, the commands are entered from the keyboard. It is not user-friendly. • Difficult to remember commands. • It need less resources • Speed is more compare to the GUI

  4. Most modern programs use a GUI. • Graphical: Not just text or characters but windows, menus, buttons, .. • User: Person using the program • Interface: Way to interact with the program Graphical Elements include: Window List Choice Label Menu Scrollbar Button TextComponent etc.,

  5. Internet Explorer Window with GUI components button menus title bar menu bar combo box scroll bars

  6. Abstract Window Toolkit (AWT) • The AWT contains several classes and methods that allow you to create and manage windows. • The two most common windows are those derived from Panel, which is used by applets, and those derived from Frame, which creates a standard window. • The main purpose of the AWT is to support applet windows. • It can also be used to create stand-alone GUI applications.

  7. AWT class hierarchy Component Label TextArea Button TextComponent TextField Container List Choice CheckBox ScrollPane CheckBoxGroup Window Panel Scrollbar Canvas Applet Frame Dialog MenuComponent MenuBar MenuItem Menu

  8. Component: (java.awt) • Component is an abstract class that encapsulates all of the attributes of a visual component. • It is a super class of all user interface classes. • A component is something that can be displayed on a two-dimensional screen and with which the user can interact. • Attributes of a component include a size, a location, foreground and background colors, whether or not visible etc., Methods defined in class Component are: • setLocation(int, int), getLocation() ---To set and get component location • setSize(int, int), getSize() ---To set and get component size • setVisible() ---To show or hide the component • setForeground(Color), getForeground() ---To set and get foreground colors • setBackground(Color), getBackground() ---To set and get background colors

  9. Container: (java.awt) • Container class is a subclass of Component class. • This is a type of component that can nest other components within it. Ex:-Window, Frame, andpanelare examples of containers. • A Container is responsible for laying out (that is, positioning) any components that it contains. Methods defined in a class Containerare: • setLayout(LayoutManager) ---To set layout manager for display. • add( Component ) ---To add component to the display. • remove( Component ) ---To remove component from display.

  10. Panel: (java.awt) • The Panel class is a concrete subclass of Container class. • A panel is a window that does not contain a title bar, or border. • It provides space in which an application can attach any other component, including other panels. • It is the superclass for Applet. • Other components can be added to a Panel object by its add() method. • The default layout manager for a panel is the FlowLayout layout manager.

  11. Window: (java.awt) • The Window class creates a top-level window. • A top-level window is not contained within any other object; it sits directly on the desktop. • Generally, we won’t create Window objects directly. Instead, we use a subclass of Window called Frame. Frame: (java.awt) • It is a type of Window with a title bar, menu bar , borders, and resizing corners. Methods defined in a Frame class are: • setTitle(String), getTitle() ---To set or get title • setMenuBar(MenuBar) ---To set menu bar for window Layout Manager: • A manager is used to position and place components in a Container.

  12. Event Handling

  13. The Delegation Event Model • It defines standard and consistent mechanisms to generate and process events. • In this model, a source generates an event and sends it to one or more listeners. • The listener simply waits until it receives an event. Once received, the listener processes the event and then returns. • Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package. • When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument. • In the delegation event model, listeners must register with a source in order to receive an event notification.

  14. Action Events on Buttons The Delegation Event Model ActionEvent Event Object Event source Event listener Button ActionListener (Event handling methods) (actionPerformed(…))

  15. Example 1: class MyActionListener implements ActionListener { //Registration Method source.addActionListener(this); public void actionPerformed(ActionEventae){ // Handler_ code } }

  16. Example 2: class MyMouseListener implements MouseListener{ //Registration Method addMouseListener(this); public void mouseClicked(MouseEventme){ // Handler_ code } public void mouseEntered(MouseEvent me){ // Handler_ code } public void mouseExited(MouseEvent me){ // Handler_ code } public void mousePressed(MouseEvent me){ // Handler_ code } public void mouseReleased(MouseEvent me){ // Handler_ code } } }

  17. Events • Events are supported by the java.awt.event package. • An event is an object that describes a state change in a source. • Events can be generated as a consequence of a person interacting with elements in a graphical user interface. • Events may also occur that are not directly caused by interactions with a user interface. For example, an event may be generated • when a timer expires, • a counter exceeds a value, • a software or hardware failure occurs.

  18. EventSources • A sourceis an object that generates an event. This occurs when the internal state of that object changes in some way. • Sources may generate more than one type of event. • A source must register listeners in order for the listeners to receive notifications about a specific type of event. • Each type of event has its own registration method. Here is the general form: public void addTypeListener(TypeListener el) Here, Typeis the name of the event and elis a reference to the event listener. For example, the method that registers a keyboard event listener is called addKeyListener( ).

  19. Event Listeners • A listener is an object that is notified when an event occurs. • It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications. • For example, the MouseMotionListener interface defines two methods to receive notifications when the mouse is dragged or moved. • Any object may receive and process one or both of these events if it provides an implementation of this interface.

  20. Event Classes • EventObject is a superclass of all events which is defined in java.util package. • AWTEvent is a superclass of all AWT events that are handled by the delegation event model which is defined in java.awt package. The following are the main event classes in java.awt.event package. Event Class Description ActionEvent Generated when a button is pressed, a list item is double-clicked, or a menu item is selected. MouseEvent Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component. KeyEvent Generated when the key is pressed , key is released, or key is typed.

  21. Event Class Description TextEvent Generated when the value of a text area or text field is changed. MouseWheelEvent Generated when the mouse wheel is moved. WindowEvent Generated when a window is activated, deactivated, deiconified, iconified, opened, closing, closed. ItemEvent Generated when a check box or list item is clicked; also occurs when a choice selection is made or a checkable menu item is selected or deselected. FocusEvent Generated when a component gains or loses keyboard focus. AdjustmentEvent Generated when a scroll bar is manipulated. ContainerEvent Generated when a component is added to or removed from a container.

  22. The ActionEvent Class • You can obtain the command name for the invoking ActionEvent object by using the String getActionCommand() method. • The getWhen( ) returns the time at which the event took place. The KeyEvent Class • int getKeyChar( ) which returns the character that was entered, and int getKeyCode( ) which returns the key code. • It defines many integer constants: VK_ENTER VK_ESCAPE VK_CANCEL VK_UP VK_DOWN VK_LEFT VK_RIGHT VK_PAGE_DOWN VK_PAGE_UP VK_SHIFT VK_ALT VK_CONTROL The MouseEvent Class • int getX( ) int getY( ) • The int getClickCount( ) method obtains the number of mouse clicks for this event. The ItemEvent Class • The intgetStateChange() method returns the state change (i.e., SELECTED or DESELECTED) for the event. • The ObjectgetItem( ) method can be used to obtain a reference to the item that generated an event.

  23. Sources of Events • The following table lists some of the user interface components that can generate the events. Event Source Description Button Generates action events when the button is pressed. Choice Generates item events when the choice is changed. List Generates action events when an item is double- clicked; generates item events when an item is selected or deselected. Window Generates window events when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.

  24. Event Source Description Menu Item Generates action events when a menu item is selected; generates item events when a checkable menu item is selected or deselected. Scrollbar Generates adjustment events when the scroll bar is manipulated. Text components Generates text events when the user enters a character. Checkbox Generates item events when the check box is selected or deselected.

  25. Listener Interfaces Interface Description ActionListener Defines one method to receive action events. AdjustmentListener Defines one method to receive adjustment events. ComponentListener Defines four methods to recognize when a component is hidden, moved, resized, or shown. ContainerListener Defines two methods to recognize when a component is added to or removed from a container. FocusListener Defines two methods to recognize when a component gains or loses keyboard focus. ItemListener Defines one method to recognize when the state of an item changes. KeyListener Defines three methods to recognize when a key is pressed, released, or typed.

  26. Contd… Interface Description MouseListener Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is released. MouseMotionListener Defines two methods to recognize when the mouse is dragged or moved. MouseWheelListener Defines one method to recognize when the mouse wheel is moved. TextListener Defines one method to recognize when a text value changes. WindowFocusListener Defines two methods to recognize when a window gains or loses input focus. WindowListener Defines seven methods to recognize when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.

  27. Listener Interfaces and associated Event Classes ActionEvent ActionListener MouseEvent MouseListener MouseMotionListener KeyEvent KeyListener TextEvent TextListener AdjustmentEvent AdjustmentListener ContainerEvent ContainerListener FocusEvent FocusListener ItemEvent ItemListener TextEvent TextListener WindowEvent WindowListener

  28. The ActionListener Interface This interface defines the actionPerformed() method that is invoked when an action event occurs. Its general form is shown here: void actionPerformed(ActionEvent ae) The AdjustmentListener Interface void adjustmentValueChanged(AdjustmentEvent ae) The ComponentListener Interface This interface defines four methods that are invoked when a component is resized, moved, shown, or hidden. void componentResized(ComponentEvent ce) void componentMoved(ComponentEvent ce) void componentShown(ComponentEvent ce) void componentHidden(ComponentEvent ce) The ItemListener Interface void itemStateChanged(ItemEvent ie)

  29. The ContainerListener Interface When a component is added or removed to and from a container the following methods are invoked. void componentAdded(ContainerEvent ce) void componentRemoved(ContainerEvent ce) The KeyListener Interface void keyPressed(KeyEvent ke) void keyReleased(KeyEvent ke) void keyTyped(KeyEvent ke) The MouseListener Interface void mouseClicked(MouseEvent me) void mouseEntered(MouseEvent me) void mouseExited(MouseEvent me) void mousePressed(MouseEvent me) void mouseReleased(MouseEvent me)

  30. The MouseMotionListener Interface void mouseDragged(MouseEvent me) void mouseMoved(MouseEvent me) The WindowListener Interface void windowActivated(WindowEvent we) void windowClosed(WindowEvent we) void windowClosing(WindowEvent we) void windowDeactivated(WindowEvent we) void windowDeiconified(WindowEvent we) void windowIconified(WindowEvent we) void windowOpened(WindowEvent we) The MouseWheelListener Interface void mouseWheelMoved(MouseWheelEvent mwe) The TextListener Interface void textChanged(TextEvent te)

  31. Adapter Classes • Java provides a special feature, called an adapter class, that can simplify the creation of event handlers in certain situations. • An adapter class provides an empty implementation of all methods in an event listener interface. • Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. • You can define a new class to act as an event listener by extending one of the adapter classes and implementing only those events in which you are interested.

  32. Table 20-4 lists the commonly used adapter classes in java.awt.event and corresponding interfaces. Adapter Class Listener Interface KeyAdapter KeyListener MouseAdapter MouseListener MouseMotionAdapter MouseMotionListener WindowAdapter WindowListener ComponentAdapter ComponentListener ContainerAdapter ContainerListener FocusAdapter FocusListener

  33. Inner Class • An inner class is a class defined within other class, or within an expression. //Inner class demo. import java.applet.*; import java.awt.event.*; /*<applet code="InnerClassDemo" width=200 height=100></applet>*/ public class InnerClassDemo extends Applet{ public void init() { addMouseListener(new MyMouseAdapter()); } class MyMouseAdapter extends MouseAdapter { public void mousePressed(MouseEvent me) { showStatus("Mouse Pressed"); } } }

  34. Anonymous Inner Class • An anonymous inner class is one that is not assigned a name. //Anonymous inner class demo. import java.applet.*; import java.awt.event.*; /*<applet code="AnonymousInnerClassDemo" width=200 height=100></applet>*/ public class AnonymousInnerClassDemo extends Applet{ public void init() { addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { showStatus("Mouse Pressed"); } }); } }

  35. Frame • Frame is a window that is not contained inside another window. • Frame is the basis to contain other user interface components in Java graphical applications. • The Frame class can be used to create windows. Frame’s constructors: Frame( ) Frame(String title) • After a frame window has been created, it will not be visible until you call setVisible( true).

  36. Frame Location • By default, a frame is displayed in the upper- left corner of the screen. • To display a frame at a specified location, use the setLocation(x,y) method in the Frame class.

  37. //Method 1 import java.awt.*; public class MyFrame{ public static void main( String args[]){ Frame f = new Frame("MyFrame"); f.setSize(300,200); f.setVisible(true); } }

  38. //Method 2 import java.awt.*; public class MyFrame extends Frame{ MyFrame(){ super(“title of Frame”); setSize(300,200); setVisible(true); } } class ExFrame{ public static void main( String args[]){ new MyFrame(); } }

  39. //Method 3 import java.awt.*; public class MyFrame extends Frame{ MyFrame(){ super(“title of Frame”); setSize(300,200); setVisible(true); } public static void main( String args[]){ new MyFrame(); } }

  40. User interface components Label Button Scrollbar TextField TextArea List Checkbox CheckboxGroup Choice MenuBar Menu MenuItem …

  41. Label • Labels are passive controls that do not support any interaction with the user. • Constructors • Label() • Label( String text ) • Label( String text , int alignment ) • Alignment Constants • Label.LEFT , Label.RIGHT , Label.CENTER • Methods • void setText(String text) • String getText() • void setAlignment(int alignment) • int getAlignment()

  42. //Label Example import java.awt.*; public class ExLabel extends Frame { public ExLabel() { super("Label test"); Label label1 = new Label(“Enter User Name"); add( label1 ); setSize(300,200); setVisible(true); } public static void main( String args[] ) { new ExLabel(); } }

  43. Button • Constructors • Button() • Button(String title ) • Methods • getLabel() • setLabel()

  44. import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="ButtonDemo" width=250 height=150> </applet>*/ public class ButtonDemo extends Applet implements ActionListener { String msg = "“; Button yes, no, maybe; public void init() { yes = new Button("Yes"); no = new Button("No"); maybe = new Button("Undecided"); add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand(); if(str.equals("Yes")) { msg = "You pressed Yes."; } else if(str.equals("No")) { msg = "You pressed No."; } else { msg = "You pressed Undecided."; } repaint(); } public void paint(Graphics g) { g.drawString(msg, 6, 100); } } }

  45. Checkbox • Constructors • Checkbox() • Checkbox(String text) • Checkbox(String text , boolean state) • Checkbox(String text , CheckboxGroup group , boolean state) • Methods • String getLabel() • void setLabel() • boolean getState() • void setState()

  46. CheckboxGroup, Choice and List Three types of interface components are used to allow the user to select one item from a large number of possibilities. • First is a group of connected check boxes with the property that only one can be selected at a time.( also called radio buttons ). • Second is Choice. • A third is a List. A List is similar to a choice, but several items will be displayed at a time.

  47. CheckboxGroup / Radio buttons Constructors • CheckboxGroup() • Checkbox(String text , CheckboxGroup group , boolean state) Methods • Checkbox getSelectedCheckbox( ) • void setSelectedCheckbox(Checkbox which)

  48. Choice • Constructors • Choice() • Methods • void add(String) – To add items to the list • String getItem(int) • String getSelectedItem() • void select(int index) • void select(String name)

  49. List • Constructors • List() • List(int rows) • List(int rows, boolean multipleMode) • Methods • void add(String) //To add item to the end of the list • void add(String, int index) //To add item at an specified index • int getItemCount() //To get the count of items in the list • String getItem(int) //To get the specified item from the list • void remove(int) //To remove the item form the list • String getSelectedItem() //To get selected item from the list

  50. TextField Constructors • TextField() • void setText(String) • TextField(int columns) • TextField(String text) • TextField(String text, int columns) Methods • void setText(String) • String getText() • void setEchoChar(char) //To enter text that is not displayed • void setEditable(bolean) //if false, text cann’t be modified

More Related