PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Creating User Interfaces. 2 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
Graphical User Interface Components: Part 1 Chapter 11.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Graphical User Interface Components: Part 1 Chapter 11.
Creating User Interfaces Event-Driven Programming.
Basics of GUI Programming Chapter 11 and Chapter 22.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
1 Lecture 8: User Interface Components with Swing.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
CSC 205 Programming II Lecture 5 AWT - I.
Welcome To java
CompSci 230 S Programming Techniques
Handling User Events with Swing
A First Look at GUI Applications
Advanced User Interfaces
Lecture 09 Applets.
Java GUI.
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Chapter 7 Creating User Interfaces
Advanced Programming in Java
Graphical User Interface
Presentation transcript:

PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra

Software Development Science or Art? Science Parts of it have fixed objective which must be met Building new technology to help mankind Art Most clients just give a rough idea what they want The final product is result of programmer’s imaginations The more you practice, the better you get at it There are basic techniques that can be taught; other than that you are on your own

Task for Today Build a java application for a basic calculator The user selects the basic operations from drop-down menu The options should be addition, subtraction, multiplication, devision The user enters the numbers in two text fields provided The result appears in third text field

A tip that always helps Approach the problem in small steps Breaking the problem down in steps helps debugging For example in this case First write an application just to launch the application window Change your program to include the textfields and labels Write a code to just do addition Plug in the drop-down menu and adjust the program accordingly

Working with swing A package written for developing sophisticated GUI Adopts style of the operating system or style unique to Java Each style is called look and feel Java 7 introduces new look and feel called Nimbus, that is unique to Java Must avail the swing package first import javax.swing.*; Other packages supporting GUI programming java.awt (Abstract Windowing Toolkit) java.awt.event (class handling user input) Swing generally more powerful than awt Not implemented by platform specific code, but written entirely in java Supplies plenty of exciting additions like scroll panes, trees, tables etc When using Swing component, you work with object of that Swing class All Swing components are subclasses of abstract class JComponent

Creating Interface Create a class that represents the main GUI In most projects, main interface is a frame (JFrame class) Frame is a window that appears whenever you open an application Frame has a title bar, Maximize, Minimize, Close buttons and other features Users of graphical environments expect to move, resize and close the windows To create a graphical Java application, make the interface a subclass of JFrame public class MyFirstGui extends Jframe Set title by either setTitle(String) or super(String) in the constructor Set size by setSize(int, int) where first number denotes width and second height in pixels Make your frame visible by setVisible(true); Set default close operation to exit on closing setDefaultCloseOption(EXIT_ON_CLOSE) Other close options DISPOSE_ON_CLOSE HIDE_ON_CLOSE DO_NOTHING_ON CLOSE

Adding Components Class JButton Three constructors JButton(String)  Label on the button JButton(Icon)  Icon on the button JButton(String,Icon)  Both icon and Label on the button JButton b = new JButton(“OK”); Class JPanel After creating components, need to add them to a container Simplest container JPanel p = new JPanel(); p.add(b); //add button b to panel p

Adding Components Class JTextField JTextField()  Empty Text field JTextField(int)  Text Field with specified width JTextField(String, int)  Text Field with specified width and text setEditable(False) for text fields that do not accept user input Class JLabel Three Constructors JLabel(String)  Label with specified text JLabel(String, int)  Label with specified text and alignment SwingConstants.LEFT SwingConstants.RIGHT SwingConstants.CENTER JLabel(String, Icon, int)  Label with specified text, icon and alignment

Adding Components Class JComboBox Creates a list of items that are hidden when menu is not being used addItem(Object)  Add item to the list getItemAt(int)  Returns the text of the list item at specified index position getSelectedIndex()  Returns the index position of selected item getSelectedItem()  Returns the text of currently selected item setSelectedIndex(int)  Selects the item at indicated index position setSelectedIndex(object)  Selects specified object in the list

Responding to User Inputs Use java.awt.event package Useful listener interfaces ActionListener  When user performs an action on a component. AdjustmentListener  When component is adjusted FocusListener  Textfield or area loses or gains focus ItemListener  When items in items such as combo box are changed KeyListener  When user enters text using keyboard MouseListener  Generated by mouse clicks, mouse entering or leaving a component area MouseMotionListener  track all movement by mouse over component WindowListener  When window is minimized, maximized, moved or closes Implement listener in class definition public class t extends JFrame implements ActionListener, ItemListener { //…}

Setting up Components Need to add the attribute of listening to specific objects addActionListener()  JButton, JCheckBox, JComboBox, JTextField, JRadioButton and JMenuItem addFocusListener()  all Swing Component addItemListener()  JButton, JCheckBox, JComboBox and JRadioButton addKeyListener()  all Swing Components addMouseListener()  all Swing Components addMouseMotionListener()  all Swing Components addTextListener()  JTextField and JTextArea addWindowListener()  JWindow and JFrame JButton b = new JButton(‘OK”); b.addActionListener();

Event Handling Methods Action Events ActionListener  actionPerformed(ActionEvent) Focus Events FocustListener  focusGained(FocusEvent) and focusLost(FocusEvent) Item Events ItemListener  itemStateChanged(ItemEvent) When a class implements an event interface, must define the corresponding event handling methods public void actionPerformed(ActionEvent e) { //..} To know which for which object the event was generated Object source=e.getSource(); If the event was triggered by button b, you can check using if (source==b) { //..}

Summary of designing application Import packages Create a Class that extends JFrame and implements all the user interfaces required Make sure to set the frame to visible, set title, declare size, the application exits on closing Create the components Add property of user input listening to the components Add the components to a simple container (optional) Add the container to the Frame Define what to do when the user enters required input Define event handling methods