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.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

Introduction to Java.
The Web Warrior Guide to Web Design Technologies
IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 1: Introduction
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 1 Introducing Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Getting Started with Java
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
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.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
A First Program Using C#
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.
Chapter 1: Creating Java Programs
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
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,
The Java Programming Language
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to programming in the Java programming language.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
© 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 24.1 Test-Driving the Enhanced Car Payment.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Creating a Java Application and Applet
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use simple Output statements Understand the different types and uses of comments.
Object Oriented Programming Object and Classes Lecture 3 MBY.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Java Programming: Guided Learning with Early Objects
GC101 Introduction to computer and program
Java programming lecture one
CIS16 Application Development Programming with Visual Basic
Chapter 1: Computer Systems
elementary programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Computer Programming-1 CSC 111
Presentation transcript:

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 and files

3 3 Adding Interface Components to a java application JLabel –An area where uneditable text can be displayed. JButton –An area that triggers an event when clicked. JTextField –An area in which the user inputs data from the keyboard. The area can also display information.

4 Simple steps for creating a user interface: 1. Create a window where gui components are displayed 2. Set the layout of the window 3. Create the gui components 4. Add them to the window

5 Step 1: Create a window on which to place the gui components Container myWindow = getContentPane(); Programmer defined name Part of the Java API Method call to perform a task

6 Step 2: Set the layout of the window myWindow.setLayout (null); (Java doesn’t know where to place the components so you will have to set the bounds (specify where) each component should go) // or myWindow.setLayout (new FlowLayout()); // Java knows where to place the components // (left to right, top to bottom)

7 3. Creating the gui components

8 JLabels JLabel label = new JLabel(); –Constructs an empty Label-text is not displayed JLabel label1 = new JLabel( “Label with text” ); –Constructs a Label that displays the text with default left-justified alignment. // where do you want it to go in the window and // what do you want the size to be label1.setLocation(100,20); label1.setSize(200,100); Kind of object defined in Java api Programmer defined name of object

9 JLabels // decide on the font or use the default font label1.setFont(new Font(“Arial”, Font.PLAIN,40)); Any valid font name Font.BOLD Font.ITALIC Size of font in pixels

10 JLabels // where within the allocated space should the // label appear ? label1.setHorizontalAlignment( JLabel.CENTER); // other alignment choices: LEFT RIGHT // add the label to the window myWindow.add(label1);

11 JLabel JLabel label2= new JLabel( ); –Constructs a label that is empty label2.setIcon (new ImageIcon(“java.jpg”)); The label contains a picture instead of text. // set the bounds label2.setBounds(300,100); // set the alignment label2. setHorizontalAlignment( JLabel.CENTER);

12 JLabel // add label2 to the window myWindow.add(label2);

13 Creating a complete Java application Creating a complete Java application

14 Coding/Implementing the Solution Integrated development environments (IDE) such as NetBeans can be used to make coding simpler. NetBeans is a free download from Sun and is installed on the computers in the Junkins lab and the SIC open lab. Instructions for using NetBeans are available via a link at the top of your outline. You should print them out before next week’s lab. Lab assistants should show you how to use NetBeans next week.

15 Coding the Program - Comments as Documentation Purpose of comments –Provides clear description when reviewing code –Helps programmer think clearly when coding Placement of comments –Use a comment header to identify a file and its purpose –Place a comment at the beginning of code for each event and method –Place comments near portions of code that need clarification

16 Coding the Program - Comments as Documentation General form: /* block comments */ // line comments Example: /* Programmer:Judy Date:Sept. 3, 2007 Filename:MyProgram.java Purpose: This program displays the name and webaddress of a company */

17 Import Packages Use the import statement to access classes in the SDK –The java.lang package is automatically imported –Place the import statement before the class header –Use an asterisk (*) after the package name and period delimiter to import all necessary classes in the package import javax.swing.*;

18

19 import javax.swing.JOptionPane; The import statement includes particular classes from a particular package (folder) in the java library of classes

20 import javax.swing.JApplet; The import statement includes particular classes from a particular package (folder) in the java library of classes

21 import javax.swing.*; public class Welcome4 All code in java must be inside a class definition; the above line begins the definition of a class called Welcome4.

22 Coding the Program - The Class Header Identify how the code can be accessed with an access modifier –public indicates that the code can be accessed by any and all entities Specify a unique name for the class –The class name at the beginning of the program must match the file name exactly –Java is case-sensitive –Must begin with an underscore, dollar sign or letter and can then contain underscores, $, letters, or digits (no special characters)

23 Coding the Program - The Class Header –Cannot be reserved words –By convention, uppercase letters are used for class names and to distinguish words in class names

24 Sample Class Header Use braces { } after the class header to enclose the class body public class Person { // body of the class }

25 import javax.swing.*; public class Welcome extends JFrame We want our class to have the characteristics of a JFrame Keyword extends gives us inheritance in Java

26 Coding the Program - The Method Header The method header contains modifiers, return value, method name, and parameters along with their data type Every stand-alone Java application must contain a main() method, which is the starting point during execution

27 Coding the Program - The Method Header Modifiers set properties for a method –public allows other programs to invoke this method –static means this method is unique and can be invoked without creating a subclass or instance Return value is the data type of the data returned by the method –If no data is returned, the keyword void is used

28 Coding the Program - The Method Header Parameters are pieces of data received by the method to help the method perform its operation –Identifiers are used to name the variable sent to the method

29 Special Characters/ keywords CharacterUse // Double slash Marks the beginning of a comment import Tells the compiler where to search for the packages that contain predefined code

30 Special Characters CharacterUse { } Open / close braces Encloses a group of statements, such as the contents of a method ( ) Open / close parentheses Used in naming a method such as in public void paint (….)

31 import javax.swing.*; // import statements required to use part of the Java API import java.awt.*; public class LabelFrame extends JFrame { private JLabel label1, label2; public LabelFrame( ) { Container myWindow= getContentPane(); myWindow.setLayout(null); myWindow.setBackground(Color.CYAN);

32 label1 = new JLabel(“Label with text”); label1.setLocation(300,20); label1.setSize(200,100); myWindow.add( label1 ); label2= new JLabel( ); label2.setIcon (new ImageIcon(“java.jpg”)); label2.setBounds(300,100); label2. setHorizontalAlignment(JLabel.CENTER); myWindow.add(label2); setTitle(“Testing JLabel”); setSize(300,200); setVisible(true); } // end of LabelFrame()

33 // Every Java application must have a method main public static void main(String a[]) { LabelFrame l = new LabelFrame(); l.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }// end of main }// end of class

34 Label with text

35 Testing the Solution Compile the source code If the compiler detects errors, fix the errors and compile again If the compilation was successful, a new bytecode file for each class is created with a.class extension run the program (test it logically)

36 Debugging the Solution System Errors –System command is not set properly –Software is installed incorrectly –Location of stored files is not accessible Syntax Errors –One or more violations of the syntax rules of Java

37 Debugging the Solution Logic and Run-Time Errors –Unexpected conditions during execution of a program –Wront results

38 Running the Application After compilation is successful, run the program to test for logic and run-time errors

39 Editing the Source Code - cont. Recompile and run the application –The bytecode should be updated after any changes to the source code Print a hard copy of the source code –The final step of the program development cycle is to document the solution