Basic Java - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Basic Java

Description:

How to use TextPad for Java. How to define classes and objects. How to create a GUI interface ... Create panel p. Add p to pane. Panels can contain GUI objects ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 23
Provided by: computing84
Category:
Tags: basic | create | how | java | to

less

Transcript and Presenter's Notes

Title: Basic Java


1
Basic Java Interface design
2
Understand
How to define classes and objects
How to create a GUI interface
How event-driven programming works
How classes inherit methods
Overall program structure
How to use TextPad for Java
3
Classes or Objects in Java
Illustrate with Rectangle object
Class definition of rectangle
including constructor, properties, methods
Driver or Test program that creates instance of
rectangle
4
class blueprint for object
class Rectangle

int height , width
public Rectangle (int h, int w )
height h width w
int findArea ( )
return height width
int getHght ( ) return height

5
Driver or Test program
public class TestRectangle

public static void main ( String args )

Rectangle r
r new Rectangle (2, 4)
int area r.findArea ( )
System.out.println ( "Area is " area )

6
public class Rectangle

public static void main ( String args )

Rectangle r
r new Rectangle (2, 4)
int area r.findArea ( )
System.out.println ( "Area is " area )
Rectangle s new Rectangle (5, 10)
System.out.println ( "Area is " s.findArea (
) )

7
Defining Executing a window-like object
8
Import Java class libraries to be used
import javax.swing. import java.awt. import
java.awt.event.
public class X extends JFrame
X is a JFrame ...and maybe more
currently... just a shell
9
import javax.swing. import java.awt. import
java.awt.event.
class X file X.java
public class X extends JFrame
needs a main program to run
Compile ctrl-1 in TextPad Execute ctrl-2
" "
not this class !
10
Empty JFrame
11
main
import javax.swing. import java.awt.
public class TestX
main program
public static void main (String args )
X m new X()
m.setVisible(true) m.setSize(200,
200)
creates instance m like int k 3
JFrame methods
// MyInput.readString()
For dos IO or stalling program - requires MyInput
class
12
Summary
class X - blueprint for object
import - provides methods for object
extends JFrame - X can use inherits JFrame
methods
naming - class X in file X.java
TextPad - can compile separately but run main
Main - public static void main (String
args)
create instance - X m new X( )
frame methods - setVisible(true)
setSize(200, 200)
DOS IO - MyInput.readString ( )
13
Calculator example
inputs
multiply
14
Plan of Attack for calculator object
Structure
Java
Containers content pane getContentPane ( )
Containers a canvas JPanels
Make input fields place in panel JTextFields
Make buttons trigger actions JButtons
Define handler for button events actionPerformed
Acquire, convert and store field data
15
1. JFrame
2. ContentPane
3. JPanel
hello
button
JButton
4. JTextField
16
JFrame
17
panels
import javax.swing. import java.awt.
import java.awt.event. public class Y
extends JFrame

Constructor Y()
JPanel p
Declare panel p
public Y()
p new JPanel()
Create panel p
this.getContentPane().add(p)
Add p to pane
Panels can contain GUI objects like buttons
18
import javax.swing. import java.awt. import
java.awt.event.
Fields Buttons
public class Y extends JFrame implements
ActionListener
JPanel p
JTextField n1, n2,
n3 JButton b1,b2
declare fields buttons
public Y() p new JPanel()
this.getContentPane().add(p)
create fields add to p
n1new JTextField(10) p.add(n1) n2new
JTextField(10) p.add(n2) n3new
JTextField(10) p.add(n3)
create buttons add to p
b1new JButton("") p.add(b1) b2new
JButton("") p.add(b2)
19
Events
import javax.swing. import java.awt.
import java.awt.event. public class Y
extends JFrame
says class has events
implements ActionListener
JPanel p JTextField n1, n2,
n3 JButton b1, b2
public Y() p new JPanel() this.getConte
ntPane().add(p) n1new JTextField(10)
p.add(n1) n2new JTextField(10) p.add(n2)
n3new JTextField(10) p.add(n3) b1new
JButton("") p.add(b1) b2new
JButton("") p.add(b2)
make buttons listen for events
b1.addActionListener(this) b2.addActionListen
er(this)
public void actionPerformed (ActionEvent
bert)
template for event handler
20
import javax.swing. import java.awt.
import java.awt.event. public class Y
extends JFrame implements ActionListener

event handler template
JPanel p JTextField n1, n2,
n3 JButton b1, b2
public Y() p new JPanel() this.getConte
ntPane().add(p) n1new JTextField(10)
p.add(n1) n2new JTextField(10) p.add(n2)
n3new JTextField(10) p.add(n3) b1new
JButton("") p.add(b1) b2new
JButton("") p.add(b2) b1.addActionListe
ner(this) b2.addActionListener(this)
get/convert data
public void actionPerformed (ActionEvent bert
)
or - depending on source of event
int num1Integer.parseInt(n1.getText()) int
num2Integer.parseInt(n2.getText()) int
num30
if (bert.getSource()b1) num3num1 num2
if (bert.getSource ()b2) num3num1
num2
n3.setText ( String.valueOf ( num3 ) )
convert back/store
21
import javax.swing. import java.awt.
import java.awt.event. public class Y
extends JFrame implements ActionListener

class Y definition
JPanel p JTextField n1, n2,
n3 JButton b1, b2
Global declarations
public Y() p new JPanel() this.getConte
ntPane().add(p) n1new JTextField(10)
p.add(n1) n2new JTextField(10) p.add(n2)
n3new JTextField(10) p.add(n3) b1new
JButton("") p.add(b1) b2new
JButton("") p.add(b2) b1.addActionListe
ner(this) b2.addActionListener(this)
Constructor Y ( )
public void actionPerformed (ActionEvent
bert) int num1Integer.parseInt(n1.getText())
int num2Integer.parseInt(n2.getText()) i
nt num30 if (bert.getSource()b1)num3num1
num2 if (bert.getSource()b2)num3num1
num2 n3.setText ( String.valueOf(num3)
)
Event-handler
22
Terms and Concepts
JPanels
TextPad
JPanels add method
Classes
this notation
Objects
ActionEvent
Methods
ActionEvente getSource() method
Instances of objects
JFrames setSize method
Event-driven programming
JFrames setVisible method
Asynchronous
import statements
Inheriting properties methods
Multiple instances
extend class
Program structure
Constructors
Method signature
JFrames
GUI components
Color objects
RGB representation
JTextFields
JPanels setBackground ( c) method
JButtons
JLabels
dos IO MyInput.readString ( )
Write a Comment
User Comments (0)
About PowerShow.com