Transcript Slide

ECP4136 Java Technology
Tutorial 6
Activities
• Revision – Tutorial 4
• Work on tutorial questions
– today’s target – part 1 of tutorial 6
• Try to figure out:
– ways of implementing ActionListener
– ways of passing information of the button
being pressed (hence the Color) to
ButtonListener in Tutorial 4
• Last 45 minutes - Discussion
Discussion
a)
b)
c)
d)
Revisit static keyword
Use of this keyword
Ways of implementing ActionListener
Ways of passing information of the
button being pressed (hence the Color)
to ButtonListener in Tutorial 4
The static keyword
int size, weight;
char category;
Data declarations
Method declarations
Variables
Methods
public
private
Violate
encapsulation
Enforce
encapsulation
Provide services
to clients
Support other
methods in the
class
The static keyword
• Examples:
– class variables:
• Math.PI, JFrame.EXIT_ON_CLOSE
– class methods:
• Math.sin( ), JOptionPane.showMessageDialog( )
static
Variables
Instance
variables
Class
variables
Methods
Instance
methods
Class
methods
The static keyword
• How can we identify whether it is an instance or not?
String str = new String(“Instantiate me”);
JButton blueButton = new JButton(“Blue”);
Random gen = new Random( );
• Recall how we called the class variables/methods…
int area = Math.PI*radius*radius;
String age = JOptionPane.showInputDialog(“Enter your age:”);
static
Variables
Instance
variables
Class
variables
Methods
Instance
methods
Class
methods
The static keyword
public class Student {
public int count = 0;
private String name;
public Student(String stdName) {
Ivan
Ali
name = stdName;
count++;
int count;
String name;
int count;
String name;
}
}
public class Tutorial {
public static void main(String[ ] args) {
Student Ivan = new Student(“Ivan”);
Student Ali = new Student(“Ali”);
}
}
count = 1
count = 1
name = “Ivan”
name = “Ali”
The static keyword
public class Student {
public static int count = 0;
Ivan
private String name;
Ali
public Student(String stdName) {
int count;
name = stdName;
count++;
declared as
static
}
}
String name;
String name;
public class Tutorial {
public static void main(String[ ] args) {
Student Ivan = new Student(“Ivan”);
Student Ali = new Student(“Ali”);
}
}
count = 1
count = 2
name = “Ivan”
name = “Ali”
The static keyword
• Under normal
circumstances
– Class variables may
/may not be
encapsulated
– Instance variables
are encapsulated
Ivan
Ali
int count;
String name;
String name;
• Assessors &
Mutators
count = 1
count = 2
name = “Ivan”
name = “Ali”
The static keyword
• Recall that instance variables/methods are
referenced through the objects
(i.e. instances has to be created – new keyword)
• Example:
JFrame frame = new JFrame(“Circle”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
• You cannot refer to the instance variables
if there is no instance created yet.
The static keyword
• Remember, you
cannot refer to the
instance variables if
there is no instance
created yet!
• Now, let’s say you
have a accessor to
get the mark of
students…
public class Student {
private int mark;
private String name;
public Student(String stdName) {
name = stdName;
mark = 0;
}
public int getMark( ) { return mark; }
}
public class Tutorial {
public static void main(String[ ] args) {
Student Ivan = new Student(“Ivan”);
Ivan.getMark( );
}
}
The static keyword
public class Student {
private int mark;
private String name;
public Student(String stdName) {
Can we set this
as class
method?
name = stdName;
mark = 0;
}
public static int getMark( ) { return mark; }
}
public class Tutorial {
public static void main(String[ ] args) {
Student Ivan = new Student(“Ivan”);
Ivan.getMark( );
}
}
The static keyword
• Compile-time
error !!!
public class Student {
private int mark;
private String name;
public Student(String stdName) {
name = stdName;
javac Tutorial.java
Student.java:8: non-static
variable mark cannot be
reference from a static
context
mark = 0;
}
public static int getMark( ) { return mark; }
}
public class Tutorial {
• Look’s familiar?
public static void main(String[ ] args) {
Student Ivan = new Student(“Ivan”);
Ivan.getMark( );
|
}
The static keyword
• That is what would
happen if you try to
initialise instances in
the main method.
import javax.swing.JPanel;
//
// This is a bad example, do not follow!
//
public class TestGUI {
private JPanel panel;
javac TestGUI.java
public static void main(String[] args) {
TestGUI.java:19: non-static
variable panel cannot be
referenced from a static
context
// Common wrong perception,
// initialisation in main method
panel = new JPanel();
• Isn’t main method a
static type?
// ...
}
}
The this keyword
• “Within an instance method or a
constructor, this is a reference to the
current object — the object whose method
or constructor is being called.”
• Keywords:
– Instance, object
The this keyword
• Having hard times creating different
names for similar variables?
public class Circle {
private int xPoint, yPoint, size;
private Color circleColor;
public Circie(int xLocation, int yLocation, int diameter, Color shadeColor) {
xPoint = xLocation;
yPoint = yLocation;
size = diameter;
circleColor = shadeColor;
}
The this keyword
• More elegant way to solve it
– Simple & avoid confusion
public class Circle {
private int xPoint, yPoint, size;
private Color circleColor;
public Circie(int xPoint, int yPoint, int size, Color circleColor) {
this.xPoint = xPoint;
this.yPoint = yPoint;
this.size = size;
this.circleColor = circleColor;
}
The this keyword
• It is also pretty useful for the overloading
of the constructor
public class Circle {
private int xPoint, yPoint, size;
private Color circleColor;
public Circle(int xPoint, int yPoint) {
this(xPoint, yPoint, 10, Color.black);
}
public Circie(int xPoint, int yPoint, int size, Color circleColor) {
this.xPoint = xPoint;
this.yPoint = yPoint;
this.size = size;
this.circleColor = circleColor;
}
About the ActionListener
The are a few possible ways to implement
the ActionListener in Tutorial 4
√
√
a) Define a private class in the same source
file
b) Define class as the input parameter of the
addActionListener method
c) Implement on the JPanel subclass itself
About the ActionListener
import java.awt.event.*;
import javax.swing.*;
public class TestPanel extends JPanel {
a) Define a private class
in the same source file
public TestPanel ( ) {
JButton blueButton = new JButton(“Blue”);
blueButton.addActionListener( new ButtonListener() );
add(blueButton);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// do something here
}
} // inner class
}
Must know !
About the ActionListener
import java.awt.event.*;
import javax.swing.*;
public class TestPanel extends JPanel {
public TestPanel ( ) {
JButton blueButton = new JButton(“Blue”);
blueButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
//do something here
}
}
);
add(blueButton);
}
}
b) Define class as the
input parameter of the
addActionListener
method
About the ActionListener
import java.awt.event.*;
import javax.swing.*;
public class TestPanel extends JPanel implements ActionListener {
public TestPanel ( ) {
JButton blueButton = new JButton(“Blue”);
blueButton.addActionListener( this );
add(blueButton);
}
public void actionPerformed(ActionEvent event) {
// do something here
} // it’s a method of TestPanel, not an inner class
}
c) Implement on
the JPanel
subclass itself
About the ActionListener
The are a few possible ways to implement
the ActionListener in Tutorial 4
√
√
a) Define a private class in the same source
file
b) Define class in the addActionListener
method
c) Implement on the JPanel subclass itself
Passing information to
ActionListener
•
•
How to determine which button is
pressed in Tutorial 4?
Again, there are at least four ways of
doing this:
a) Simplest method – redundant classes
b) ActionEvent -> getSource( )
c) Utilize setActionCommand and
getActionCommand
d) Object oriented concept -> pass to constructor
Passing information to
ActionListener
import java.awt.event.*;
import javax.swing.*;
public class TestPanel extends JPanel {
a) redundant classes
public TestPanel ( ) {
JButton blueButton = new JButton(“Blue”);
blueButton.addActionListener( new BlueButtonListener() );
add(blueButton);
}
private class BlueButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// this is for sure a blue button !
}
} // inner class
// private class GreenButtonListener implements ActionListener { … }
// private class RedButtonListener implements ActionListener { … }
}
Passing information to
ActionListener
import java.awt.event.*;
import javax.swing.*;
public class TestPanel extends JPanel {
a) redundant classes
public TestPanel ( ) {
JButton blueButton = new JButton(“Blue”);
blueButton.addActionListener( new BlueButtonListener() );
add(blueButton);
}
private class BlueButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// this is for sure a blue button !
}
} // inner class
stupid,
// private class GreenButtonListener implements ActionListener { …Looks
}
// private class RedButtonListener implements ActionListener { … } but it works !
}
Passing information to
ActionListener
import java.awt.event.*;
import javax.swing.*;
public class TestPanel extends JPanel {
JButton blueButton;
public TestPanel ( ) {
blueButton = new JButton(“Blue”);
blueButton.addActionListener( new BlueButtonListener() );
blueButt
add(blueButton);
}
private class BlueButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource()==blueButton) {
// it’s blue, call the blue color object
} else {
// it’s other buttons
}
}
} // inner class
}
b) ActionEvent ->
getSource( )
Passing information to
ActionListener
import java.awt.event.*;
import javax.swing.*;
public class TestPanel extends JPanel {
JButton blueButton;
public TestPanel ( ) {
blueButton = new JButton(“Blue”);
blueButton.addActionListener( new BlueButtonListener() );
blueButton.setActionCommand( “blue”);
add(blueButton);
}
private class BlueButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if ( (event.getActionCommand( )).equals(“blue”) ) {
// it’s blue, call the blue color object
} else {
// it’s other buttons
}
}
} // inner class
}
c) Utilize setActionCommand
and getActionCommand
Passing information to
ActionListener
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class TestPanel extends JPanel {
JButton blueButton;
d) Object oriented concept ->
pass to constructor
public TestPanel ( ) {
blueButton = new JButton(“Blue”);
blueButton.addActionListener( new ButtonListener( Color.blue ) );
add(blueButton);
}
private class ButtonListener implements ActionListener {
Color buttonColor;
public ButtonListener(Color buttonColor) { this.buttonColor = buttonColor; }
public void actionPerformed(ActionEvent event) {
// we can directly call the code we want
}
} // inner class
}
Conclusion
• Multiple ways of achieving a single target
– Look for alternatives, judge for the best
method
– Discuss with peers
• Homework? You may try to:
– Modify your PlusPanel according to the
previously discussed method of
implementation
Reference
• Using the this Keyword
(link:http://download.oracle.com/docs/cd/E
17409_01/javase/tutorial/java/javaOO/this
key.html)