Packages, Javadoc, Testing, GUIs Chapter 5 Chapter 6 Class notes.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
Graphic User Interfaces Layout Managers Event Handling.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
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.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
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.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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 
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Java Programming Chapter 10 Graphical User Interfaces.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
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.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
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.
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.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
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.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Review_6 AWT, Swing, ActionListener, and Graphics.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
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.
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.
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.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
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:
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.
©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
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
GUIS: Graphical User Interfaces
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
GUI building with the AWT
Graphical User Interface
Presentation transcript:

Packages, Javadoc, Testing, GUIs Chapter 5 Chapter 6 Class notes

JavaDoc Documentation View in BlueJ through Interface View on web through API documentation (application programming interface): – Write your own /** Here is what my Class does * can go over multiple lines */

JavaDoc: write your own Write in html (text IS html) /** Here is my important part */ Use your date or name description For a full list of tags, see:

Quiz 6 Add javadoc to your mastermind program. Add an explanation for each method when appropriate. Add a comment at the beginning of the program. and put the date. View your javadoc through the BlueJ interface. Print out your source code with the javadoc comments.

Using the API Use API whenever you want to do something, but don’t know how Example: How do I use Random numbers? Look at API for Random Or, Look at documentation for classes: – –(note: search isn’t always that helpful) Or, look at: –This has a more graphical representation than the API; most people use the API –Look at top description and each method summary

Chapter 6 Testing

Testing/Debugging Syntax errors: Initially fixing syntax errors the hard part. After that, fixing logic errors: Testing: Ensuring that your code works Debugging: finding where code is incorrect

Levels of Testing Unit testing Application testing Always start at the lowest level Test after every method is written Use these tests throughout. after each method, run all of the old tests

Types of Testing (in BlueJ) Inspection System.out.println Regression Testing –Create tests and rerun them for each new development To do that, Test Harness/Test Rig –Class whose sole purpose is to test another class To do that, Automatic Testing using junit To do that, Record Tests

Testing, version 1 (textbook: 6.4) Write a method that will test different parts of a class. Example: day (simple blackberry) –in chapter 6, projects –diary-testing –OneHourTests class

Testing, version 2 Downsides of version 1 –results have to be checked by eye and checker has to know what results should be –automate process diary-testing-junit-v1 –green boxes are tests classes –supports unit testing, regression testing –part of BlueJ (not Java, but in the spirit of Java) –Run tests button Test classes contain –source code to test class, AND –assert.equals( ) to test whether test was successful

Testing, writing our own To Test findSpace method Record a test –Start Recording –Perform the test on the object bench –End Recording

Start Recording Show unit testing –Under tools, preferences, miscellaneous, check "Show unit testing tools" checkbox Compile all classes Create Test Class (if one is not there) –Right click on class –Choose "Create Test Class" Create Test Method –Right click on test class, chose "Create Test Class Method" –Put a name in (findSpace9)

Perform a Test on the Object Bench Red Circle means recording Create objects –Create a day object –Create an Appointment objects with a one- hour duration –Call the findSpace method in Day Check that the test is correct –right answer –assert box checked

End Recording End recording Examine source code in test class

Quiz 7 Write a test unit class for Mastermind and write two unit tests for the same method (test it using two different inputs). If you have no methods with parameters, then write two test for two different methods.

Graphical User Interfaces class notes (in book chapter 11)

GUIs: using Sun’s javadoc Graphical User Interfaces implemented in Java in javax.Swing and AWT packages (Swing is newer, better, but some stuff only in AWT) For general information, click on library Do tutorial Note ‘x’

Designing Program with Graphical User Interfaces GUI Programs have two parts/classes: –the User Interface –the engine In Mastermind –User interface is board and methods that use can call –The engine is what does the calculations: calculates black and whites/win/lose/generates answer, etc.

Implementating GUIs: GUI parts Components –buttons, menus, textfields, etc. Layout –Where on the screen to put the components. use LayoutManager Event Handling –instead of executing methods when called (by BlueJ or in code), execute methods when an event occurs (e.g., mouse click on a button)

Swing Java has AWT (old, Abstract Window Toolkit) and Swing (based on AWT) Swing prefixes components with J –e.g., Button is from AWT, jButton is Swing –We’ll use Swing To use Swing: import java.awt.*; // * means all packages in import java.awt.event.*; import javax.swing.*; // note ‘x’

Swing Specifics Everything in a container Two top level containers: JFrame, JApplet Other containers can be embedded within each other: Jbutton on JPanel on another JPanel, etc. Lots of parts to a JFrame, mostly ContentPane –content panes can hold any number of components –(4 components in windowFrame in lab3) –contentPane (only one) vs JPanel (can have lots)

Building a Window Everything is in a JFrame JFrame has –Title bar –Menu bar (optional) –Content Pane title bar menu bar content pane Frame (whole window)

Code for a Frame To build a JFrame, do 4 things (From 150 lab3 GuiInterface.java) 1. Create new JFrame and put a title in private JFrame windowFrame = new JFrame(“Word Jumble 1.0”); 2. Tie parts to JFrame (component, menubar, etc) // if gameWordPanel already exists – type JPanel Container cp = WindowFrame.getContentPane( ); cp.add(gameWordPanel); OR windowFrame.getContentPane( ).add(gameWordPanel); OR (in Java5): windowFrame.add(gameWordPanel);

Code for a JFrame 3. pack frame. arrange components windowFrame.pack( ) 4. make the frame visible (can also make it invisible, as in quitWord) windowFrame.setVisible(true);

Some Components JButton JPanel JLabel Lots, look at: /swing/package-summary.html

title bar minimize maximize close come for free component (JPanel) { Also in lab3, what to do on close (click on red X): windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Layout managers Manage limited space for components. –FlowLayout, BorderLayout, GridLayout, BoxLayout, GridBagLayout (most flexible, hardest) Manage Container objects, e.g. a content pane. Each imposes its own style.

FlowLayout

BorderLayout default for top level containers (JFrame)

GridLayout

BoxLayout Note: no component resizing.

Layout Manager Lab 3 4 JPanels gameWordPanel, gameGuessPanel, gameButtonPanel, gameStatusPanel

Layout Managers – Lab 3 Container contentPane = windowFrame.getContentPane ( ); contentPane.setLayout(new GridLayout(4, 1)); OR windowFrame.getContentPane().setLayout(new GridLayout(4,1)); OR windowFrame.setLayout(new GridLayout(4, 1)); // add panels in order windowFrame.getContentPane().add(gameWordPanel); windowFrame.getContentPane().add(gameGuessPanel); windowFrame.getContentPane().add(gameButtonPanel); windowFrame.getContentPane().add(gameStatusPanel);

Events We can now put items on a JFrame and arrange them. We need to be able to do something with them. Action Events –mouse click, mouse over, window action, menu choice, etc. General, ActionListener –Something happened –in java.awt.event –( vent/package-summary.html) vent/package-summary.html

“Check” button in Lab 3: add ActionListener event //Associate actionlistener with button and write actionPerformed b = new JButton("Check"); b.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); } );

Action Listeners: a different way b = new JButton("Check"); b.addActionListener(new checkAction( )); private class checkAction implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); }

Create a Frame with Questions //Create the frame: –JFrame frame = new JFrame( ); // layout is BorderLayout //Create the button panel –JPanel buttonPanel = new JPanel(); // layout is flowLayout //Create the buttons: –JButton b1 = new JButton("yes"); –JButton b2 = new JButton("no"); –JButton b3 = new JButton("maybe"); //Create the Question Label: –JLabel question = new JLabel( );

Create a Frame with Questions part 2 // Add the buttons to the Panel –buttonPanel.add(b1); –buttonPanel.add(b2); –buttonPanel.add(b3); // Add the question and panel to the frame –frame.add(question, BorderLayout.CENTER); –frame.add(buttonPanel, BorderLayout.SOUTH); // Pack the frame and display it –frame.setSize(200, 100); // instead of frame.pack( ); –frame.setVisible(true); –frame.setDefaultCloseOperation(JFrame. EXIT_ON_CL OSE );

No action listener No action listeners yet