Вы находитесь на странице: 1из 22

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 properties or


instance variables
{
int height , width ;
constructor
public Rectangle (int h, int w )
{
height = h;
width = w;
}
methods

int findArea ( )
{
return height * width ;
}

int getHght ( ) { return height }

}
Driver or Test program
saved in file
TestRectangle.java

public class TestRectangle


{ constructor
public static void main ( String [ ] args )
{
instance r Rectangle r ;
of rectangle
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 Java
import javax.swing.* ; class libraries
import java.awt.* ; to be used
import java.awt.event.* ;

public class X extends JFrame


{

X is a JFrame
} ...and maybe more

currently...
just a shell
import javax.swing.* ;
import java.awt.* ; class X
import java.awt.event.* ;
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 !
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 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 ( ) ;
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() ;
Create panel p
this.getContentPane().add(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); create fields & add to p


n2=new JTextField(10); p.add(n2);
n3=new JTextField(10); p.add(n3);

b1=new JButton("+"); p.add(b1); create buttons & add to p


b2=new JButton("*"); p.add(b2);
}

}
import javax.swing.*;
import java.awt.*; says class has events
Events
import java.awt.event.*;

public class Y extends JFrame implements ActionListener


{
JPanel p;
JTextField n1, n2, n3 ;
JButton b1, b2 ;
public Y()
{ p = new JPanel() ;
this.getContentPane().add(p) ;

n1=new JTextField(10); p.add(n1);


n2=new JTextField(10); p.add(n2);
n3=new JTextField(10); p.add(n3);
b1=new JButton("+") ; p.add(b1);
b2=new JButton("*") ; p.add(b2);

b1.addActionListener(this); make buttons listen


b2.addActionListener(this); 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


{ event handler template
JPanel p;
JTextField n1, n2, n3 ;
JButton b1, b2 ;
public Y()
{ p = new JPanel() ;
this.getContentPane().add(p) ;

n1=new JTextField(10); p.add(n1);


n2=new JTextField(10); p.add(n2);
n3=new JTextField(10); p.add(n3);
b1=new JButton("+") ; p.add(b1);
b2=new JButton("*") ; p.add(b2);

b1.addActionListener(this); get/convert data


b2.addActionListener(this);
}
public void actionPerformed (ActionEvent bert )
{
int num1=Integer.parseInt(n1.getText()) ; + or - depending on
int num2=Integer.parseInt(n2.getText())
int num3=0;
; source of event
if (bert.getSource()==b1) { num3=num1 + num2 ;}
if (bert.getSource ()==b2) { num3=num1 * num2 ;}

n3.setText ( String.valueOf ( num3 ) ) ;

} 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 n1, n2, n3 ;
JButton b1, b2 ; Global declarations
public Y()
{ p = new JPanel() ;
this.getContentPane().add(p) ;

n1=new JTextField(10); p.add(n1);


n2=new JTextField(10); p.add(n2); Constructor Y ( )
n3=new JTextField(10); p.add(n3);
b1=new JButton("+") ; p.add(b1);
b2=new JButton("*") ; p.add(b2);

b1.addActionListener(this);
b2.addActionListener(this);
}

public void actionPerformed (ActionEvent bert)


{
Event-handler
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) ) ;
}
}
Terms and Concepts

TextPad JPanels
Classes JPanel’s add method
Objects this notation
Methods ActionEvent
Instances of objects ActionEvent’e getSource() method
JFrame’s setSize method
Event-driven programming
JFrame’s 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
JPanel’s setBackground ( c) method
JButtons
JLabels dos IO – MyInput.readString ( )

Вам также может понравиться