Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.

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
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
GUIs Part 4 CS221 – 4/17/09. Professional Assignments Assignment #2 – Download and install Visual Studio 2008 Professional Trial Software –
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
CPSC150 JavaLynn Lambert CPSC150 Week 12 InheritanceInterfaces.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
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: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
CS Lecture 01 Frames and Components and events Lynda Thomas
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
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.
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.
Layout Managers Arranges and lays out the GUI components on a container.
ITEC 109 Lecture 27 GUI. GUIs Review Sounds –Arrays hold sample values –Creating a keyboard –Sound effects Homework 3 –The big two –Due after break –Lab.
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.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Review_6 AWT, Swing, ActionListener, and Graphics.
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.
Graphical User Interfaces Tonga Institute of Higher Education.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
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.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUIs and Events Rick Mercer.
“Form Ever Follows Function” Louis Henri Sullivan
Java GUI.
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Timer class and inner classes
Containers and Components
Creating Graphical User Interfaces
Graphical User Interface
Presentation transcript:

Event Driven Programming and GUIs Part 3 CS221 – 4/15/09

Professional Assignments Assignment #2 – Download and install Visual Studio 2008 Professional Trial Software – ID=83c3a1ec-ed72-4a db0192b&displaylang=en ID=83c3a1ec-ed72-4a db0192b&displaylang=en Assignment #3 – Implement any one of your sorting algorithms using C# instead of Java To get credit for both assignments: – Send me your working C# project in – If it compiles and works you’ll get full credit

GUI Programming - Key Points The point is to make the application you are creating easier to use Don’t think of the UI as the solution, but as the human interface to the solution you are providing You don’t have to create from scratch, Java gives you many controls to reuse

GUI Programming is Different To create a simple UI is relatively easy To create a good UI requires: – Programming skills – Layout skills – Human/Computer interface skills There are not many people who can do it all! Good GUI programming is a matter of practice and using good user interface principles

Imitation is a good Approach How to get better: – Study programs that do a good job with UI. How do they do it? – Read books on UI programming and human/computer interface techniques – Review the built in class libraries and controls. Understand what’s available vs. what you may need to create from scratch – Practice! Whenever you can, build a GUI for your programs

JFrame One way to create a GUI is to extend JFrame – ng/JFrame.html ng/JFrame.html JFrame gives you: A visible UI window in which you can place buttons, form fields, menus, and other UI elements. Handles events for close, minimize and maximize. Resizes for you. That’s pretty much it!

Extending JFrame How do we make a simple GUI extending JFrame? – Create a package – Create a class, extends JFrame – Import javax.swing.* – Create a constructor setVisible(true); – Create main and call the constructor

What do we get? Kinda lame so far… But it does draw itself, minimize, maximize, resize and close Not bad for so little effort actually!

Hello World Let’s turn it into a Hello World application – That’s exciting right? What do we need to do? – Add some text – Make sure it actually closes on exit…

Hello World To add text: – Add a JLabel field – Set the value of the label field – Add it to the frame Ensure the application closes: – setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

What do we get? Still boring… let’s add some interactivity How about we allow the user to set the text?

Hello World Revised We need to: – Add an input field JTextField – Give the user a button to click JButton – Update the label with the contents of the input field when the button is clicked

Layout After we add the controls, they aren’t visible on the frame… We need to arrange the controls in the frame so they aren’t on top of each other We need to make sure the frame is big enough to display everything

Layout In order to layout the UI controls on the frame we need to use a layout manager A layout manager controls the size and location of the UI elements based on – Layout manager rules – Parameters you pass in governing those rules A frame can contain – Controls, such as buttons and fields – Containers, that contain other controls Each container can have its own layout manager

Layout Managers Border Layout – Lay out controls in a header, footer, margins or center – Assign each control to North, South, East and West Flow Layout – Flows the controls onto the screen from left to right – Chooses number of rows based on width of panel, number of controls and the size of each Card Layout – Allows you to show some controls and keep others hidden – You can toggle what you display based on user input Grid Layout – Lay out controls in a grid format with a specified number of rows and colums GridBag Layout – Like grid layout but it gives you more options to change the size of elements, etc

Border Layout

Flow Layout

Card Layout

Grid Layout

Gridbag Layout

Back to our Layout To layout the UI elements on the frame: – Set layout on the frame getContentPane.setLayout() – Pass in a layout manager GridLayout(3, 1), will create a grid layout of 3 rows and 1 column To make sure the window is big enough – Call pack() to resize the window

Handling the Button Click To handle the click we: – Create an action listener class public class AL implements ActionListener – Add the action listener to the button Button.addActionListener() – Handle the click in our action listener class public void actionPerformed(ActionEvent e) If e.getActionCommand()==“…”

Let’s look at the lab code Why do we use panels? Why are we setting the size of some controls but not others? Notice how we handle multiple event types in a single listener

Improving FlightsWindow How would we: – Add a button that when pressed would add a flight? – Sort the flight list when the user clicks the sort button? – Add another row of UI elements for new functionality?