Lesson Seven: A good example of making a button work.
previous lesson - back to tutorial index -
next lesson
The right way to attach listeners to components is to make a
different class. This class interfaces ActionListener which means that
it has access to properties of the ActionListener interface and
redefines the methods in the class. If you take a look at the one and
only method of the Interface ActionListener (
http://java.sun.com/j2se/1.3/docs/api/index.html ) you will notice
this is the method actionPerformed(ActionEvent e). And this method will
have to be redefined.
Change Calculator.java into:
import java.awt.*;
import java.awt.event.*;
//---------------------------------------------
// Class
//---------------------------------------------
public class Calculator extends Frame {
private TextField numberOne;
private TextField numberTwo;
private Button buttonAdd;
private Button buttonSub;
private Button buttonMul;
private Button buttonDiv;
private Label result;
//---------------------------------------------
// Constructor
//---------------------------------------------
public Calculator() {
Panel panel= getWinComponents();
this.add(panel);
addButtonListeners();
initScreen();
this.validate();
System.out.println("Constructing a
new Calculator...");
}
//---------------------------------------------
// Methods
//---------------------------------------------
private void addButtonListeners() {
ButtonListener buttonListener= new
ButtonListener();
buttonAdd.addActionListener(buttonListener);
}
//---------------------------------------------
private Panel getWinComponents() {
numberOne= new TextField();
numberTwo= new TextField();
buttonAdd= new Button("+");
buttonSub= new Button("-");
buttonMul= new Button("*");
buttonDiv= new Button("/");
result= new Label("Nothing yet...");
Panel panel= new Panel(new
BorderLayout());
Panel numberPanel= new Panel(new
GridLayout(1,2));
numberPanel.add(numberOne);
numberPanel.add(numberTwo);
panel.add(numberPanel,
BorderLayout.NORTH);
Panel operationsPanel= new Panel(new
GridLayout(1,4));
operationsPanel.add(buttonAdd);
operationsPanel.add(buttonSub);
operationsPanel.add(buttonMul);
operationsPanel.add(buttonDiv);
panel.add(operationsPanel,
BorderLayout.CENTER);
panel.add(result,
BorderLayout.SOUTH);
return panel;
}
//---------------------------------------------
private void initScreen() {
this.addWindowListener(new
WindowAdapter() {
public void windowClosing(WindowEvent
e) {
System.exit(0); } });
this.setBounds(100, 100, 300, 200);
this.setTitle("A java calculator");
this.show();
System.out.println("End of the method
initScreen...");
}
}
And Save the next code in a new file with the name
ButtonListener.java:
import java.awt.event.*;
//---------------------------------------------
// Class
//---------------------------------------------
public class ButtonListener implements ActionListener {
//---------------------------------------------
// Constructor
//---------------------------------------------
public ButtonListener() {
}
//---------------------------------------------
// Methods
//---------------------------------------------
public void actionPerformed(ActionEvent e) {
System.out.println("Button + was
pressed...");
}
}
What happens here is that in the class Calculator the variable
buttonListener is defined as a new ButtonListener (a class that will be
defined by ourselves in the file ButtonListener.java) or in Java terms:
a new instance or new example is made of the ButtonListener class. This
instance buttonListener, is then attached to the ´+´ button with the
addActionListener(ActionListener l) method of the Button class. Verify
this method of a Button on the Java API pages:
http://java.sun.com/j2se/1.3/docs/api/index.html . We can attach
buttonListener as an action-listener because ButtonListener is
implementing the interface ActionListener.
Reviewing the code:
The class ButtonListener ´implements´ the interface ActionListener.
That means ButtonListener has properties of the ActionListener class. It
also needs to redefine the methods in ActionListener because of this
implementation. In the Constructor nothing happens, but on using (or
instantiating) the class with ´ButtonListener buttonListener= new
ButtonListener()´, an example, an instance of this class will reside in
the memory of your computer. When attached to a button and upon pressing
it, this instance will start its method actionPerformed() and a line
will be printed to the DOS/UNIX window.
Remarks:
Implementing an interface has some similarities to extending a class,
as we have seen before with the class Calculator extending a Frame.
Sometimes, it is even possible to implement an interface instead of
extending a class with the same results. I think that the matter is too
complicated and not important enough to go into at the moment, but
remember that a class can extend only one other class, and a class can
implement several interfaces.
previous lesson - back to tutorial index -
next lesson
|