Escolar Documentos
Profissional Documentos
Cultura Documentos
Chapter 6
Methods: Part II
Math.sqrt( 900.0 )
Type double
Java I--Copyright © 2000 Tom Hunter
Math Class Methods
System.out.println( 30.0 );
• The return value of Math.sqrt is the argument for the
println method.
Java I--Copyright © 2000 Tom Hunter
Math Class Methods
Math m;
d = square( x );
w.myMethod()
Integer.parseInt()
sum = x + y;
return sum;
}
• number of arguments
• type of arguments
• order of arguments
• The listener object(s) will then use the details in the event
object to decide how to react to the event.
eventSourceObject.addEventListener( eventListenerObject );
• Code like this requires that the class the panel comes
from to implement the appropriate interface.
actionPerformed( ActionEvent e )
someComponent.addActionListener( MyClass );
// GUI components.
JLabel die1Label,
die2Label,
sumLabel,
pointLabel;
JTextField firstDie,
secondDie,
sum,
point;
JButton roll;
Java I--Copyright © 2000 Tom Hunter
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
must
// GUI components.
JLabel
import
die1Label,
the
event class API:
die2Label,
sumLabel,
java.awt.event.*;
pointLabel;
JTextField firstDie,
secondDie,
sum,
point;
JButton roll;
Java I--Copyright © 2000 Tom Hunter
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// GUI components.
JLabel die1Label,
die2Label,
sumLabel,
pointLabel;
JTextField firstDie,
secondDie,
sum,
point;
JButton roll;
JButton roll;
// GUI components.
JLabel die1Label,
die2Label,
sumLabel,
pointLabel;
JTextField firstDie,
secondDie,
sum,
point;
JButton roll;
// GUI components.
JLabel die1Label,
die2Label,
sumLabel,
pointLabel;
JTextField firstDie,
secondDie,
sum,
point;
JButton roll;
c is a Container
firstDie = new JTextField( 10 ); object that came
firstDie.setEditable( true );
from the Content Pane. Here, we
c.add( firstDie );
are setting the Layout, or how the
roll = new JButton( "Roll get
objects Dice" ); on the page.
stacked
roll.addActionListener( this );
c.add( roll ); There are many different choices
for layouts. We will discuss them
} // end of method init()
later.
--iteration and
--recursion
init()
start()
paint()
• These are always guaranteed to be performed.
• If you override these methods--exactly override their
signatures--they will execute automatically.
• If you do not exactly copy their signatures, then your
override will not automatically be executed.
Java I--Copyright © 2000 Tom Hunter
JApplet’s Methods
• In addition to these are several others:
init()
start()
paint()
stop()
destroy()
• As they are inherited, these members are guaranteed to be
called--but still none of them contain any code.
• They are all empty, and only contain code when you
override them and add some.
Java I--Copyright © 2000 Tom Hunter
JApplet’s Methods
init()
start()
paint()
paint()
paint()
destroy()