I am having trouble getting this to run. It compiles just fun, and it will let you click one button but does not recognize a second button click. What am I doing wrong?

/*
Chapter 6: Telephone Keypad
Programmer: Charlene Wood
Date: April 8, 2012
Filename: TelephoneKeypad.java
Purpose: This program will create a functional telephone keypad.
*/

import java.awt.*;
import java.awt.event.*;

public class TelephoneKeypad extends Frame implements ActionListener
{
// declaring variables
public Button keys [];
public Panel keyPad;
public TextField lcd;
public Label padLabel;
public boolean foundKey;

    //construct
    public TelephoneKeypad()
    {
        lcd = new TextField(20);
        lcd.setEditable(false);
        keyPad = new Panel();
        keys = new Button[13];
        padLabel = new Label();

     //assign captions to the buttons
                for (int i=0; i<=9; i++)
                {
                    keys[i] = new Button(String.valueOf(i));
                    keys[i].addActionListener(this);
                    keys[12] = new Button("#");
                    keys[i].addActionListener(this);
                    keys[11] = new Button("0");
                    keys[i].addActionListener(this);
                    keys[10] = new Button("*");
                    keys[i].addActionListener(this);
                }

                //set frame and keypad layout to grid layout
                setBackground(Color.magenta);
                setLayout(new BorderLayout());

        keyPad.setLayout(new GridLayout(4,3,10,10));

        for (int i=1; i<=3; i++)
                        keyPad.add(keys[i]);

                    for (int i=4; i<=6; i++)
                        keyPad.add(keys[i]);

                    for (int i=7; i<=9; i++)
                        keyPad.add(keys[i]);

        keyPad.add(keys[10]);
        keyPad.add(keys[11]);
        keyPad.add(keys[12]);

add(lcd, BorderLayout.NORTH);
        add(keyPad, BorderLayout.CENTER);
        add(padLabel, BorderLayout.SOUTH);

    addWindowListener(
        new WindowAdapter()
        {
        public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        }
    );
}

public void actionPerformed(ActionEvent e)
    {
        for (int i=0; i<keys.length && !foundKey; i++)
        {
            if(e.getSource() ==keys[i]) // loop for array. Testing against actionPerformed
            {
                foundKey=true;
                switch(i)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                    case 11:

                    if(foundKey)
                    {
                        lcd.setText("");
                    }

                    lcd.setText(lcd.getText() + keys[i].getLabel());
                    break;
                }
            }
        }
}
    public static void main(String arg[])
    {
        TelephoneKeypad t = new TelephoneKeypad();
        t.setTitle("Telephone");
        t.setBounds(50,100,300,400);
        t.setVisible(true);
    }

    }

it will let you click one button but does not recognize a second button click

Try debugging the code by adding a println statement first thing the actionPerformed() method that prints out the value of the ActionEvent object passed to the method and also the values of any of the variables that the code in the listener method uses to control execution.
The print out will show you what the computer sees (it sees all the button clicks) so you can see where your code is going wrong.

Edited 4 Years Ago by NormR1

This article has been dead for over six months. Start a new discussion instead.