CompSci 415.1 Event Handling. CompSci 415.2 Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.

Slides:



Advertisements
Similar presentations
Drawing in a frame – Java GUI
Advertisements

GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 16 Event-Driven Programming Of all men’s miseries.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
(c) 2008 E.S.Boese All Rights Reserved. Threads and Media Chapter 8 - Lecture Slides 1.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
GUI programming Graphical user interface-based programming.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
GUIs for Video Games. The Plan ● Layout suggestions ● Observer/Observable ● Steps to writing Observer/Observables ● Sample Code ● Practice.
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.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Layout Managers Arranges and lays out the GUI components on a container.
Event Driven Programming. Event-driven Programming In the early days of computing communication with the outside world was accomplished using a technique.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
Class Class 20 Objectives Use JCheckbox, JRadioButton, and a JComboBox in a UID.
Java Applet Basics (2). The Body Mass Index Calculator.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Ch15 簡單計算機 物件導向系統實務. Ch22_Main.java import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch22_Main { public static void main(String.
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)
Event Handling CS 21a: Introduction to Computing I First Semester,
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
Design Applet based java program to find the area and perimeter of a rectangle given its length and width. Java GUI.
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.
CompSci 230 S Programming Techniques
Java Applet What is a Java Applet? How is applet compiled?
Graphical User Interfaces -- GUIs
Web Design & Development Lecture 11
A First Look at GUI Applications
GUIs for Video Games.
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Graphical user interface-based programming
PC02 Consolidation %WITTY_QUOTE%. PC02 Consolidation %WITTY_QUOTE%
Graphical User Interfaces in Java Event-driven programming
Computer Science 209 Images and GUIs.
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

CompSci Event Handling

CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples  Practice

CompSci Event Handling Sequential (Single Thread) Model Program Start Program End

CompSci Event Handling Event Model AWT Event Loop Program Thread

CompSci Event Handling Event Model Program Thread

CompSci Event Handling Event Model AWT Event Loop

CompSci Event Handling Making the GUI Interactive 1) import java.awt.event.* 2) implements ActionListener 3) write method public void actionPerformed(ActionEvent e) 4) call addActionListener(this) for all JButton s

CompSci Event Handling Examples AdderGUI.java GameShell.java

CompSci Event Handling Examples AdderGUI.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AdderGUI extends JApplet implements ActionListener

CompSci Event Handling Examples AdderGUI.java public void actionPerformed(ActionEvent ae) { String addend0Text=addend0.getText(); double addend0Number=Double.parseDouble(addend0Text); String addend1Text=addend1.getText(); double addend1Number=Double.parseDouble(addend1Text); double answer=addend0Number+addend1Number; sum.setText(""+answer); }

CompSci Event Handling Examples AdderGUI.java private void makeComponents() { frame=new JFrame("Game Shell"); addend0=new JTextField(8); addend1=new JTextField(8); sum=new JTextField(8); compute=new JButton("="); compute.addActionListener(this); plus=new JLabel("+"); plus.setHorizontalAlignment(SwingConstants.CENTER); }

CompSci Event Handling Examples GameShell.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GameShell extends JApplet implements ActionListener

CompSci Event Handling Examples GameShell.java public void actionPerformed(ActionEvent ae) { Object cause=ae.getSource(); if(cause==pause) { if(pause.getText().equals("Pause")) { pause.setText("Resume"); shell.setText("Paused"); } else { pause.setText("Pause"); shell.setText("Game Running"); } if(cause==reset) { pause.setText("Start"); shell.setText("Splash"); }

CompSci Event Handling Examples GameShell.java pause=new JButton("Start"); pause.addActionListener(this); reset=new JButton("Start New Game"); reset.addActionListener(this);

CompSci Event Handling Practice  Make a 2x2 tic-tac-toe board out of initially blank Jbuttons.  Make the JButton text change to X when the user clicks on it.  Make the JButton text change to X and O alternatively as the user clicks on the buttons. Hint: use a boolean instance variable.  Make the fonts larger, and maybe add images.  Look at Splash.java and SplashLoop.java in Splash.jar