Java-Intro-Overview

Download Report

Transcript Java-Intro-Overview

Basic Java – Interface design
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
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
class= blueprint for object
class Rectangle
{
int height , width ;
constructor
public Rectangle (int h, int w )
{
}
height = h;
width = w;
methods
int findArea ( )
{
return height * width ;
}
int getHght ( ) { return height }
}
properties or
instance variables
Driver or Test program
saved in file
TestRectangle.java
public class TestRectangle
{
constructor
public static void main ( String [ ] args )
{
instance r
of rectangle
Rectangle r ;
r = new Rectangle (2, 4) ;
int area = r.findArea ( );
System.out.println ( "Area is: " + area )
}
}
say:
r’s findArea
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 ) ;
another
instance
Rectangle s = new Rectangle (5, 10) ;
System.out.println ( "Area is: " + s.findArea ( ) );
}
}
Defining & Executing
a window-like object
import javax.swing.*
import java.awt.*
import java.awt.event.*
;
;
;
Import Java
class libraries
to be used
public class X extends JFrame
{
X is a JFrame
...and maybe more
}
currently...
just a shell
import javax.swing.*
import java.awt.*
import java.awt.event.*
;
;
;
public class X extends JFrame
{
class
file
X
X.java
needs a main
program to run
}
Compile: ctrl-1
Execute: ctrl-2
[ in TextPad ]
" "
not this class !
Empty
JFrame
main
import javax.swing.* ;
import java.awt.*
;
main program
public class TestX
{
public static void main (String [ ] args )
{
creates instance m
X m = new X()
;
like: int k = 3;
m.setVisible(true)
;
m.setSize(200, 200) ;
JFrame methods
}
}
// MyInput.readString();
For dos IO or stalling program
- requires MyInput class
Summary
class X
- blueprint for object
import
- provides methods for object
extends JFrame
- X can use [inherits] JFrame methods
naming
- class X
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 ( ) ;
in file
X.java
Calculator example
inputs
multiply
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
2. ContentPane
1. JFrame
3. JPanel
hello
button
4. JTextField
JButton
JFrame
Title Bar
Content Pane
Panel
JLabel
JTextField
JButton
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Constructor Y()
panels
public class Y extends JFrame
{
JPanel
p;
Declare panel p
public Y()
{
p = new JPanel()
this.getContentPane().add(p) ;
;
Create panel p
Add p to pane
}
Panels can contain GUI
objects like buttons
}
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);
n1=new JTextField(10); p.add(n1);
}
}
n2=new JTextField(10);
n3=new JTextField(10);
p.add(n2);
p.add(n3);
b1=new JButton("+");
p.add(b1);
b2=new JButton("*");
p.add(b2);
create fields & add to p
create buttons & add to p
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
says class has events
public class Y extends JFrame implements
{
JPanel
p;
JTextField
JButton
public Y()
{
Events
ActionListener
n1, n2, n3 ;
b1, b2
;
p = new JPanel()
this.getContentPane().add(p)
n1=new JTextField(10);
n2=new JTextField(10);
n3=new JTextField(10);
b1=new JButton("+") ;
b2=new JButton("*") ;
;
;
p.add(n1);
p.add(n2);
p.add(n3);
p.add(b1);
p.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
make buttons listen
for events
}
public void actionPerformed (ActionEvent bert)
{
}
}
template for
event handler
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Y extends JFrame implements ActionListener
{
JPanel
p;
JTextField
JButton
public Y()
{
event handler template
n1, n2, n3 ;
b1, b2
;
p = new JPanel()
this.getContentPane().add(p)
n1=new JTextField(10);
n2=new JTextField(10);
n3=new JTextField(10);
b1=new JButton("+") ;
b2=new JButton("*") ;
;
;
p.add(n1);
p.add(n2);
p.add(n3);
p.add(b1);
p.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
get/convert data
}
public void actionPerformed (ActionEvent bert )
{
int num1=Integer.parseInt(n1.getText())
int num2=Integer.parseInt(n2.getText())
int num3=0;
}
}
;
;
if (bert.getSource()==b1) { num3=num1 + num2
if (bert.getSource ()==b2) { num3=num1 * num2
;}
;}
n3.setText ( String.valueOf ( num3 ) )
;
+ or - depending on
source of event
convert & back/store
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Y definition
public class Y extends JFrame implements ActionListener
{
JPanel
p;
JTextField
JButton
public Y()
{
n1, n2, n3 ;
b1, b2
;
Global declarations
p = new JPanel()
this.getContentPane().add(p)
n1=new JTextField(10);
n2=new JTextField(10);
n3=new JTextField(10);
b1=new JButton("+") ;
b2=new JButton("*") ;
;
;
p.add(n1);
p.add(n2);
p.add(n3);
p.add(b1);
p.add(b2);
Constructor Y ( )
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed (ActionEvent bert)
{
int num1=Integer.parseInt(n1.getText()) ;
int num2=Integer.parseInt(n2.getText()) ;
int num3=0;
if (bert.getSource()==b1){num3=num1 + num2
if (bert.getSource()==b2){num3=num1 * num2
n3.setText ( String.valueOf(num3) )
}
}
Event-handler
;}
;}
;
Terms and Concepts
TextPad
Classes
Objects
Methods
Instances of objects
Event-driven programming
Asynchronous
Inheriting properties & methods
extend class
Constructors
Method signature
JFrames
GUI components
JTextFields
JButtons
JLabels
JPanels
JPanel’s add method
this notation
ActionEvent
ActionEvent’e getSource() method
JFrame’s setSize method
JFrame’s setVisible method
import statements
Multiple instances
Program structure
Color objects
RGB representation
JPanel’s setBackground ( c) method
dos IO – MyInput.readString ( )