Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java.awt GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel,

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Corresponds with Chapter 12
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Abstract Windowing Toolkit Design Goal: –allow the programmer to build o GUI that looks good on all platforms –provide a well-designed object-oriented.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
LayoutManager, J 1 Layout Manager. LayoutManager, J 2 Layout Manager To each UI container there is a layout manager (an object). When you add a component.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Animation Revisited The Timer class is in Swing. Animation can be done without using the Timer class. Using threads.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming 1 Java Programming II Events, AWT, and Swing.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
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.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
CS GUI Frameworks CS 3331 Fall CS 3331 Outline MVC Model GUI components (widgets) Layout managers Handling events.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
1 Java GUIs Dr. Randy M. Kaplan. 2 Constructing User Interfaces From a functionality standpoint there are two things that every program must implement.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Object Oriented Programming.  Interface  Event Handling.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Csc Basic Graphical User Interface (GUI) Components.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
OOSD Using Java GUI Programming. Components & Containers.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Gillian Miller Java Teacher Workshop :  Macquarie University 1Contents Advanced OO – Inheritance The Java API and documentation Strings Swing The Java.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Welcome To java
Aum Amriteshwaryai Namah
Event Handling Chapter 2 Objectives
Layout Managers Layout Manager—an object that decides how components will be arranged in a container Some types of layout managers: BorderLayout FlowLayout.
Swing JComponents.
GUI Programming III: Events
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
AWT.
Timer class and inner classes
Creating Graphical User Interfaces
GUI building with the AWT
Events, Event Handlers, and Threads
Programming Graphical User Interface (GUI)
GUI building with the AWT
Graphical User Interface
Presentation transcript:

Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java.awt GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel, Frame, Dialog, etc. Layout managers: FlowLayout, BorderLayout, etc. Supporting classes: Event handling java.awt.event package Graphics Color, Font, Graphics, etc. Geometry Point, Rectangle, Dimension, etc. Imaging Image class and java.awt.image package

The Component Hierarchy

The Swing Components

Layout Managers The layout of the elements in a container is handled by the layout manager associated with the container. Relative positions of the elements are specified, not their absolute coordinates. The positions and sizes of the element will be automatically adjusted when the window is resized.

The Layout Manager Hierarchy

Buttons and Flow Layout width=400 height=50 width=100 height=120

Buttons and Flow Layout (cont'd) Layout elements in horizontal rows. import java.awt.*; import java.applet.Applet; public class Flow extends Applet { public Flow () { setLayout(new FlowLayout()); add(new Button("Java")); add(new Button("C++")); add(new Button("Perl")); add(new Button("Ada")); add(new Button("Smalltalk")); add(new Button("Eiffel")); }

Border Layout

Border Layout (cont'd) import java.awt.*; import java.applet.Applet; public class Border extends Applet { public Border () { setLayout(new BorderLayout()); add(new Button("North"), BorderLayout.NORTH); add(new Button("South"), BorderLayout.SOUTH); add(new Button("East"), BorderLayout.EAST); add(new Button("West"), BorderLayout.WEST); add(new Button("Center"), BorderLayout.CENTER); }

Grid Layout row=3 col=2 row=1 col=0 row=0 col=1

Grid Layout (cont'd) import java.awt.*; import java.applet.Applet; public class Grid extends Applet { public void init () { int row = 0, col = 0; String att = getParameter("row"); if (att != null) row = Integer.parseInt(att); att = getParameter("col"); if (att != null) col = Integer.parseInt(att); if (row == 0 && col == 0) { row = 3; col = 2; }

Grid Layout (cont'd) (class Grid continued.) setLayout(new GridLayout(row, col)); add(new Button("Java")); add(new Button("C++")); add(new Button("Perl")); add(new Button("Ada")); add(new Button("Smalltalk")); add(new Button("Eiffel")); }

Nested Panels

Nested Panels (cont'd) public class NestedPanels extends Applet { protected Label messageBar; protected Choice choice; public NestedPanels () { // set up the center panel Panel center = new Panel(); center.setLayout(new BorderLayout()); center.add(new Button("south"), BorderLayout.SOUTH); center.add(new Button("north"), BorderLayout.NORTH); center.add(new Button("east"), BorderLayout.EAST); center.add(new Button("west"), BorderLayout.WEST); center.add(new Button("center"), BorderLayout.CENTER);

(class NestedPanels continued.) // set up the south panel Panel south = new Panel(); south.setLayout(new FlowLayout()); south.add(new Button("Help")); choice = new Choice(); choice.addItem("one"); choice.addItem("two"); choice.addItem("three"); choice.addItem("four"); choice.addItem("five"); south.add(choice); messageBar = new Label("This is a message bar."); south.add(messageBar);

Nested Panels (cont'd) (class NestedPanels continued.) // set up the outer panel setLayout(new BorderLayout()); add(new Button("North"), BorderLayout.NORTH); add(new Button("East"), BorderLayout.EAST); add(new Button("West"), BorderLayout.WEST); add(south, BorderLayout.SOUTH); add(center, BorderLayout.CENTER); }

Event Handling Event source: buttons, checkboxes, choices Event listener: any class interested in handling certain events A listener must implement an appropriate listener interface; inform the source that it is interested in handling a certain type of events. A listener may listen to several sources and different types of events. The source may also be the listener. Listeners can be full-fledged classes or inner classes.

The Event Object and Listener Classes ActionEvent ActionListener ItemEvent ItemListener MouseEvent MouseListener MouseMotionListener MouseAdapter MouseMotionAdapter KeyEvent KeyListener KeyAdapter WindowEvent WindowListener WindowAdapter XyzListener are interfaces. XyzAdapter are classes that implement the corresponding listener interfaces.

Nested Panels, Handling Events import java.awt.*; import java.awt.event.*; public class NestedPanels2 extends NestedPanels implements ActionListener, ItemListener { public NestedPanels2() { super(); // create all the components // register item listener choice.addItemListener(this); // register action listener registerButtonHandler(this); }

Event Handling Methods public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { messageBar.setText("Choice selected: " + event.getItem()); } public void actionPerformed(ActionEvent event) { Button source = (Button) event.getSource(); messageBar.setText("Button pushed: " + source.getLabel()); }

Register The Listener protected void registerButtonHandler(Component comp) { if (comp != null) { if (comp instanceof Button) { Button button = (Button) comp; button.addActionListener(this); } else if (comp instanceof Container) { Container container = (Container) comp; int n = container.getComponentCount(); for (int i = 0; i < n; i++) registerButtonHandler( container.getComponent(i)); }

Event Handling Using Inner Class import java.awt.*; import java.awt.event.*; public class NestedPanels3 extends NestedPanels { public NestedPanels3() { super(); ChoiceEventHandler cHandler = new ChoiceEventHandler(); choice.addItemListener(cHandler); ButtonEventHandler bHandler = new ButtonEventHandler(); bHandler.registerButtonHandler(this); }

Inner Class Listener Inner classes classes that reside inside other (full-fledged) classes. Intended to be small. serve as helpers to the enclosing class. class ChoiceEventHandler implements ItemListener { public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { messageBar.setText("Choice selected: " + event.getItem()); }

Inner Class Listener (cont'd) class ButtonEventHandler implements ActionListener { public void actionPerformed(ActionEvent event){ Button source = (Button) event.getSource(); messageBar.setText("Button pushed: " + source.getLabel()); }

Inner Class Listener (cont'd) (class ButtonEventHandler continued.) protected void registerButtonHandler(Component comp) { if (comp != null) { if (comp instanceof Button) { Button button = (Button) comp; button.addActionListener(this); } else if (comp instanceof Container) { Container container = (Container) comp; int n = container.getComponentCount(); for (int i = 0; i < n; i++) registerButtonHandler( container.getComponent(i)); }