Keypad.java display problem

Keypad Program will not display in application

Page 1 of 1

2 Replies - 8600 Views - Last Post: 12 December 2006 - 04:10 PM Rate Topic: -----

#1 twigg_girl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-November 06

Keypad.java display problem

Posted 11 December 2006 - 10:35 PM

Hi, this is a homework assignment that I need some help with. I created a Keypad application in Java. It was a morph from a calculator homework assignment. I have the app written and it compiles but when I run it I can not get the app to display. I'm not sure what I'm doing wrong but I assume it has something to do with the set frame properties at the end of the program. My reason for assuming this is because if I apply the text from the calculator assignment it displays something (although it calls the calculator program display).

Thanks for the help,




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

public class Phonepad extends Frame implements ActionListener
{
   private Button keys[];
   private Panel keypad;
   private TextField lcd;
   private boolean first;
   private boolean foundKey;
   private boolean clearText;


   public Phonepad()
   {
		// 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
		lcd = new TextField(20);
	   		lcd.setEditable(false);
		keypad = new Panel();
		keys = new Button[12];
		clearText = true;


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

		keys[11] = new Button("*");
		keys[12] = new Button("#");


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

		for (int i=1; i<=4; 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<=10; i++) // 7, 8, 9

		keypad.add(keys[11]); // *

		keypad.add(keys[0]); // 0

		keypad.add(keys[12]); // #

		add(lcd, BorderLayout.NORTH);
		add(keypad, BorderLayout.CENTER);


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

   } // end of constructor method

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<keys.length && !foundKey; i++)
		  {
	   		if(e.getSource() == keys[i])
	   		{
		 		foundKey=true;
		 		switch(i)
		 		{
					// number and symbol buttons
					case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11:
						if(clearText)
						{
							lcd.setText("");
							clearText = false; // !clearText;
						}
						lcd.setText(lcd.getText() + keys[i].getLabel());
						break;

				 } // end of switch(i)
			  } // end of if
	   	} // end of for
   } // end of actionPerformed

   public static void main(String args[])
   {
	   	// set frame properties
		Phonepad f = new Phonepad();
		  f.setTitle("Phonepad Application");
		  f.setBounds(200,200,300,300);
		f.setVisible(true);


   } // end of main
} // end of class
 


Is This A Good Question/Topic? 0
  • +

Replies To: Keypad.java display problem

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Keypad.java display problem

Posted 12 December 2006 - 06:00 AM

you declare your keys array
		keys = new Button[12];


which will have elements 0 to 11. However you then access element 12
	  keys[11] = new Button("*");
	   keys[12] = new Button("#");


which will give a ArrayIndexOutOfBoundsException

you have a similar problem with keypad

fix the above errors and the keypad should display

This post has been edited by horace: 12 December 2006 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 twigg_girl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-November 06

Re: Keypad.java display problem

Posted 12 December 2006 - 04:10 PM

Thank You, that did it.

I discovered that I have another problem with the LCD part of my app but I haven't even touched trying to work it out myself. I'll be back to the post if I can't manage it on my own.

Again, Thanks so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1