Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Advertisements

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.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
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.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
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 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Java Programming Chapter 10 Graphical User Interfaces.
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.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Consolidation. Code making (i.e. making ciphers) is undertaken by a cryptographer whereas cryptanalysts try to break ciphers in order to gain intelligence.
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,
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.
The Java Inheritance Hierarchy CSIS 3701: Advanced Object Oriented Programming.
GUI programming Graphical user interface-based programming.
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.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
CS Lecture 01 Frames and Components and events Lynda Thomas
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
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.
Event Driven Programming. Event-driven Programming In the early days of computing communication with the outside world was accomplished using a technique.
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.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
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.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Graphical User Interfaces. Graphical input and output with JOptionPane.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Unit Testing Part 2: Drivers and Stubs
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
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 Layouts CSIS 3701: Advanced Object Oriented Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
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.
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.
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
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.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
GUIs & Event-Driven Programming Chapter 11 Review.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Modular Event Handling
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
GUI’s.
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Graphical user interface-based programming
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Java Visual Applications CSIS 3701: Advanced Object Oriented Programming

Visual Applications Exist in own window (JFrame ) main constructs an instance of the application –Constructs and adds own visual components –Displays itself on screen –Handles events JVM starts application by calling its main method main method main method constructs an instance of that class Instance of class OS calls code when visual components activated Constructor adds visual components to its surface

Visual Application Structure public class AppName extends JFrame … { declare visual components as member variables public AppName() { construct visual components add to surface of application … setSize(width, height); setVisible(true); } … public static void main(String[] args) { AppName a = new AppName(); } } 1) main called 2) Which in turn constructs an object of this class 3) Which constructs and adds visual components to itself, and makes itself visible

Visual Component Classes Objects in Java –Must construct and add to surface of application –Must import javax.swing.* package Basic types: –JButton –JTextField –JPanel (surface other components attached to for display)

Visual Component Classes JButton –A constructor: JButton(String label) label to display –Some methods: void setText(String label) change label void setEnabled(boolean b) can user press? (default: true)

Visual Component Classes JTextField –A constructor: JTextField(int width) width in columns –Some methods: void setText(String s) display this string String getText() get current value (what user entered) void setEditable(boolean b) can user type? (default: true)

Visual Component Classes JTextArea (multi-line text area) –A constructor: JTextArea(int rows, int cols) sets width and height –Some methods: void setText(String s) display this string String getText() get current value void append(String s) add to current text rather than replacing

Visual Component Classes JLabel (cannot be entered into by user) –A constructor: JLabel(String label) displays this –Some methods: void setText(String s)

Adding Components to Surface JPanel –A constructor: JPanel() Default constructor adds other components left-to-right across suface –Some methods: void add(Component c) Add this component to surface of panel

Adding Components to Surface ContentPane –Member variable representing visible surface of app –Accessed with getContentPan e method –Add JPanel to surface and add components to it Application ContentPane getContentPane() JPanel add

Adding Components to Surface // Create a new visual panel and add it to the // surface (the ContentPane) of the application. JPanel mainPanel = new JPanel(); getContentPane().add(mainPanel); // Add the button and textfield to // panel to make them visible. mainPanel.add(helloButton); mainPanel.add(responseField); Side point: Unlike other components, declared mainPanel as local variable since not used outside constructor (more efficient)

Event-driven Programming Code executed at startup to initialize –Code implemented in constructor Additional code executed on demand when user causes events (button press, etc.) –Usually done in actionPerformed method of the application public void actionPerformed(ActionEvent e) { responseField.setText("Welcome to CSIS 3701!"); }

Event-driven Programming Application registers as an “ ActionListener ” –Can handle “action events” created by components public class Main extends JFrame implements ActionListener { Application tells button to notify it when pressed –Adds itself as an ActionListener to the button helloButton.addActionListener(this); Reference to the application itself

Event-driven Programming Application Construct button Have it notify me about events by passing my own address User presses helloButton.addActionListener(this); Button notifies application actionPerformed(ActionEvent e) location 37A4 37A4

Clock Object as Member Variable ClockProject package Main.java visual application Stores current hour and minute Allows hour and minute to be set Increments to next second or minute Clock.java Clock.java business logic clock

Clock Object as Member Variable Main application contains a Clock object: –Stores as member variable –Constructs and initializes in constructor –Manipulates it in response to events public class Main extends JFrame implements ActionListener { private Clock clock; … public Main() { … clock = new Clock(); timeField.setText(clock.toString());

Clock Object as Member Variable public void actionPerformed(ActionEvent actionObject) { if (actionObject.getSource() == tickButton) { clock.nextMinute(); } if (actionObject.getSource() == setButton) { int h = Integer.parseInt(hourField.getText()); int m = Integer.parseInt(minuteField.getText()); clock.setHour(h); clock.setMinute(m); } String timeString = “” + clock.getHour() + ” : ” + clock.getMinute(); timeField.setText(timeString); }

Parsing Note that a JTextField contains a string Must parse to a numeric value before manipulating getText() String parsing numeric value for math Key: Each simple type has corresponding class int  Integer double  Double boolean  Boolean char  Char Built-in tools to manipulate simple type values

Parsing Parsing syntax: type = Classname.parseType(String); Examples: int h = Integer.parseInt(hourField.getText()); double area = Double.parseDouble(areaString);

Popups Static methods in JOptionPane class Simple message : JOptionPane.showMessageDialog( location, String); displayed Example: JOptionPane.showMessageDialog(null, "The radius is " + radiusString); Usual values: null  center of screen this  same location as app

Popups Prompting for values: String reply = JOptionPane. showInputDialog(location,String); Returns string entered by user Example: String areaString = JOptionPane.showInputDialog(null, "Enter the area of a circle"); Note: returns null if Cancel button pressed instead