Building Graphical User Interfaces. Overview

Similar documents
Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Building Graphical User Interfaces. GUI Principles

Introduction to the JAVA UI classes Advanced HCI IAT351

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Window Interfaces Using Swing Objects

Today. cisc3120-fall2012-parsons-lectiii.3 2

import javax.swing.*; import java.awt.*; import java.awt.event.*;

Window Interfaces Using Swing Objects

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Chapter 14. More Swing

Chapter 13 Lab Advanced GUI Applications

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

More Swing. CS180 Recitation 12/(04,05)/08

Graphic User Interfaces. - GUI concepts - Swing - AWT

Programming graphics

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Inheritance (cont) Abstract Classes

encompass a group of features for building Graphical User Interfaces (GUI).

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

A Simple Text Editor Application

1005ICT Object Oriented Programming Lecture Notes

Part I: Learn Common Graphics Components

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

More Swing. Chapter 14. Chapter 14 1

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

AP CS Unit 11: Graphics and Events

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Part 3: Graphical User Interface (GUI) & Java Applets

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Graphical User Interface (GUI)

Jonathan Aldrich Charlie Garrod

Graphical User Interfaces in Java - SWING

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

CSE 8B Intro to CS: Java

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

1. Swing Note: Most of the stuff stolen from or from the jdk documentation. Most programs are modified or written by me. This section explains the

CS 251 Intermediate Programming GUIs: Components and Layout

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Graphical User Interfaces. Comp 152

Systems Programming Graphical User Interfaces

Java: Graphical User Interfaces (GUI)

Using Several Components

JLayeredPane. Depth Constants in JLayeredPane

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical interfaces & event-driven programming

JAVA NOTES GRAPHICAL USER INTERFACES

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

JFrame In Swing, a JFrame is similar to a window in your operating system

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Introduction. Introduction

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Java. GUI building with the AWT

MIT AITI Swing Event Model Lecture 17

Programming Languages and Techniques (CIS120e)

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Window Interfaces Using Swing. Chapter 12

Laying Out Components. What is Widget Layout?

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Graphical Interfaces

Chapter 6: Graphical User Interfaces

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Graphical Interfaces

FirstSwingFrame.java Page 1 of 1

PIC 20A GUI with swing

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Programmierpraktikum

(Incomplete) History of GUIs

We are on the GUI fast track path

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Swing from A to Z Some Simple Components. Preface

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Graphical User Interface (GUI)

Event Driven Programming

Dr. Hikmat A. M. AbdelJaber

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Basicsof. JavaGUI and SWING

Widgets. Widgets Widget Toolkits. User Interface Widget

Attempt FOUR questions Marking Scheme Time: 120 mins

Example 3-1. Password Validation

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Java Programming Lecture 6

Transcription:

Building Graphical User Interfaces 4.1 Overview Constructing GUIs Interface components GUI layout Event handling 2 1

GUI Principles Components: GUI building blocks. Buttons, menus, sliders, etc. Layout: arranging components to form a usable GUI. Using layout managers. Events: reacting to user input. Button presses, menu selections, etc. 3 AWT and Swing 4 2

Elements of a frame Menu bar Title Window controls Content pane 5 Creating a frame import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ImageViewer private JFrame frame; /** * Create an ImageViewer show it on screen. */ public ImageViewer() makeframe(); // rest of class omitted. 6 3

The content pane /** * Create the Swing frame and its content. */ private void makeframe() frame = new JFrame("ImageViewer"); Container contentpane = frame.getcontentpane(); JLabel label = new JLabel("I am a label."); contentpane.add(label); frame.pack(); frame.setvisible(true); 7 Adding menus JMenuBar Displayed below the title. Contains the menus. JMenu e.g. File. Contains the menu items. JMenuItem e.g. Open. Individual items. 8 4

private void makemenubar(jframe frame) JMenuBar menubar = new JMenuBar(); frame.setjmenubar(menubar); // create the File menu JMenu filemenu = new JMenu("File"); menubar.add(filemenu); JMenuItem openitem = new JMenuItem("Open"); filemenu.add(openitem); JMenuItem quititem = new JMenuItem("Quit"); filemenu.add(quititem); 9 Event handling Events correspond to user interactions with components. Components are associated with different event types. Frames are associated with WindowEvent. Menus are associated with ActionEvent. Objects can be notified when an event occurs. Such objects are called listeners. 10 5

Centralized event receipt A single object handles all events. Implements the ActionListener interface. Defines an actionperformed method. It registers as a listener with each component. item.addactionlistener(this) It has to work out which component has dispatched the event. 11 ActionListener public interface ActionListener public void actionperformed(actionevent ev); 12 6

public class ImageViewer implements ActionListener public void actionperformed(actionevent e) String command = e.getactioncommand(); if(command.equals("open")) else if (command.equals("quit")) private void makemenubar(jframe frame) openitem.addactionlistener(this); 13 Centralized event handling The approach works. It is used, so you should be aware of it. However It does not scale well. Identifying components by their text is fragile. An alternative approach is preferred. 14 7

Nested class syntax Class definitions may be nested. public class Enclosing private class Inner 15 Inner classes Instances of the inner class are localized within the enclosing class. Instances of the inner class have access to the private members of the enclosing class. 16 8

Anonymous inner classes Obey the rules of inner classes. Used to create one-off objects for which a class name is not required. Use a special syntax. The instance is always referenced via its supertype, as it has no subtype name. 17 Anonymous action listener JMenuItem openitem = new JMenuItem("Open"); openitem.addactionlistener(new ActionListener() public void actionperformed(actionevent e) openfile(); ); 18 9

Anonymous class elements Anonymous object creation openitem.addactionlistener( new ActionListener() public void actionperformed(actionevent e) openfile(); ); Class definition Actual parameter 19 Exit on window close frame.addwindowlistener(new WindowAdapter() public void windowclosing(windowevent e) System.exit(0); ); WindowAdapter provides a no-op implementation of the WindowListener interface. 20 10

The imageviewer project 21 Image processing 22 11

Class responsibilities ImageViewer Sets up the GUI structure. ImageFileManager Static methods for image file loading and saving. ImagePanel Displays the image within the GUI. OFImage Models a 2D image. 23 OFImage Our subclass of BufferedImage. Represents a 2D array of pixels. Important methods: getpixel, setpixel getwidth, getheight Each pixel has a color. We use java.awt.color. 24 12

Adding an ImagePanel public class ImageViewer private JFrame frame; private ImagePanel imagepanel; private void makeframe() Container contentpane = frame.getcontentpane(); imagepanel = new ImagePanel(); contentpane.add(imagepanel); 25 public class ImageViewer private JFrame frame; private ImagePanel imagepanel; Loading an image private void openfile() File selectedfile = ; OFImage image = ImageFileManager.loadImage(selectedFile); imagepanel.setimage(image); frame.pack(); 26 13

Layout managers Manage limited space for competing components. FlowLayout, BorderLayout, GridLayout, BoxLayout, GridBagLayout. Manage Container objects, e.g. a content pane. Each imposes its own style. 27 FlowLayout 28 14

BorderLayout 29 GridLayout 30 15

BoxLayout Note: no component resizing. 31 Nested containers Sophisticated layouts can be obtained by nesting containers. Use JPanel as a basic container. Each container will have its own layout manager. 32 16

Dialogs Modal dialogs block all other interaction. Forces a response from the user. Non-modal dialogs allow other interaction. This is sometimes desirable. May be difficult to avoid inconsistencies. 33 JOptionPane standard dialogs Message dialog Message text plus an OK button. Confirm dialog Yes, No, Cancel options. Input dialog Message text and an input field. Variations are possible. 34 17

A message dialog private void showabout() JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION, "About ImageViewer", JOptionPane.INFORMATION_MESSAGE); 35 Image filters Functions applied to the whole image. int height = getheight(); int width = getwidth(); for(int y = 0; y < height; y++) for(int x = 0; x < width; x++) Color pixel = getpixel(x, y); alter the pixel's color value; setpixel(x, y, pixel); 36 18

Adding further filters private void makelighter() if(currentimage!= null) currentimage.lighter(); frame.repaint(); showstatus("applied: lighter"); else showstatus("no image loaded."); Code duplication? Refactor! private void threshold() if(currentimage!= null) currentimage.threshold(); frame.repaint(); showstatus("applied: threshold"); else showstatus("no image loaded."); 37 Adding further filters Define a Filter superclass (abstract). Create function-specific subclasses. Create a collection of subclass instances in ImageViewer. Define a generic applyfilter method. See imageviewer2-0. 38 19

imageviewer2-0 39 Buttons and nested layouts A GridLayout inside a FlowLayout inside a BorderLayout. 40 20

Borders Used to add decoration around components. Defined in javax.swing.border BevelBorder, CompoundBorder, EmptyBorder, EtchedBorder, TitledBorder. 41 Adding spacing JPanel contentpane = (JPanel)frame.getContentPane(); contentpane.setborder(new EmptyBorder(6, 6, 6, 6)); // Specify the layout manager with nice spacing contentpane.setlayout(new BorderLayout(6, 6)); imagepanel = new ImagePanel(); imagepanel.setborder(new EtchedBorder()); contentpane.add(imagepanel, BorderLayout.CENTER); 42 21

Other components Slider Spinner Tabbed pane Scroll pane 43 Review Aim for cohesive application structures. Endeavor to keep GUI elements separate from application functionality. Pre-defined components simplify creation of sophisticated GUIs. Layout managers handle component juxtaposition. Nest containers for further control. 44 22

Review Many components recognize user interactions with them. Reactive components deliver events to listeners. Anonymous inner classes are commonly used to implement listeners. 45 Acknowledgement The original authors of these slides are the authors of the textbook. The instructor made necessary modifications, with permissions from the authors. 46 23