Abstract Windowing Toolkit Design Goal: –allow the programmer to build o GUI that looks good on all platforms –provide a well-designed object-oriented.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

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 Interfaces
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
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.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java.awt GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel,
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
Java Programming 1 Java Programming II Events, AWT, and Swing.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Objectives of This Session
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Csc Basic Graphical User Interface (GUI) Components.
CPSC 233 Tutorial Xin Apr 6, Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
AWT Layout Managers (Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 3 Designing the Interface with Layout Managers.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Java Swing - Lecture 3 Layout Management
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Session 10. Java Simplified / Session 10 / 2 of 36  An Applet is a Java program that can be executed with the help of a Java enabled browser.  Every.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Welcome To java
Modern Programming Language Java
University of Central Florida COP 3330 Object Oriented Programming
AWT Components.
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
AWT.
LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes.
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Abstract Windowing Toolkit Design Goal: –allow the programmer to build o GUI that looks good on all platforms –provide a well-designed object-oriented interface to low-level services and resources (tracking the mouse, reading the keyboard, writing to the screen etc.) so programmers don't have to worry about the details The user interface elements provided by the AWT are implemented using each platform's native GUI toolkit, thereby preserving the look and feel of each platform.

Abstract Windowing Toolkit java.awt –Graphics colors, images, fonts, polygons, e.t.c. –Components GUI (graphical user interface) components (buttons, menus, lists, dialog boxes) –Layout Managers classes which control the layout of components within their container objects java.awt.datatransfer (classes for cut-and-paste) java.awt.event (classes for event handling) java.awt.image (classes for image manipulation)

Inheritance relationship between the user interface component classes

Component classes (detailed)

The basic awt components

Basic Applet import java.awt.*; import java.applet.*; public class Example1 extends Applet { public void paint(Graphics g) { g.drawString("First applet", 10, 10); }

HTML Code that contains the applet Applet Test Page Applet Test Page <applet code="Example1.class" width=400 height=400 name="Applet1">

Another Example import java.awt.*; import java.applet.*; public class Example2 extends Applet { Button b1 = new Button("Button 1"), b2 = new Button("Button 2"); public void init() { add(b1); add(b2); }

How AWT components draw themselves programs can draw only when AWT tells them to AWT orders drawing requests by making them run in a single thread AWT orders a component to draw itself by invoking its update() method The default implementation of the update() method clears the Component’s background and then calls the paint() method The default implementation of the paint() method does nothing AWT can call the paint() method directly (e.g. when an area of a Component is revealed after being hidden behind an other window) a Graphics object is the only argument of paint() and update() –represents the context in which the component can perform its drawing The Graphics class provide methods for drawing and filling lines, rectangles e.t.c., images, text, for setting and getting colors and fonts e.t.c.

Creating the components of a simple editor (a) The Menus Create a new class which extends the Frame class –public class SimpleEditor extends Frame{... } In the Constructor Create a MenuBar object –MenuBar menubar = new MenuBar(); this.setMenuBar(menubar); // this refers to the Frame object Create and add the Menu objects (File) to the menu bar –Menu file = new Menu(‘’File’’); menubar.add(file); Create and add the MenuItem objects to the Menu containers (Open, New, Save, Save as, Exit) –MenuItem open = new MenuItem(‘’Open’’); file.add(open);

Creating the components of a simple editor (b) The Panel Create a new TextArea object and add it to the scroll pane –TextArea textArea = new TextArea(); pane.add(textArea); Set the frame size and pop it up –this.setSize(300, 300); –this.pack(); –this.show();

java.awt.event - Events, Listeners, Adapters Events –represent event occurrences –java.awt.AWTEvent is the superclass of all the awt events –ActionEvent, TextEvent, MouseEvent, KeyEvent e.t.c. Listeners –extend the java.util.EventListener interface –provide methods that handle events –ActionListener, TextListener, MouseListener, KeyListener e.t.c. Adapters –implement the corresponding listener, providing empty bodies for all the methods of it (it is useful when we don’ t want to implement all the methods of a listener). –MouseAdapter, KeyAdapter e.t.c.

Example with Events import java.awt.*; import java.awt.event.*; // Must add this import java.applet.*; public class Example5 extends Applet { Button b1 = new Button("Button 1"), b2 = new Button("Button 2"); public void init() { b1.addActionListener(new B1()); b2.addActionListener(new B2()); add(b1); add(b2); } class B1 implements ActionListener { public void actionPerformed(ActionEvent e) { getAppletContext().showStatus("Button 1"); } class B2 implements ActionListener { public void actionPerformed(ActionEvent e) { getAppletContext().showStatus("Button 2"); }

Adding events to our editor Opening a document –open.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { openDocument(); } }); Saving a document –save.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { saveDocument(); } }); Handling window close requests –this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } });

Opening a file - File Dialogs displaying an open file dialog FileDialog dialog = new FileDialog(this, "Select a file", FileDialog.LOAD); dialog.show(); if(dialog.getFile() == null) return; filename = dialog.getDirectory() + File.separator + dialog.getFile(); to display a save file dialog FileDialog dialog = new FileDialog(this, "Select a file", FileDialog.SAVE); dialog.show();

Layout Managers A Layout Manager is an object that controls the position and the size of components in a container Every layout manager implements the LayoutManager interface By default, every Container has a layout manager object that controls its layout –for Panel objects: instances of the FlowLayout class –for Window objects: instances of the BorderLayout class There are five layout manager classes in AWT –FlowLayout –GridLayout –BorderLayout –CardLayout –GridBagLayout

BorderLayout It’s the default layout manager for all Windows (Frames, Dialogs, e.t.c.) the layout is divided into five areas: North, South, East, West and Center Using BorderLayout –setLayout(new BorderLayout()); add(“North”, new Button(“North”)); add(“South”, new Button(“South”)); add(“East”, new Button(“East”)); add(“West”, new Button(“West”)); add(“Center”, new Button(“Center”)); to insert gaps between the components we use the following constructor: –public BorderLayout(int horizontalGap, int verticalGap)

FlowLayout It’s the default layout manager for all Panels It simply lays out components (at their preferred size) from left to right, starting new rows if there is not enough space –setLayout(new FlowLayout); add(new Button(«This is a long-named button»)); add(new Button(«Hello»)); Within each row the components are centered (by default), left- aligned or right-aligned Constructors –public FlowLayout() –public FlowLayout(int alignment) –public FlowLayout(int alignment, int horizontalGap, verticalGap) –alignment: FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER

GridLayout Arranges components into a grid of rows and columns The cells are equal size based on the largest component in the grid Adding components –setLayout(new GridLayout(0,2));//construct a GridLayout with two //columns and unspecified number of rows add(new Button(“Button 1”)); add(new Button(“2”)); add(new Button(“Button 3”)); add(new Button(“Long-Named Button 4”)); add(new Button(“Button 5”)); At least one of the rows and columns must be non-zero

CardLayout Helps to manage two or more components that share the same display space. Adding components Panel cards = new Panel(); cards.setLayout(new CardLayout());...//Create a Panel named p1. Put buttons in it....//Create a Panel named p2. Put a text field in it. cards.add("Panel with Buttons", p1); cards.add("Panel with TextField", p2); Showing its components ((CardLayout)cards.getLayout()).show(cards, “Panel with Buttons”); Choosing a component public void first(Container panel) public void next(Container panel) public void previous(Container panel) public void last(Container panel) public void show(Container panel, String name)

GridBagLayout The most flexible and complex layout manager Places components in a grid of rows and columns the applet specifies the size and position characteristics of its components is by by means of GridBagConstraints GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridbag); //...Create the component... //...Set instance variables in the GridBagConstraints instance... gridbag.setConstraints(theComponent, c); add(theComponent);

GridBagConstraints instance variables gridx, gridy : Specify the column and row of the upper left of the component gridwidth, gridheight : Specify the number of columns or rows in the component's display area fill : how to resize the component when its display size is larger than its requested size ipadx, ipady : how many pixels to add to the minimum size of the component insets : the minimum amount of space between the component and the edges of its display area anchor : where to place the component when it’s smaller than its display area weightx, weighty : how to distribute space among columns (weightx) and among rows (weighty)

Absolute Positioning we do not use any layout manager –setLayout(null); adding the components –Button b1 = new Button(«one»); –add(b1); specifying the exact size and position –We overwrite the paint(Graphics g) method and specify the position and size of each component: b1.reshape(70 + insets.left, 35 + insets.top, 50, 20); We should avoid using absolute positioning in order to guarantee a platform-dependent component appearance

Example with layouts/panels (1) import java.applet.*; import java.awt.*; public class Example6 extends Applet { public void init() { Panel p; setLayout(new BorderLayout()); p = new Panel(); p.add(new TextArea()); add("Center", p); p = new Panel(); p.add(new Button("One")); p.add(new Button("Two")); Choice c = new Choice(); c.addItem("one"); c.addItem("two"); c.addItem("three"); p.add(c); add("South", p); } continued…

Example with layouts/panels (2) public static void main(String [] args) { Frame f = new Frame("Example 6"); Example6 ex = new Example6(); ex.init(); f.add("Center", ex); f.pack(); f.show(); }

Common problems when a window never shows up –set the window size or pack it when the component never shows up –if the container is already visible call validate() on the container after adding the componen ô How can I specify a component’s exact size? –standard components size depends on the platform and the fonts that are used. You don’t need to specify their exact size. –for custom components you need to override getMinimumSize() and getPreferreSize() methods. –component sizes are subject to the layout manager you use

Shapes, Text, Images The class Graphics provides methods for drawing Lines, Rectangles (simple, 3D and round-edged rectangles), Ovals, Arcs and Polygons. –g.drawLine(10, 10, 120, 100); drawing text –g.drawString(«Hello World», 50, 50); //g: instance of Graphics setting fonts –g.setFont(new Font(new Font(«Helvetica», Font.ITALIC, 14))); loading Images –Image image = getImage(URL); //in an applet –Image image = Toolkit.getDefaultToolkit().getImage(filenameOrURL); displaying Images –g.drawImage(image, 0, 0, this);

Extending AWT – JFC Swing a new GUI API based on AWT –new visual components (tables, split panes, toolbars, trees, progress bars e.t.c) –drag and drop support –pluggable Look & Feel support two releases –JFC 1.1 (for use with JDK 1.1) –JDK 1.2 For each AWT component there is a equivalent Swing component (JButton, JLabel, JFrame, e.t.c.) –Swing components are implemented with absolutely no native code –They have much more capabilities than AWT components