BASICS OF GRAPHICAL APPS

Similar documents
Graphical Applications

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

Chapter 6: Graphical User Interfaces

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

Introduction to the JAVA UI classes Advanced HCI IAT351

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

GUI Components: Part 1

Chapter 7: A First Look at GUI Applications

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!

GUI in Java TalentHome Solutions

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

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

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CS 251 Intermediate Programming GUIs: Components and Layout

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

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

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphical User Interfaces. Comp 152

Chapter 12 Advanced GUIs and Graphics

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components

Graphic User Interfaces. - GUI concepts - Swing - AWT

Laying Out Components. What is Widget Layout?

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

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

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

Unit 6: Graphical User Interface

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

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

CS11 Java. Fall Lecture 4

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

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

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

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

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

To gain experience using GUI components and listeners.

Chapter 5: Enhancing Classes

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Widgets. Widgets Widget Toolkits. User Interface Widget

Sri Vidya College of Engineering & Technology

Mach4 CNC Controller Screen Editing Guide Version 1.0

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

Lecture 5: Java Graphics

This page intentionally left blank

Java Programming Lecture 6

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

CS 4300 Computer Graphics

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

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

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

Lecture 9. Lecture

Part I: Learn Common Graphics Components

PIC 20A GUI with swing

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

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

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

2110: GUIS: Graphical User Interfaces

Swing. By Iqtidar Ali

Lecture 19 GUI Events

Graphical User Interface (GUI)

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

H212 Introduction to Software Systems Honors

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

With ClaroIdeas you can quickly and easily create idea maps using a combination of words, symbols and pictures.

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

Window Interfaces Using Swing Objects

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Excel 2013 Intermediate

Java IDE Programming-I

Window Interfaces Using Swing Objects

Microsoft Office Training Skills 2010

Lecture 18 Java Graphics and GUIs

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Lecture 3: Java Graphics & Events

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

Systems Programming Graphical User Interfaces

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

Anatomy of a Window (Windows 7, Office 2010)

Graphical User Interfaces (GUIs)

CS11 Java. Fall Lecture 3

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Widget. Widget is a generic name for parts of an interface that have their own behaviour. e.g., buttons, progress bars, sliders, drop-down

&KDSWHU(QKDQFLQJ&ODVVHV

Clip Art and Graphics. Inserting Clip Art. Inserting Other Graphics. Creating Your Own Shapes. Formatting the Shape

2IS45 Programming

CSE 331 Software Design & Implementation

Introduction to Windows

Come & Join Us at VUSTUDENTS.net

MyProgram m i ng Lab. get with the programming. Through the power of practice and immediate personalized

PowerPoint Spring 2002

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

1.00 Lecture 14. Lecture Preview

Transcription:

CSC 2014 Java Bootcamp Lecture 7 GUI Design BASICS OF GRAPHICAL APPS 2 Graphical Applications So far we ve focused on command-line applications, which interact with the user using simple text prompts In this lecture, we re begin to explore Java applications that have graphical components These components will serve as a foundation to programs that have true graphical user interfaces (GUIs) GUI Components A GUI component is an object that represents a screen element such as a button or a text field GUI-related classes are defined primarily in the java.awt and the javax.swing packages The Abstract Windowing Toolkit (AWT) was the original Java GUI package The Swing package provides additional and more versatile components Both packages are needed to create a Java GUI-based program 3 4 GUI Containers A GUI container is a component that is used to hold and organize other components A frame is a container that is used to display a GUIbased Java application A frame is displayed as a separate window with a title bar it can be repositioned and resized on the screen as needed A panel is a container that cannot be displayed on its own but is used to organize other components A panel must be added to another container to be displayed GUI Containers A GUI container can be classified as either heavyweight or lightweight A heavyweight container is one that is managed by the underlying operating system A lightweight container is managed by the Java program itself Occasionally this distinction is important A frame is a heavyweight container and a panel is a lightweight container 5 6 1

Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: components events listeners First, components are objects that represent screen elements labels, buttons, text fields, menus, etc. GRAPHICAL COMPONENTS Some components are containers that hold and organize other components frames, panels, applets, dialog boxes 7 8 Labels A label is a GUI component that displays a line of text Labels are usually used to display information or identify other components in the interface Buttons A push button is a component that allows the user to initiate an action by pressing a graphical button using the mouse A push button is defined by the JButton class It generates an action event The PushCounter example displays a push button that increments a counter each time it is pushed 9 10 Text Fields Let's look at another GUI example that uses another type of component A text field allows the user to enter one line of input If the cursor is in the text field, the text field component generates an action event when the enter key is pressed Dialog Boxes A dialog box is a window that appears on top of any currently active window It may be used to: convey information confirm an action allow the user to enter data pick a color choose a file A dialog box usually has a specific, solitary purpose, and the user interaction with it is brief 11 12 2

Dialog Boxes The JOptionPane class provides methods that simplify the creation of some types of dialog boxes Check Boxes A check box is a button that can be toggled on or off It is represented by the JCheckBox class Unlike a push button, which generates an action event, a check box generates an item event whenever it changes state (is checked on or off) The ItemListener interface is used to define item event listeners The check box calls the itemstatechanged method of the listener when it is toggled 13 14 Radio Buttons A group of radio buttons represents a set of mutually exclusive options only one can be selected at any given time When a radio button from a group is selected, the button that is currently "on" in the group is automatically toggled off To define the group of radio buttons that will work together, each radio button is added to a ButtonGroup object File Choosers Situations often arise where we want the user to select a file stored on a disk drive, usually so that its contents can be read and processed A file chooser, represented by the JFileChooser class, simplifies this process The user can browse the disk and filter the file types displayed A radio button generates an action event 15 16 Sliders A slider is a GUI component that allows the user to specify a value within a numeric range A slider can be oriented vertically or horizontally and can have optional tick marks and labels The minimum and maximum values for the slider are set using the JSlider constructor Nested Panels Containers that contain other components make up the containment hierarchy of an interface This hierarchy can be as intricate as needed to create the visual effect desired A slider produces a change event when the slider is moved, indicating that the slider and the value it represents has changed 17 18 3

Scroll Panes A scroll pane is useful for images or information too large to fit in a reasonably-sized area A scroll pane offers a limited view of the component it contains It provides vertical and/or horizontal scroll bars that allow the user to scroll to other areas of the component Split Panes A split pane (JSplitPane) is a container that displays two components separated by a moveable divider bar The two components can be displayed side by side, or one on top of the other Moveable Divider Bar No event listener is needed for a scroll pane Left Component Right Component Top Component Bottom Component 19 20 Split Panes The orientation of the split pane is set using the HORIZONTAL_SPLIT or VERTICAL_SPLIT constants The divider bar can be set so that it can be fully expanded with one click of the mouse The components can be continuously adjusted as the divider bar is moved, or wait until it stops moving Split panes can be nested EVENTS 21 22 Events An event is an object that represents some activity to which we may want to respond For example, we may want our program to perform some action when the following occurs: the mouse is moved the mouse is dragged a mouse button is clicked a graphical button is clicked a keyboard key is pressed a timer expires Events often correspond to user actions, but not always Events and Listeners The Java standard class library contains several classes that represent typical events Components, such as a graphical button, generate (or fire) an event when it occurs A listener object "waits" for an event to occur and responds accordingly We can design listener objects to take whatever actions are appropriate when an event occurs 23 24 4

Events and Listeners Event Mouse Events Events related to the mouse are separated into mouse events and mouse motion events Component A component object may generate an event Listener A corresponding listener object is designed to respond to the event When the event occurs, the component calls the appropriate method of the listener, passing an object that describes the event Mouse Events: mouse pressed mouse released mouse clicked mouse entered mouse exited the mouse button is pressed down the mouse button is released the mouse button is pressed down and released without moving the mouse in between the mouse pointer is moved onto (over) a component the mouse pointer is moved off of a component 25 26 Mouse Events Mouse Motion Events: mouse moved mouse dragged the mouse is moved the mouse is moved while the mouse button is pressed down Mouse Events For a given program, we may only care about one or two mouse events To satisfy the implementation of a listener interface, empty methods must be provided for unused events Listeners for mouse events are created using the MouseListener and MouseMotionListener interfaces A MouseEvent object is passed to the appropriate method when a mouse event occurs 27 28 Mouse Events Rubberbanding is the visual effect in which a shape is "stretched" as it is drawn using the mouse Key Events A key event is generated when the user types on the keyboard key pressed key released key typed a key on the keyboard is pressed down a key on the keyboard is released a key on the keyboard is pressed down and released Listeners for key events are created by implementing the KeyListener interface A KeyEvent object is passed to the appropriate method when a key event occurs 29 30 5

Key Events The component that generates a key event is the one that has the current keyboard focus Constants in the KeyEvent class can be used to determine which key was pressed LAYOUT 31 32 Layout Managers A layout manager is an object that determines the way that components are arranged in a container There are several predefined layout managers defined in the Java standard class library: Layout Managers Every container has a default layout manager, but we can explicitly set the layout manager as well Each layout manager has its own particular rules governing how the components will be arranged Flow Layout Border Layout Card Layout Grid Layout GridBag Layout Box Layout Overlay Layout Defined in the AWT Defined in Swing Some layout managers pay attention to a component's preferred size or alignment, while others do not A layout manager attempts to adjust the layout as components are added and as containers are resized 33 34 Layout Managers We can use the setlayout method of a container to change its layout manager JPanel panel = new JPanel(); panel.setlayout(new BorderLayout()); Flow Layout Flow layout puts as many components as possible on a row, then moves to the next row Rows are created as needed to accommodate all of the components Components are displayed in the order they are added to the container Each row of components is centered horizontally in the window by default, but could also be aligned left or right Also, the horizontal and vertical gaps between the components can be explicitly set 35 36 6

Border Layout A border layout defines five areas into which components can be added Border Layout Each area displays one component (which could be a container such as a JPanel) North Each of the four outer areas enlarges as needed to accommodate the component added to it West Center East If nothing is added to the outer areas, they take up no space and other areas expand to fill the void The center area expands to fill space as needed South 37 38 Grid Layout A grid layout presents a container s components in a rectangular grid of rows and columns One component is placed in each cell of the grid, and all cells have the same size As components are added to the container, they fill the grid from left-to-right and top-to-bottom (by default) The size of each cell is determined by the overall size of the container Box Layout A box layout organizes components horizontally (in one row) or vertically (in one column) Components are placed top-to-bottom or left-to-right in the order in which they are added to the container By combining multiple containers using box layout, many different configurations can be created Multiple containers with box layouts are often preferred to one container that uses the more complicated gridbag layout manager 39 40 Box Layout Invisible components can be added to a box layout container to take up space between components Rigid areas have a fixed size Glue specifies where excess space should go A rigid area is created using the createrigidarea method of the Box class Glue is created using the createhorizontalglue or createverticalglue methods Borders A border can be put around any Swing component to define how the edges of the component should be drawn Borders can be used effectively to group components visually The BorderFactory class contains several static methods for creating border objects A border is applied to a component using the setborder method 41 42 7

Borders An empty border buffers the space around the edge of a component otherwise has no visual effect A line border surrounds the component with a simple line the line's color and thickness can be specified An etched border creates the effect of an etched groove around a component uses colors for the highlight and shadow Borders A bevel border can be raised or lowered uses colors for the outer and inner highlights and shadows A titled border places a title on or around the border the title can be oriented in many ways A matte border specifies the sizes of the top, left, bottom, and right edges of the border separately uses either a solid color or an image 43 44 Borders A compound border is a combination of two borders one or both of the borders can be a compound border GUI DEVELOPMENT 45 46 GUI Development Generally we use components and events that are predefined by classes in the Java class library Therefore, to create a Java program that uses a GUI we must: instantiate and set up the necessary components implement listener classes for any events we care about establish the relationship between listeners and components that generate the corresponding events Let's now explore some new components and see how this all comes together GUI Design Principles We must remember that the goal of software is to help the user solve the problem To that end, the GUI designer should: Know the user Prevent user errors Optimize user abilities Be consistent Let's discuss each of these in more detail 47 48 8

Know the User Knowing the user implies an understanding of: the user's true needs the user's common activities the user's level of expertise in the problem domain and in computer processing We should also realize these issues may differ for different users Remember, to the user, the interface is the program Prevent User Errors Whenever possible, we should design user interfaces that minimize possible user mistakes We should choose the best GUI components for each task For example, in a situation where there are only a few valid options, using a menu or radio buttons would be better than an open text field Error messages should guide the user appropriately 49 50 Optimize User Abilities Not all users are alike some may be more familiar with the system than others Knowledgeable users are sometimes called power users We should provide multiple ways to accomplish a task whenever reasonable "wizards" to walk a user through a process short cuts for power users Help facilities should be available but not intrusive Be Consistent Consistency is important users get used to things appearing and working in certain ways Colors should be used consistently to indicate similar types of information or processing Screen layout should be consistent from one part of a system to another For example, error messages should appear in consistent locations 51 52 The Conditional Operator Java has a conditional operator that uses a boolean condition to determine which of two expressions is evaluated Its syntax is: condition? expression1 : expression2 BONUS COOL OPERATOR If the condition is true, expression1 is evaluated; if it is false, expression2 is evaluated The value of the entire conditional operator is the value of the selected expression 53 54 9

The Conditional Operator The conditional operator is similar to an if-else statement, except that it is an expression that returns a value For example: larger = ((num1 > num2)? num1 : num2); If num1 is greater than num2, then num1 is assigned to larger; otherwise, num2 is assigned to larger The conditional operator is ternary because it requires three operands The Conditional Operator Another example: System.out.println ("Your change is " + count + ((count == 1)? "Dime" : "Dimes")); If count equals 1, then "Dime" is printed If count is anything other than 1, then "Dimes" is printed 55 56 10