ecp4136 java technology tutorial 6. activities revision – tutorial 4 work on tutorial questions...

Post on 21-Dec-2015

222 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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) Revisit static keyword

b) Use of this keyword

c) Ways of implementing ActionListener

d) 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

public private

Variables

MethodsProvide services

to clients

Support othermethods in the

class

Enforceencapsulation

Violateencapsulation

The static keyword

• Examples:– class variables:

• Math.PI, JFrame.EXIT_ON_CLOSE

– class methods:• Math.sin( ), JOptionPane.showMessageDialog( )

static

Variables

MethodsInstancemethods

Class methods

Class variables

Instance variables

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

MethodsInstancemethods

Class methods

Class variables

Instance variables

The static keywordpublic class Student {

public int count = 0;

private String name;

public Student(String stdName) {

name = stdName;

count++;

}

}

public class Tutorial {

public static void main(String[ ] args) {

Student Ivan = new Student(“Ivan”);

Student Ali = new Student(“Ali”);

}

}

int count;String name;

int count;String name;

Ivan Ali

count = 1

name = “Ivan”

count = 1

name = “Ali”

The static keywordpublic class Student {

public static int count = 0;

private String name;

public Student(String stdName) {

name = stdName;

count++;

}

}

public class Tutorial {

public static void main(String[ ] args) {

Student Ivan = new Student(“Ivan”);

Student Ali = new Student(“Ali”);

}

}

String name; String name;

Ivan Ali

count = 1

name = “Ivan”

count = 2

name = “Ali”

declared as static

int count;

The static keyword

• Under normal circumstances– Class variables may

/may not be encapsulated

– Instance variables are encapsulated

• Assessors & Mutators

String name; String name;

Ivan Ali

count = 1

name = “Ivan”

count = 2

name = “Ali”

int count;

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 keywordpublic class Student {

private int mark;

private String name;

public Student(String stdName) {

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( );

}

}

Can we set this as class method?

The static keyword

• Compile-time error !!!

• Look’s familiar?

public class Student {

private int mark;

private String name;

public Student(String stdName) {

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( );

|

}

javac Tutorial.java

Student.java:8: non-static variable mark cannot be reference from a static context

The static keyword

• That is what would happen if you try to initialise instances in the main method.

• Isn’t main method a static type?

import javax.swing.JPanel;//// This is a bad example, do not follow!//public class TestGUI {

private JPanel panel;

public static void main(String[] args) {

// Common wrong perception, // initialisation in main method panel = new JPanel(); // ...

}

}

javac TestGUI.java

TestGUI.java:19: non-static variable panel cannot be referenced from a static context

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 constructorpublic 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 ActionListenerimport java.awt.event.*;import javax.swing.*;

public class TestPanel extends JPanel {

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

}

a) Define a private class in the same source file

Must know !

About the ActionListenerimport 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 ActionListenerimport 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 {

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 { … }

}

a) redundant classes

Passing information to ActionListener

import java.awt.event.*;import javax.swing.*;

public class TestPanel extends JPanel {

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 { … }

}

a) redundant classes

Looks stupid, 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;

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

}

d) Object oriented concept -> pass to constructor

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/E17409_01/javase/tutorial/java/javaOO/thiskey.html)

top related