Java Tutorial - lesson six


Lesson Six: Making the ´+´ button work.

previous lesson - back to tutorial index - next lesson


This is all very nice. We have the closable window, the components in it, but we can´t do anything with them yet. In Java, you can make components listen to actions. I.e., you can make a window ´listen´ to mouse-movement and you can make buttons listen to mouse-clicks. The next sample of code is to see how to attach a mouse-click-listener to a button. It is actually a bad example, but it works. A better example will follow in the next lesson.

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() {
        buttonAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                result.setText("Button ´+´ pressed");
                System.out.println("Button + was pressed...");
            }
        });
    }
//---------------------------------------------
    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.show();
        System.out.println("End of the method initScreen...");
    }
}

 
Reviewing the code:

Not much has changed in the code. When we are setting up the window with initScreen(), we give the window a title with one of the methods of the class Frame. Check out this class and its methods on http://java.sun.com/j2se/1.3/docs/api/index.html . In our own new method addButtonListeners() a mouse-click-listener is attached to the ´+´ button. From the code, it is not very clear what happens exactly, but it works. It is the easy way out. A much better and more illustrative example follows in the next lesson. Note that when running your code, and upon pressing the ´+´ button, not only the text in the label changes with one of the methods of the Label class, but also a text string is printed in your DOS/UNIX window. We have seen this before, and I just wanted to point out that it is a handy way of debugging your code, of checking if something works or not. Press the button ´+´ a couple of times.

previous lesson - back to tutorial index - next lesson