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