© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.

Slides:



Advertisements
Similar presentations
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.
Advertisements

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
© by Pearson Education, Inc. All Rights Reserved. continued …
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 Introduction to the Visual Studio.NET IDE Powerpoint slides modified from Deitel & Deitel.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Introducing Web Controls Outline 29.1 Analyzing the.
IE 411/511: Visual Programming for Industrial Applications
XP Mohammad Moizuddin Creating Web Pages with HTML Tutorial 1 1 New Perspectives on Creating Web Pages With HTML Tutorial 1: Developing a Basic Web Page.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 2 Welcome Application Introducing the Visual Basic 2008 Express Edition IDE.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 3.1 Test-Driving the Welcome Application 3.2.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
Chapter 2 – Introduction to the Visual Studio .NET IDE
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.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Java Application and Applet
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 4 Designing the Inventory Application Introducing TextBox es and Button s.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
 You won’t write a single line of program code.  Instead, you’ll use visual programming techniques.  Visual Studio processes your actions (such as mouse.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Chapter 13: Advanced GUIs and Graphics
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Advanced GUIs and Graphics
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical User Interface Programming Outline 2.1 Test-Driving the Welcome Application 2.2 Compiling and Running the Template Welcome Application 2.3 Constructing the Welcome Application 2.4 Syntax Errors 2.5 Wrap-Up

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Set the text in the JFrame ’s title bar. –Change the JFrame ’s background color. –Place a JLabel component on the JFrame. –Display text in a JLabel component. –Display an image in a JLabel component. –Execute an application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Welcome Application (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Welcome Application (Cont.) Figure 2.1 Locating the completed Welcome application. Locate the application –Locate the directory

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Welcome Application (Cont.) Figure 2.2 Running the completed Welcome application. Execute the application –Type java Welcome

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Welcome Application (Cont.) Figure 2.3 Welcome application executing. close button

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Compiling and Running the Template Welcome Application Figure 2.4 Locating the template Welcome application. Change directoriesDisplay a directory listing No.class file

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Compiling and Running the Template Welcome Application (Cont.) Compiling –Use the javac command to compile Type javac Welcome.java to compile the Welcome application and create a.class file.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Compiling and Running the Template Welcome Application (Cont.) Figure 2.5 Compiling the template Welcome application. Compile Welcome.java Display a directory listing.class file was created

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Compiling and Running the Template Welcome Application (Cont.) Figure 2.6 Running the template Welcome application. Running the application –Run the application by typing java Welcome

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application Figure 2.7 Welcome application’s Java source code in an editor window.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.8 Setting the text in the JFrame ’s title bar. Set title bar text Title bar in a JFrame –Use book-title capitalization Java statement –End with a semicolon ( ; ) –Arguments –Case sensitive String literal

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.9 Compiling the updated Welcome application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Title bar Figure 2.10 JFrame after setting the title.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.11 The size property controls the width and height of the JFrame. Change the JFrame ’s width and height Setting the size of the JFrame –size property –Pixels (picture elements) –Set the width of the JFrame to 608 –Set the height of the JFrame to 413

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.12 Compiling the updated Welcome application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.13 JFrame after setting its title and size properties. Height (includes the title bar) Width

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Colors –Java’s pre-defined color constants –RGB values

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Content Pane –Visual area in which to place GUI components –background property

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.15 JFrame with highlighted content pane. The dashed box represents the area of the content pane

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.16 Setting the JFrame ’s background property to yellow. Change the background color background property –Set the background color of the content pane

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.17 Compiling the updated Welcome application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.18 JFrame after setting the title, size and background color

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.19 Setting the text property of the JLabel. Setting the text property text property –Set the text displayed in the JLabel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.20 Setting the location property of the JLabel. Set the location of the JLabel location property –Set the location of the JLabel –Location is measured from the upper-left corner of the JPanel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.21 Setting the size property of the JLabel. Set the size of the JLabel size property –Set the width and height of the JLabel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.22 Changing the JLabel ’s font size to 36 points. Setting the font of the JLabel font property –Set the font of the text in the JLabel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.23 Centering the JLabel ’s text. horizontalAlignment property –Set the horizontal alignment of the text in the JLabel Centering the text in the JLabel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.24 Compiling the updated Welcome application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.25 Welcome application after modifying the text JLabel. JLabel component with text ( 0, 0 ) coordinate of content pane

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) icon property –Set the image displayed in the JLabel –Java assumes the image is in the same directory unless you specify a path –Java can display many image types including PNG (Portable Network Graphics) GIF (Graphics Interchange Format) JPEG (Joint Photographic Experts Group) BMP (Windows Bitmap).

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.26 Setting the icon property to display an image on a JLabel. Displaying an image

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.27 Using the bounds property to set the size and location. Set the bounds (size and location) of the JLabel bounds property –Set the location, width and height of the JLabel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.28 Setting the horizontalAlignment property of the JLabel. Centering the image

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.29 Compiling the updated Welcome application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Welcome Application (Cont.) Figure 2.30 Running the completed Welcome application. JLabel component with a centered image

 2004 Prentice Hall, Inc. All rights reserved. Outline 37 Welcome.java (1 of 3) 1 // Tutorial 2: Welcome.java 2 // This application welcomes the user to Java programming. 3 import java.awt.*; 4 import javax.swing.*; 5 6 public class Welcome extends JFrame 7 { 8 private JLabel textJLabel; // label that displays text 9 private JLabel pictureJLabel; // label that displays an image // default constructor 12 public Welcome() 13 { 14 createUserInterface(); 15 } // create and position GUI components; register event handlers 18 private void createUserInterface() 19 { 20 // get content pane and set layout to null 21 Container contentPane = getContentPane(); 22 contentPane.setBackground( Color.YELLOW ); 23 contentPane.setLayout( null ); 24 Set the background color of the content pane to yellow

 2004 Prentice Hall, Inc. All rights reserved. Outline 38 Welcome.java (2 of 3) 25 // set up textLabel 26 textJLabel = new JLabel(); 27 textJLabel.setText( "Welcome to Java Programming!" ); 28 textJLabel.setLocation( 35, 0 ); 29 textJLabel.setSize( 550, 88 ); 30 textJLabel.setFont( new Font( "SanSerif", Font.PLAIN, 36 ) ); 31 textJLabel.setHorizontalAlignment( JLabel.CENTER ); 32 contentPane.add( textJLabel ); // set up pictureLabel 35 pictureJLabel = new JLabel(); 36 pictureJLabel.setIcon( new ImageIcon( "bug.png" ) ); 37 pictureJLabel.setBounds( 54, 120, 500, 250 ); 38 pictureJLabel.setHorizontalAlignment( JLabel.CENTER ); 39 contentPane.add( pictureJLabel ); // set properties of window 42 setTitle( "Welcome" ); // set JFrame's title bar string 43 setSize( 608, 413 ); // set width and height of JFrame 44 setVisible( true ); // display JFrame on screen } // end method createUserInterface Set the JFrame ’s title and size Customize pictureJLabel ’s properties Customize textJLabel ’s properties

 2004 Prentice Hall, Inc. All rights reserved. Outline 39 Welcome.java (3 of 3) // main method 49 public static void main( String args[] ) 50 { 51 Welcome application = new Welcome(); 52 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class Welcome

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Syntax Errors Debugging Errors –Syntax errors (compilation errors, compile-time errors) Application will not compile –Logic errors Application will compile, but may produce erroneous results Debugger –Use the debugger to fix syntax errors

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Syntax Errors Figure 2.32 Command Prompt window listing syntax errors. Missing semicolon at the end of this line

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Syntax Errors Figure 2.33 Introducing two syntax errors into your code. Two syntax errors Creating syntax errors –Add the letter “s” to the end of textJLabel in line 27 –Delete the right parenthesis at the end of the statement in line 28

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Syntax Errors Figure 2.34 Two error messages about syntax errors generated by the compiler. Number of errors Line of code Explanation of the error File name and line number

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Syntax Errors Fixing the errors –Go back to your code and fix the syntax errors indicated in the command prompt.