Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.

Similar presentations


Presentation on theme: "Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar."— Presentation transcript:

1 Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar

2 Graphical User Interface A graphical user interface presents a user friendly mechanism for interacting with a program. A GUI pronounced as (GOO-EE) gives a program a distinctive “look” and “feel”.

3 Introductory Concepts Most swing components are written, manipulated and displayed completely in java. (so called pure java components). Swing components are part of java foundation classes (JFC)-java libraries for cross platform GUI development. Concept of AWT (Abstract Window Toolkit) and the concept of “look and feel” Concept of heavy weight and light weight components. Each heavy weight component has a peer that is responsible for the interations b/w the component and the local platform. Some swing components are heavyweight components, Such as subclass of (Java.awt.Window) e.g JFrame.

4 Hierarchy of GUI Classes Object Component Container JComponent Class component declares the common attributes and behaviors of derived classes. e.g. paint method A container manages a collection of related components JComponent is the super class Of most swing components. Declares The common attributes and behaviors of all subclasses

5 JComponent class This class declares the common attributes and behaviors of all subclasses of JCompoent. –A pluggable look and feel –Mnemonics –Common event handling capabilities –Tooltips –Support for assistive technologies –Support for user interface localization

6 A simple GUI program for JLabel import java.awt.*; import java.awt.Event; import javax.swing.*; public class LabelTesting extends JFrame{ private JLabel label1,label2,label3; public LabelTesting() { super("Firs GUI Program"); Container container = getContentPane(); container.setLayout(new FlowLayout()); label1 = new JLabel("Text on First Label"); label1.setToolTipText("Hi i am label"); container.add(label1);

7 Icon icon = new ImageIcon("icon1.gif"); label2 = new JLabel("Second Label",icon, SwingConstants.LEFT); label2.setToolTipText("Hi am second label"); container.add(label2); setSize(200,200); setVisible(true); } } Continued….

8 public class Main { /** Creates a new instance of Main */ public Main() { } /** * @param args the command line arguments */ public static void main(String[] args) { LabelTesting test = new LabelTesting(); } Main Method

9 Event Handling The interaction of the user with GUI generates events. Three parts of an event handling mechanism –Event Source –Event Object –Event Listener

10 How event handling works How did the event handler get registered? How does the GUI Component know to the right event handler? Answers Step1:- Event registration for a control. Every JComponent has an object of class EventListenerList (javax.swing.event) called listenerList as an instance variable. References to all registered listeners are stored in listenerList. Consider the statement (textField1.addActionListener(handler))

11 Listener type is important in handling the respective event. Each event type has a corresponding event listener interface (Key Point) When an event occurs, the GUI Component is handed a unique event id specifying the event type. How event handling works

12 Practice on Event Handling public class TextFieldsTest extends JFrame implements ActionListener { private JTextField text1,text2; private JPasswordField passwd; String string = ""; public TextFieldsTest() { super("Text Fields Demonstration"); Container container = getContentPane(); container.setLayout(new FlowLayout()); text1 = new JTextField(10); container.add(text1); text2 = new JTextField("Enter Text"); container.add(text2);

13 passwd = new JPasswordField("Hidden Text"); container.add(passwd); text1.addActionListener(this); text2.addActionListener(this); passwd.addActionListener(this); setSize(350,250); setVisible(true); } Continued…

14 public void actionPerformed(ActionEvent event){ if (event.getSource() == text1){ string = "Text 1: " + event.getActionCommand(); } else if (event.getSource() == text2){ string = "Text 2: " + event.getActionCommand(); } else if (event.getSource() == passwd){ string = "Password: " + event.getActionCommand(); } JOptionPane.showMessageDialog(null,string); }

15 public static void main(String[] args) { TextFieldsTest test = new TextFieldsTest(); } Main Class


Download ppt "Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar."

Similar presentations


Ads by Google