Skip to main content

ActionListener for Keypad.java not working

2 replies [Last post]
jlnickymaster
Offline
Joined: 2007-05-02
Points: 0

I compile correctly but cannot understand why when I push the keys nothing is
happening on my program. I have an ActionListner and an event built into this Keypad.
But, it does not seem to be working. Can you take and look and point me in the right
direction?

[code]
// Java Document

/*Build and execute a program for a security keypad. The valid security codes are:

* 1645–1689: Technicians.

* 8345: Custodians.

* 55875: Special Services.

* 999898: Chief Scientist.

* 1000006–1000008: Scientists.

As an added security measure, any code less than 1000 is treated as a panic code and
the program should output a red WARNING display. Access is denied and security is
notified immediately. Once a code is entered, access is either granted or denied. All
interactions with the keypad are written to the window below the keypad with the date
and time the interaction occurred.*/

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class Keypad extends Frame implements ActionListener
{
private Button keys[];
private Panel keypad;
private TextField lcd;
private Label lcdDisplay;
private boolean first;
private boolean foundKey;
private boolean clearText;
private TextArea outputLog;
private Label DisplError;
boolean OperatorKey; //the 'flag' that will signify whether or not any operator button has been pressed
double Result;

public Keypad()
{
// create an instance of the menu
MenuBar mnuBar = new MenuBar();
setMenuBar(mnuBar);

// construct and populate the File menu
Menu mnuFile = new Menu("File", true);
mnuBar.add(mnuFile);
MenuItem mnuFileExit = new MenuItem("Exit");
mnuFile.add(mnuFileExit);

// add the ActionListener to each menu item
mnuFileExit.addActionListener(this);

// assign an ActionCommand to each menu item
mnuFileExit.setActionCommand("Exit");

// construct components and initialize beginning values
Label lcdDisplay = new Label("0",Label.RIGHT);
lcd = new TextField(20);
lcd.setEditable(false);
keypad = new Panel();
keys = new Button[12];
clearText = true;
Label DisplError = new Label("Enter Your Password");
TextArea outputLog = new TextArea(7,32);
OperatorKey = true;

// construct and assign captions to the Buttons
for (int i=0; i<=9; i++)
keys[i] = new Button(String.valueOf(i));

keys[10] = new Button("Clear");
keys[11] = new Button("Enter");

// set Frame and keypad layout to grid layout
setLayout(new BorderLayout());
keypad.setLayout(new GridLayout(4,3,2,2));

for (int i=1; i<=3; i++) // 1, 2, 3
keypad.add(keys[i]);

for (int i=4; i<=6; i++) // 4, 5, 6
keypad.add(keys[i]);

for (int i=7; i<=9; i++) // 7, 8, 9
keypad.add(keys[i]);

keypad.add(keys[10]); // Clear
keypad.add(keys[0]); // 0
keypad.add(keys[11]); // Enter

add(lcd, BorderLayout.NORTH);
add(keypad, BorderLayout.CENTER);
add(outputLog, BorderLayout.SOUTH);
add(DisplError, BorderLayout.EAST);

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

} // end of constructor method

//methods

/* This method is called whenever anything needs to be displayed
* in the error message field at the bottom of the calculator,
* accepting a String as an argument
*/
void DisplayError(String err_msg)
{
/* calls the setText method of the Label DisplError, sending
* whatever string it received initially
*/
DisplError.setText(err_msg);
DisplError.setForeground(Color.red);
}

/* This method is called whenever a numeric button (0-9) is pushed. */
public void NumericButton(int i)
{
DisplayError(" "); //Clears the error message field

/* Declares a String called Display that will initialize to whatever
* is currently displayed in the lcdDisplay of the calculator
*/
String Display = lcdDisplay.getText();
//The number just entered is appended to the string currently displayed
Display = Display + String.valueOf(i);
}

public void actionPerformed(ActionEvent e)
{
//test for menu item clicks
String arg = e.getActionCommand();
if (arg == "Exit")
System.exit(0);

// test for button clicks
foundKey = false;

// search for the clicked key
for (int i=0; i

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18
Points: 0

[i]Captain's Log, Stardate 6051: had trouble sleeping last night... my hiatal hernia is acting up. The ship is drafty and damp; I complain, but... [b]nobody listens[/b].[/i]

You will have to register your action listener with the needed components. Don't worry, ace, happens to the best of us. ;)

jlnickymaster
Offline
Joined: 2007-05-02
Points: 0

OH! need more coffee!

I went ahead and put
keys[1].addActionListener( this );
keys[2].addActionListener( this );
keys[3].addActionListener(this);
keys[4].addActionListener(this);
etc.

into the appropriate area.

thanks