Introduction to Computing
Concepts
Note Set 6
Where we’re going…
Using a GUI
• Functionality: the tasks that an application can
perform
• Events: user actions in a program
• Event Handlers: segments of code that are executed
in response to an event
Review
• All Java programs consist of classes.
▫ Help organize a code base of an application
▫ class represents an object that is participating in the
problem domain usually
• Classes have methods.
▫ code statements grouped together that perform a
single task in the program
▫ Can accept information to be used in processing
▫ Can feed back information to whatever invoked the
method
• Methods contain blocks of code that help “get the
job done”.
More on methods…
Input
Process
Output
Declaring the Class
Keyword in Java
Name of Class
public class Inventory {
Keyword –Word in a language that
has meaning. Cannot be used in
any other way.
Java is Case Sensitive.
var and VAR and Var are different
identifiers.
Name of class
•By Convention – always begin with
capital letter
•An Identifier
•must start with letter,
underscore or dollar sign
•can contain letters,
underscores, numbers, dollar
signs. No spaces
Checkpoint
• Which are valid identifiers in Java?
▫ 123ABC
▫ _myVar
▫ _myVar123
▫ $Abc?135
Name of File/Class & Body
• Source code is stored in files in the file system.
• Name of file must be the name of the (public) class
stored in that file with the extension .java
• Very important to the java compiler and debugger
• Body of the class begins with the opening curly brace
{
Inheritance Introduction
public class Inventory extends JFrame {
• extends keyword indicates your class is “inheriting”
from another class
• Inheritance: “inheriting” all of the functionality of a
previously defined class and adding to it or changing
some of it
• With JFrame, helps us get the GUI working.
Event Driven System
• When using a GUI app
▫ app waits for the user to “do something” (generates an
event)
▫ app then responds based on the action that user
performed
General Idea:
Step 1: Did the user perform an action?
If yes: Handle the event by calling appropriate method
If no: Go back to Step 1.
Event Handler
• Event Handler : Java method that will be executed
when a particular action is taken by the user
▫
▫
▫
▫
click of a button
double click on a name
move mouse over a picture
etc………
private void calcButtonHandler(ActionEvent e){
//place code here to be executed when
//appropriate
}
Event Handler
• Need to tell the button which handler to call
Must tell button about method
Provide the link in code
private void calcButtonHandler(ActionEvent e){
//place code here to be executed when
//appropriate
}
13
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
totalResultJTextField.setEditable( false );
contentPane.add( totalResultJTextField );
// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setText( "Calculate Total" );
calculateJButton.setBounds( 204, 48, 126, 24 );
contentPane.add( calculateJButton );
calculateJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// method called when calculate JButton is pressed
public void actionPerformed( ActionEvent event )
{
calculateJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
 2004 Prentice Hall, Inc.
All rights reserved.
Event Handler
• More specifically – when button clicked/pressed, it
generates an actionPerformed event.
on click by user,
actionPerformed event
generated.
What to do?
• In inventory app,
▫
▫
▫
▫
grab textual data from the two text fields,
convert textual data to numeric data
multiply
display in total’s text field.
Textual vs. Numeric
• Data entered in text fields is text.
▫ Even though it’s a number, to the computer it doesn’t
represent a quantity.
• Must convert to text to a number
Integer.parseInt(
);
Will put the data from the
textField
• Ask the textfield for its data
cartonsJTextField.getText();
itemsJTextField.getText();
Math in Java
• Performing Math in Java is easy
• Basic Math Operators
▫
▫
▫
▫
▫
Addition +
Subtraction Division /
Multiplication *
Modulus %
Follows PEMDAS
All are Binary Operators
• Assignment operator: =
▫ Allows you to store a value
variable = 3;
▫ Always right associative: Stores what’s on right in
what’s on the left. (more abt variables later)
Putting it Together
multiplying two values
Integer.parseInt(cartonsJTextField.getText()) *
Integer.parseInt(itemsJTextField.getText());
Need to take the value from
this equation and display it in the
convert result of mult
back to a string so it can
be displayed
totalResultJTextField.setText(String.valueOf (
Integer.parseInt(cartonsJTextField.getText()) *
Integer.parseInt(itemsJTextField.getText())
));
Notice the parentheses have to match…
Important Concepts:
•
•
•
•
•
•
•
•
•
•
Classes and Methods
Keywords
Rules for Identifier Names
Inheritance/extends
Events
Event Handler
Converting text to number
Converting number to text
Basic Math Operators
Nesting of braces and parentheses