5 Replies - 1857 Views - Last Post: 18 September 2011 - 09:50 AM Rate Topic: -----

#1 nielatjie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-August 11

Creating a Telephone Keypad Application not running right

Posted 18 September 2011 - 07:47 AM

Helo I did this assignment just now! it compiles and run but when I type in more than one number it only shows one number! I don't know why it is not working right!

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

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

	//construct
	public Telephone()
	{
		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[])
{
	Telephone t = new Telephone();
	t.setTitle("Telephone");
	t.setBounds(50,100,300,400);
	t.setVisible(true);
}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Creating a Telephone Keypad Application not running right

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: Creating a Telephone Keypad Application not running right

Posted 18 September 2011 - 08:01 AM

Your 'for' statement at Line 70 only works the first time. Fix that.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,617
  • Joined: 27-December 08

Re: Creating a Telephone Keypad Application not running right

Posted 18 September 2011 - 09:07 AM

Also, you should really use more modern Swing Components like JFrame, JLabel, JPanel, JButton, etc., over their AWT counterparts. AWT is a dead technology. Let it stay dead.
Was This Post Helpful? 1
  • +
  • -

#4 nielatjie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-August 11

Re: Creating a Telephone Keypad Application not running right

Posted 18 September 2011 - 09:40 AM

View Postmacosxnerd101, on 18 September 2011 - 09:07 AM, said:

Also, you should really use more modern Swing Components like JFrame, JLabel, JPanel, JButton, etc., over their AWT counterparts. AWT is a dead technology. Let it stay dead.

That is what my textbook teaches me :(
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,617
  • Joined: 27-December 08

Re: Creating a Telephone Keypad Application not running right

Posted 18 September 2011 - 09:44 AM

I'm guessing your textbook is also more than a few years old. It's pretty much out of date. Check Oracle's website if you don't believe me. :)
Was This Post Helpful? 0
  • +
  • -

#6 nielatjie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-August 11

Re: Creating a Telephone Keypad Application not running right

Posted 18 September 2011 - 09:50 AM

I believe you! I hate this book!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1