F27SB2 Software Development 2 Lecture 2: Java GUIs 1.

Slides:



Advertisements
Similar presentations
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Advertisements

Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Java 212: Interfaces Intro to UML Diagrams. UML Class Diagram: class Rectangle +/- refers to visibility Color coding is not part of the UML diagram.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Introduction to Computation and Problem Solving Class 14: Introduction to the Swing Toolkit Prof. Steven R. Lerman and Dr. V. Judson Harward 1.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Copyright © 2002, Systems and Computer Engineering, Carleton University Gui1.ppt * Object-Oriented Software Development Part 18 Building.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
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.
MIT-AITI 2004 – Lecture 16 Introduction to Swing.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
COSC 3461: Module 2 Control Flow Paradigms: Reacting to the User.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
Csc Basic Graphical User Interface (GUI) Components.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
Java Swing One of the most important features of Java is its ability to draw graphics.
TCU CoSc Programming with Java The JFrame Class.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
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.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
Sep 181 Example Program DemoTranslateEnglishGUI.java.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©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
Object-Orientated Analysis, Design and Programming
CHAPTER Reacting to the user.
A Quick Java Swing Tutorial
Abstract Window ToolKit (AWT)
Ellen Walker Hiram College
Event-driven programming for GUI
Introduction to Computing Using Java
Graphics Programming - Frames
A Quick Java Swing Tutorial
Graphical User Interface
Presentation transcript:

F27SB2 Software Development 2 Lecture 2: Java GUIs 1

Overview Java GUIs based on 2 main class libraries AWT - Abstract Windowing Toolkit – original Java GUI classes Swing – subset of JFC - Java Foundation Classes – extends AWT – subtle but crucial differences with AWT... we will use Swing but need to be aware of AWT concepts

AWT import java.awt.*; contains original AWT GUI classes Component – main AWT class of GUI elements e.g Label, Button, TextField

Container class of Component s that can have other Component s inside them e.g. Frame – top-level displayable window e.g. Panel – Container with no display LayoutManager – interface class – control positions of nested Components

Container methods all Container s have rectangular size smallest enclosing rectangle – width – height public void setSize(int width,int height) width and height are in pixels pixel – one point on screen

Container methods size of pixel depends on – screen’s physical size diagonal in inches e.g. 15”; 17”; 21” – screen resolution number of columns/rows in pixels e.g. 1680*1050 NB make sure Container is big enough to hold everything! NB size of Containe r will change depending on properties of LayoutManager - more later

Container methods Container s may be visible or invisible public void setVisible(boolean b) make visible setVisible(true) make invisible setVisible(false) use to change Component availability e.g. turn available buttons on/off

Container methods Container s have: – background what things are drawn on e.g. paper – foreground what things are drawn in e.g pen e.g. Label

Container methods public void setBackground(Color c) set Container ’s background colour to c public void setForeground(Color c) set Container ’s foreground colour to c use these to change foreground/background dynamically at run-time

Container methods public class Color public static final Color black, blue, cyan, darkGrey, gray, green lightGray, magenta, orange, pink, red, white,yellow must preface colour identifier with Color e.g. Color.blue NB to make things in foreground disappear – set foreground to same colour as background

Basic Swing classes import javax.swing.*; many Swing classes extend AWT classes public class JComponent extends Container main Swing GUI class NB JComponents may be: opaque – can’t see through background transparent – can see through background

Basic Swing classes opacity depends on look and feel change opacity with: public void setOpaque(Boolean isOpaque) may need to setOpaque(true)

Frames public class JFrame extends Frame top-level window with: title-bar; menu-bar; cursor; icon; etc NB not a JComponent public void setTitle(String title) set title on Frame’s title bar to title

Frames to create and open a simple JFrame : class class-name extends JFrame {... public class-name(...){... } } frame is set up in a class called class-name which is an extended JFrame need an constructor for a class-name to – initialise an instance of the JFrame – hold JComponent s that make up the JFrame

Frames class program-name {... class-name frame-name;... } in the class for the main program need an instance of class-name called frame-name

Frames public static void main(String [] args) { frame-name = new class-Name(); frame-name.setTitle(“title”); frame-name.setSize(width,height); frame-name.setVisible(true); } call the constructor to instantiate frame- name to an instance of class-name give JFrame an appropriate title

Frames public static void main(String [] args) { frame-name = new class-Name(); frame-name.setTitle(“title”); frame-name.setSize(width,height); frame-name.setVisible(true); } set the JFrame ’s size to width and height – NB must allow size for title bar when specifying Frame height e.g. 20 pixels make the JFrame visible

Frames in general, cannot work directly on JFrame must manipulate the JFrame ’s ContentPane ContentPane is a Container has BorderLayout as default – more later to access ContentPane use: public Container getContentPane()

Frames e.g. 200*220 white window import java.awt.*;import javax.swing.*; class White extends JFrame { public White() { getContentPane(). setBackground(Color.white); } constructor explicitly locates ContentPane and sets background to white however, many methods will work implicitly on ContentPane e.g. add,setLayout

Frames class TestWhite { static White w; public static void main(String [] args) { w = new White(); w.setTitle(“White”); w.setSize(200,220); w.setVisible(true); } main constructs instance of White and sets up title, size and visibility

Events and listeners not yet enough to display window... need to provide further details for the operating system – what to do when window is opened or closed or iconified etc event – something that happens outside of a program listener – code that responds to an event

Events and listeners selecting window menu bar icons causes events events are detected by the Java system Java system: – stops our program – calls the appropriate listener

Events and listeners we need to... – import AWT Java event classes import java.awt.event.*; – tell the system that we have a listener for window events public void addWindowListener(WindowListener l) – build a WindowListener to respond to events

Events and listeners interface WindowListener package provides framework for building a WindowListener – abstract method defined by method signature type + name + parameters – no method body

Events and listeners interface – specification of abstract methods that implementation class will provide implementation class must provide method details

Events and listeners public interface WindowListener extends EvenListener { public void windowActivated(WindowEvent e); public void windowClosed(WindowEvent e); public void windowClosing(WindowEvent e); public void windowDeactivated(WindowEvent e); public void windowDeiconified(WindowEvent e); public void windowIconified (WindowEvent e); public void windowOpened(WindowEvent e); }

Events and listeners specific events when window is: – activated/deactivated or opened/closed or iconified/deiconified system: – calls the appropriate method from interface – provides a WindowEvent parameter to provide details of the event

Events and listeners all methods in WindowListener are abstract our class must – implement WindowListener – provide details for all methods we could implement details of all the interface methods ourselves...

Events and listeners better to: – subclass a supplied implementation of the interface – override methods we are concerned about public class WindowAdapter implements WindowListener provides empty versions of all above methods call WindowAdapter constructor – with body that defines our versions of methods – to return a WindowListener

Example e.g. 200*220 window with white background import java.awt.*; import java.awt.event.*; import javax.swing.*; class White extends JFrame { public White() { setBackground(Color.white); } }

Example class testWhite { public static void main(String [] args) { White w; w = new White(); w.setTitle(“White”); w.setSize(200,220); w.setVisible(true);...

Example... w.addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } } call addWindowListener – with a new WindowListener built from WindowAdapter – with a new windowClosing to exit from White

Example

Example: flashing window window flashes black to white and back every 0.5 sec repeatedly change background need some way of pausing import java.util.*; Calendar class Calendar Calendar.getInstance() returns a Calendar object which holds a snapshot of the current date and time

Example: flashing window long getTimeInMillis() returns time in milliseconds from Calendar object NB to update time, must create new Calendar object to pause: – find start time – repeatedly compare current time with start time – until required time has passed

Example: flashing window void pause(long millisecs) { long startTime = Calendar.getInstance(). getTimeInMillis(); while(Calendar. getInstance(). getTimeInMillis()- startTime<millisecs); }

Example: flashing window import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; class Blackwhite extends JFrame { public Blackwhite() { getContentPane(). setBackground(Color.black); } private void pause(long millisecs){...}

Example: flashing window public void flash() { while(true) { pause(500); getContentPane(). setBackground(Color.white); pause(500); getContentPane(). setBackground(Color.black); }

Example: flashing window class TestBlackwhite { public static void main(String [] args) { static Blackwhite b; b = new Blackwhite(); b.setTitle(“Black and white”); b.setSize(200,220); b.setVisible(true);

Example: flashing window b.addWindowListener (new WindowAdapter() { public void windowClosing (WindowEvent e) { System.exit(0); } }); b.flash(); }

Example: flashing window