1 / 64

Graphical User Interfaces

By: Abu Bakar. Graphical User Interfaces. GUI. What is GUI Support for GUI in java Java.awt package Javax.swing package GUI classes vs. Non-GUI Support Classes JButton, JFrame & JRadioButton etc Layout managers & Event handling classes etc. java.awt package. Abstract Windowing Toolkit

shelby
Download Presentation

Graphical User Interfaces

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. By: Abu Bakar Graphical User Interfaces

  2. GUI • What is GUI • Support for GUI in java • Java.awt package • Javax.swing package • GUI classes vs. Non-GUI Support Classes • JButton, JFrame & JRadioButton etc • Layout managers & Event handling classes etc

  3. java.awt package • Abstract Windowing Toolkit • tied directly to local platform’s GUI capabilities • often called Heavy Weight Components

  4. javax.swing package • Swing components are written, manipulated and displayed completely in java • allow the programmer to specify a uniform look and feel across all platforms • often referred to as Light Weight Components

  5. Class Hierarchy

  6. GUI Creation Steps • import required packages • import java.awt.* and/or javax.swing.* package • Setup the top level containers • allows other components to be nested inside it • Methods: add and setLayout • classified into two broad categories • Top Level containers  JFrame, Dialog and Applet etc • General Purpose Containers  JPanel, Toolbar and ScrollPane etc

  7. GUI Creation Steps –Cont.. • JFrameframe = new JFrame(); • Get the component area of the top level container • JFrameis a frame is a window. So, it can be interpreted as JFrameis a window • Every window has two areas. System Area & Component Area • The programmer cannot add/remove components to the System Area • Conntainercon = frame.getContentPane();

  8. GUI Creation Steps - Cont.. • Apply layout to component area • The layout of components in a container is usually governed by Layout Managers. • con.setLayout( new FlowLayout( ) ); • We can also write: FlowLayout layout = new FlowLayout(); con.setLayout(layout);

  9. GUI Creation Steps - Cont.. • Create and Add components • JTextField tf = new JTextField(10); • JButton button = new JButton (“My Button”); • c.add(tf); • con.add(button);

  10. GUI Creation Steps - Cont.. • Set size of frame and make it visible • frame.setSize(200, 300) ; • frame.setVisible(true) ; • frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  11. Code it & Run

  12. Layout Managers 1. Flow Layout 2. Grid Layout 3. Border Layout 4. Box Layout 5. Card Layout 6. GridBag Layout and so on

  13. Flow Layout • Position components on line by line basis. Each time a line is filled, a new line is started. • c.setLayout( new FlowLayout( ) );

  14. Grid Layout • Splits the panel/window into a grid (cells) with given number of rows and columns • c.setLayout( new GridLayout( 3 , 2 ) );

  15. Border Layout • Divides the area into five regions. North, South, East, West and Center • c.setLayout( new BorderLayout( ); • c.add( b1 , BorderLayout.NORTH ); • c.add( b2 , BorderLayout.SOUTH );

  16. GUI Creation Approaches

  17. More Complex GUI • Try this one also:

  18. More Swing Components

  19. To do • Try different GUI components and see how they look • But, do not forget to follow those steps, taught at the start of the lecture • And also do not forget the basics of java, you have learnt so far.

  20. Questions

  21. Events • GUIs generate events when the user interacts with GUI. For example, • Clicking a button • Moving the mouse • Closing Window etc

  22. Events – cont.. • In java, events are represented by Objects • These objects tells us about event and its source. Examples are • ActionEvent (Clicking a button) • WindowEvent (Doing something with window e.g. closing , minimizing) • Both AWT and swing components (not all) generate events • java.awt.event.*; • javax.swing.event.*;

  23. Event Handling Model • Common for both AWT and Swing components • Event Delegation Model • Processing of an event is delegated to a particular object (handlers ) in the program • Publish-Subscribe • Separate UI code from program logic

  24. Event Handling Steps • For a programmer the event Handling is a three step process in terms of code • Step 1 • Create components which can generate events • Step 2 • Build component (objects) that can handle events (Event Handlers) • Step 3 • Register handlers with generators

  25. Event Handling Process [1]Event Generators • You have already seen alot of event generators • Buttons • Mouse • Key • Window etc • JButton b1 = new JButton(“Hello”); • Now b1 can generate events

  26. Event Handling Process [2]Event Handlers/ Event Listener • First Technique - By Implementing Listener Interfaces • Java defines interfaces for every event type • If a class needs to handle an event. It needs to implement the corresponding listener interface • To handle “ActionEvent” a class needs to implement “ActionListener” • To handle “KeyEvent” a class needs to implement “KeyListener” • To handle “MouseEvent” a class needs to implement “MouseListener”

  27. Example Listeners public interface ActionListener { public void actionPerformed(ActionEvent e); } public interface ItemListener { public void itemStateChanged(ItemEvent e); } public interface ComponentListener { public void componentHidden(ComponentEvent e); public void componentMoved(ComponentEvent e); public void componentResized(ComponentEvent e); public void componentShown(ComponentEvent e); }

  28. Event Handling Process [2]Event Handlers • By implementing an interface the class agrees to implement all the methods that are present in that interface. • Inside the method the class can do what ever it wants to do with that event • Event Generator and Event Handler can be the same or different classes

  29. Event Handling Process [2]Event Handlers • To handle events generated by Button. A class needs to implement ActionListener interface. public class Test implements ActionListener{ public void actionPerformed(ActionEvent ae){ // do something } }

  30. Event Handling Process [3]Registering Handler with Generator • The event generator is told about the object which can handle its events • Event Generators have a method add______Listener(________) • b1.addActionListener(objectOfTestClass)

  31. Let’s code it • Event Handling, a Simple Example

  32. Behind the Scenes • What’s happening Behind the Scenes • ??

  33. Event Handling Participants • Event Generator / Source • Swing and awt components • For example, JButton, JTextField, JFrame etc • Generates an event object • Registers listeners with itself

  34. Event Handling Participants -- cont • Event Object • Encapsulate information about event that occurred and the source of that event • For example, if you click a button, ActionEvent object is created

  35. Event Handling Participants -- cont • Event Listener/handler • Receives event objects when notified, then responds • Each event source can have multiple listeners registered on it • Conversely, a single listener can register with multiple event sources

  36. Event Handling Participants -- cont • JVM • Receives an event whenever one is generated • Looks for the listener/handler of that event • If exist, delegate it for processing • If not, discard it (event)

  37. Event Handling Diagram

  38. Try to code • Making Small Calculator

  39. Questions

  40. Handling Mouse Events • Mouse events can be trapped for any GUI component that inherit from Component class. For example, JPanel, JFrame & JButton etc. • To handle Mouse events, two types of listener interfaces are available • MouseMotionListener • MouseListener

  41. Handling Mouse Events – cont.. • MouseMotionListener • For processing mouse motion events • Mouse motion event is generated when mouse is moved or dragged public interface MouseMotionListener { public void mouseDragged (MouseEvent me); public void mouseMoved (MouseEvent me); }

  42. Handling Mouse Events – cont.. • MouseListener • For processing “interesting” mouse events • Mouse event is generated when mouse is • Pressed • Released • clicked (pressed & released without moving the cursor) • Enter (mouse cursor enters the bounds of component) • Exit (mouse cursor leaves the bounds of component)

  43. Handling Mouse Events – cont.. public interface MouseListener { public void mousePressed (MouseEvent me); public void mouseClicked (MouseEvent me); public void mouseReleased(MouseEventme); public void mouseEntered (MouseEvent me); public void mouseExited (MouseEvent me); }

  44. Steps forHandling Mouse Motion Events • Create mouse motion events generating components • JFrame myFrame = new JFrame(); • Create Event Handler • Implement MouseMotionListener Interface • Provide implementaion for the required method public void mouseMoved (MouseEvent e) { // write your code here } • What about remaining method?

  45. Steps forHandling Mouse Motion Events • Register event generator with event handler myFrame.addMouseMotionListener (this);

  46. Let’s code it • Handling mouse motion events, a Simple Example

  47. Questions

  48. Handling Window Events • To handle window events, need to implement interface “WindowListner”. • Interface “WindowListner” contains 7 methods • We require only one i.e. windowClosing • So have to provide definitions of all methods to make concrete class

  49. Interface WindowListener public interface WindowListener { public void windowActivated (WindowEvent we); public void windowClosed (WindowEvent we); public void windowClosing (WindowEvent we); public void windowDeactivated (WindowEvent we); public void windowDeiconified (WindowEvent we); public void windowIconified (WindowEvent we); public void windowOpened (WindowEvent we); }

  50. Let’s code it • Window Exit Handler, a Simple Example

More Related