Class 1 ~ Chapter 1

Download Report

Transcript Class 1 ~ Chapter 1

CSE1340
Class 4
1
Objectives

Write a simple computer program in Java

Use Swing components to build the GUI

Use proper naming conventions for
classes and files
2
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.
3

3
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
4
Step 1: Create a window on which to place the gui
components
Container myWindow = getContentPane();
Part of the Java API
Method call to perform a
task
Programmer defined name
5
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)
6
3. Creating the gui components
7
Kind of object defined in Java api
JLabels

Programmer defined name
of object
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);
8
label1.setSize(200,100);
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
9
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);
10
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);
11
JLabel
// add label2 to the window
myWindow.add(label2);
12
Creating a complete Java
application
13
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.
14
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
15
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 */
16
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.*;
17
18
import javax.swing.JOptionPane;
The import statement includes particular
classes from a particular package (folder)
in the java library of classes
19
import javax.swing.JApplet;
The import statement includes particular
classes from a particular package (folder) in
the java library of classes
20
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.
21
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)
22
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
23
Sample Class Header

Use braces { } after the class header to
enclose the class body
public class Person {
// body of the class
}
24
import javax.swing.*;
public class Welcome extends JFrame
Keyword extends gives us inheritance in Java
We want our class to have the characteristics
of a JFrame
25
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
26
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
27
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
28
Special Characters/
keywords
Character
Use
// Double slash
Marks the beginning
of a comment
import
Tells the compiler
where to search for
the packages that
contain predefined
code
29
Special Characters
Character
Use
{ } 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 (….)
30
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);
31
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()
32
// 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
33
Label with text
34
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)
35
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
36
Debugging the Solution

Logic and Run-Time Errors
– Unexpected conditions during execution of a
program
– Wront results
37
Running the Application

After compilation is successful, run the
program to test for logic and run-time
errors
38
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

39