Jump to content

Need Help Creating Telephone

- - - - -

  • Please log in to reply
19 replies to this topic

#1
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hello All,

Can someone guide me in the right direction to getting the action for the buttons down and also help me print the actions to the status area I have created?

package telephone;
import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

/**
 *
 * @author A.N.T
 */
public class telephoneDesign implements ActionListener
{
    /*
     * Variables Used Throughout Project
     */
    private JFrame telephone = new JFrame("Ant's Telephone");

    private JPanel telephonepanel = new JPanel();

    private JLabel printarea = new JLabel();
    private JButton number0 = new JButton("0");
    private JButton number1 = new JButton("1");
    private JButton number2 = new JButton("2");
    private JButton number3 = new JButton("3");
    private JButton number4 = new JButton("4");
    private JButton number5 = new JButton("5");
    private JButton number6 = new JButton("6");
    private JButton number7 = new JButton("7");
    private JButton number8 = new JButton("8");
    private JButton number9 = new JButton("9");
    private JButton star = new JButton("*");
    private JButton pound = new JButton("#");

    private JButton call = new JButton("Call");
    private JButton endcall = new JButton("End Call");

    private JButton redial = new JButton("Redial");

    /*
     * Constructor that will design look.
     */
    public telephoneDesign()
    {
        /*
         * Creating the window for the project.
         */
        telephone.setSize(300, 300);
        telephone.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        telephonepanel.setLayout(new GridLayout(6,3));

        telephone.add(telephonepanel);

        /*
         * Adds the numbers & other buttons to phone.
         */
        telephonepanel.add(number1);
        telephonepanel.add(number2);
        telephonepanel.add(number3);
        telephonepanel.add(number4);
        telephonepanel.add(number5);
        telephonepanel.add(number6);
        telephonepanel.add(number7);
        telephonepanel.add(number8);
        telephonepanel.add(number9);
        telephonepanel.add(star);
        telephonepanel.add(number0);
        telephonepanel.add(pound);
        telephonepanel.add(call);
        telephonepanel.add(redial);
        telephonepanel.add(endcall);

        /*
         * For Status Area
         */
        JPanel toparea = new JPanel();
        telephone.add(toparea, BorderLayout.SOUTH);

        JLabel status = new JLabel();
        toparea.add(status, BorderLayout.CENTER);

        telephone.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}

Edited by Alexander, 02 November 2010 - 08:27 PM.
Added [code] tags


#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Check out interfaces MouseListener (Java 2 Platform SE v1.4.2) and MouseMotionListener (Java 2 Platform SE v1.4.2)

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
With a bunch of buttons you don't need mouselisteners/mousemotionlisteners. The actionperformed is enough. The first thing you want to do is, for every button do
button.addActionListener(this)
Then, when the button is pressed it will use the public void actionPerformed(ActionEvent e) of this class.
The easiest way to print it to a label is:

public void actionPerformed(ActionEvent e)

{

  JButton btnPressed = (JButton) e.getSource();

  status.setText(status.getText() + btnPressed.getText());

}


You just need to add some checks in there so you don't add "end call" or "redial" there. Or you can assign another actionlistener for those buttons.

#4
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Thank you...About to try it now...

#5
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Ok I was able to implement that code to make it where I could click the numbers with the mouse. How would I implement the keypad as well?

#6
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Thanks for the information. I was able to use that for all the buttons. Now I was wondering how can I input some code to make the keypad work as well when I press the numbers and symbols.

#7
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
An example from an application of mine (class extending JFrame):

protected void processKeyEvent(KeyEvent e){
		int keyCode = e.getKeyCode();
		int keyId = e.getID();
		
		if(keyCode == KeyEvent.VK_SPACE && keyId == KeyEvent.KEY_PRESSED){
			if the user pressed space do something
		}

                other if/elses here
                .....
		
	}


#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
The easiest that comes up to me would be to change the JLabel into a JTextfield so you can just type in it :D
Otherwise you'll have to add a KeyListener to the JFrame.
Keylistener requires 3 methods to be made:

    public void keyTyped(KeyEvent e) {

	

    }


    public void keyPressed(KeyEvent e) {

	

    }


    public void keyReleased(KeyEvent e) {


    }

You can leave 2 empty and only fill in keyTyped.
To get the text of the key that's typed. Like "A", "B", "F1", "1",.. You can use the static method getKeyText of the clas KeyEvent.
This has an integer keycode as parameter which you can get from the KeyEvent e you get in:
String text = KeyEvent.getKeyText(e.getKeyCode());
This will also work with all text keys, so you would need to check if the text is either a number or a # or *. That may be a lot of work...
There is a check you can do to know if the key was pressed on a numpad leaving you with only eliminating the / - + .
And perhaps it'll also register the enter and numlock button itself. HOWEVER i'm not sure if those keys will fire the keyTyped event.
The check to look for numpad keys:

if (event.getKeyLocation() != KeyEvent.KEY_LOCATION_NUMPAD)

Unfortunately numpad has no # so you'll need an else at the numpad-if to check if the user pressed # maybe.
Good luck =)

#9
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Thanks for the information. I am about to try and use this and see if I can get it to work.

#10
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
I wrote this code
        number0.addKeyListener(new KeyListener()
        {
            public void keyTyped(KeyEvent num)
            {
                String numb = KeyEvent.getKeyText(num.getKeyCode());
                status.setText(numb);
            }

            public void keyPressed(KeyEvent num)
            {

            }

            public void keyReleased(KeyEvent num)
            {

            }
        });

But still wasn't able to get the keys to work. Any suggestions of what I did wrong?

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Try adding them to the frame
telephone.addKeyListener...


#12
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
I tried using the frame but still no reaction in the status area.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users