עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces (Part IV)
Advertisements

CS18000: Problem Solving and Object-Oriented Programming.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
© 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;
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Lecture 19 Graphics User Interfaces (GUIs)
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Graphical User Interfaces
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
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.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
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.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
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.
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.
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.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
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.
Graphics in Java Dr. Andrew Wallace PhD BEng(hons) EurIng
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
1 Lecture 25 Listening to buttons and mice Quotes by Tony Hoare There are two ways of constructing a software design: (1) make it so simple that there.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
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.
Graphical User Interface (GUI)
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
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.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
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:
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
Graphical User Interfaces
CompSci 230 S Programming Techniques
Graphical User Interfaces
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Lecture 27 Creating Custom GUIs
Graphical user interface with Swing by jose maria gonzalez pinto
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
GUIS: Graphical User Interfaces
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

עקרונות תכנות מונחה עצמים תרגול 4 - GUI

Outline  Introduction to GUI  Swing  Basic components  Event handling

CLI VS GUI

GUI applicationsCLI applications Reactive, interactiveTransactionalProgram Flow AsynchronousTextualInput GraphicalTextualOutput YesNoMultiTasking Remains activeExitAfter Execution

GUI programming challenges  Usability  Attractiveness  Multithreading  Incremental execution  Testing And The algorithm

Elements of GUI programming  Visualization of data: Model Widgets  Visualization of commands: Buttons, pop-ups  Responding of commands: Event handling  Graphical layout: Containers, panels, layout managers  Reactiveness: Using threads safely  Separation of concerns: OOP, good design, MVC

SWING  Java’s GUI framework (Java Foundation Classes)  Extension of an older framework – AWT  Provides:  Frames, dialogs, panels, toolbars..  Graphical control widgets, from buttons to tree controls  Thread-safe events dispatching  Rich-text editing  Pluggable Look-n’-feel(PLAF)  Other goodies (accessibility, text parsing..) Official Swing Tutorial

Swing Elements  Components - Graphical widgets  Containers -Components that contain sub-components  Layout - Arrangement of components inside containers  Events - Interaction with the user  Event listeners – Objects that responds to events asynchronously  Event dispatcher – A special thread that dispatches events and re-paints the GUI  Models - Objects that hold the data and handle events for components

Swing components - hierarchy  Each component is a Java class with a fairly extensive inheritency hierarchy: Object Component Container JComponent JPanel Window Frame JFrame javax.swing java.awt java.lang

Using Swing Components  Very simple, just create object from appropriate class – examples:  JButton but = new JButton();  JTextField text = new JTextField();  JTextArea text = new JTextArea();  JLabel lab = new JLabel();  Many more classes. Don’t need to know every one to get started.

Adding components  Once a component is created, it can be added to a container by calling the container’s add method: Container cp = getContentPane(); cp.add(new JButton(“cancel”)); cp.add(new JButton(“go”)); How these are laid out is determined by the layout manager (next lesson).

JFrame  A frame contains:  Title bar  Menu bar  Content pane  The content pane is a container that represent the main area of the frame.

Example 1 Hello.jar

Jhello World! public class Hello extends JFrame { public Hello(){ super("Title Bar"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton click = new JButton("This is a button"); Container cp = getContentPane(); cp.add(click); pack(); setVisible(true); } public static void main(String args[]){ Hello frame = new Hello(); }

Event handling

Events and Listeners in Swing  Input from the user in Swing is represented by Events  Events come from a Source: a component or a model  Related kinds of inputs are grouped into Listener interfaces  A listener interface contains several messages that passes events to the receiver  Each message represent a different scenario of receiving this input from the user  An Event Listener is an object whose class implements a listener interface, so it can receive events  Attaching a listener of type X to a source is done by the call : source.addXListener(listener)

Example 2 Jumper.jar

Add Action Listener public class Jumper extends JFrame implements ActionListener{ public Jumper(){ super(“Jumper"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton btn = new JButton(“Catch me!"); btn.addActionListener(this); // source.addXListener(listener) Container cp = getContentPane(); cp.add(btn); pack(); setVisible(true); public void actionPerformed(ActionEvent e) { Random rand = new Random(); int x = rand.nextInt(300); int y = rand.nextInt(300); setLocation(x, y); }

The Event object  All event classes in Swing extends the EventObject class.  This class contains the method getSource( ) that returns the object which was the source of the event  Events sub-classes usually also contain data related to specific event.

Event Listener Example: Action Listener

Event Listener Example: Mouse Listener

Listening to an event

Example 3 Convertor.jar

public class Convertor extends JFrame implements ActionListener { private static double DOLLAR_RATE = 3.943; private JButton convertButton; private JButton resetButton; private JLabel resultLabel; private JTextField valueInput; private double value; public Convertor(){ super("Convertor"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); value = 0; // Initiate label and text field. valueInput = new JTextField(); valueInput.setColumns(10); valueInput.setText( value + ""); resultLabel = new JLabel(value + " $ is "+ convert() + " Shekel"); // Create buttons convertButton = new JButton("Convert"); resetButton = new JButton("Reset");

// Add action listeners convertButton.addActionListener(this); resetButton.addActionListener(this); // Add all objects to Content Pane getContentPane().setLayout(new FlowLayout()); getContentPane().add(resetButton); getContentPane().add(valueInput); getContentPane().add(convertButton); getContentPane().add(resultLabel); pack(); setVisible(true); {

public void actionPerformed(ActionEvent e) { if (e.getSource().equals(convertButton)){ updateValue(); } if (e.getSource().equals(resetButton)){ setValue(0); } updateView(); { private void setValue(double v) { this.value = v; } private double convert() { return value*DOLLAR_RATE; { private void updateValue(){ this.value = Double.parseDouble(valueInput.getText()); } private void updateView(){ resultLabel.setText(value +" $ is " + convert() + " Shekel " ); valueInput.setText(value +""); pack(); {