Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week 18 2.0.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces (Part IV)
Advertisements

Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 : Event-Driven Programming and GUI Objects.
Chapter 7 Event-Driven Programming and Basic GUI Objects.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling Objects First with Java - A Practical.
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.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
CPSC150 JavaLynn Lambert CPSC150 Week 12 InheritanceInterfaces.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
1 Object-Oriented Programming (Java), Unit 22 Kirk Scott.
F27SB2 Software Development 2 Lecture 9: Java GUIs 6.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Interfaces & Polymorphism part 2:
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.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
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.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
SD2054 Software Development. By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces 2.0.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling Objects First with Java - A Practical.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 COS 260 DAY 24 Tony Gauvin. 2 Agenda Questions? 10 th Mini Quiz Corrected –So-so results Capstone progress report over due Course evaluations Assignment.
Advanced Java Class Events. change in state initiated by system or user java.util.EventObject java.awt.event java.swing.event.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
Introduction to GUI in 1 Graphical User Interface Nouf Almunyif.
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:
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling © 2017 Pearson Education, Inc. Hoboken,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Multiple buttons and action calls
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
A First Look at GUI Applications
A Quick Java Swing Tutorial
Building Graphical User Interfaces
COS 260 DAY 23 Tony Gauvin.
CSE 114 – Computer Science I Event Programming
Ellen Walker Hiram College
Introduction to GUI in Graphical User Interface Nouf Almunyif.
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Graphical User Interface
1 Graphical User Interfaces
A Quick Java Swing Tutorial
Advanced Programming in Java
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week

2 Overview Constructing GUIs Components Event handling

3 AWT and Swing

4 Creating a frame import java.awt.*; import java.awt.event.*; import javax.swing.*; // comment omitted. public class ImageViewer implements ActionListener { private JFrame frame; /** * Create an ImageViewer and show it on screen. */ public ImageViewer() { makeFrame(); } // rest of class omitted. }

5 Elements of a frame Title Menu bar Content pane Window controls

6 /** * Create the Swing frame and its content. */ private void makeFrame() { frame = new JFrame("ImageViewer"); makeMenuBar(frame); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("I am a label. I can display some text."); contentPane.add(label); frame.pack(); frame.setVisible(true); } The content pane

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 private void makeMenuBar(JFrame frame) { JMenuBar menubar = new JMenuBar(); frame.setJMenuBar(menubar); JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); JMenuItem openItem = new JMenuItem("Open"); openItem.addActionListener(this); fileMenu.add(openItem); JMenuItem quitItem = new JMenuItem("Quit"); quitItem.addActionListener(this); 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 Centralised 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 public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if(command.equals("Open")) { open(); } if (command.equals("Quit")) { quit(); } public void open() { System.out.println("Unable to open a file."); } public void quit() { System.out.println("Thank you for using ImageViewer."); System.exit(0); }

12 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.

13 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.

14 A message dialog private void showAbout() { JOptionPane.showMessageDialog(frame, "ImageViewer\n" + "Version 0.2", "About ImageViewer", JOptionPane.INFORMATION_MESSAGE); }

15 Dialog message types ERROR_MESSAGE INFORMATION_MESSAGE WARNING_MESSAGE QUESTION_MESSAGE PLAIN_MESSAGE

16 Review Pre-defined components simplify creation of sophisticated GUIs. Many components recognise user interactions with them. Reactive components deliver events to listeners.