Welcome to Dream.In.Code
Become a Java Expert!

Join 149,609 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,877 people online right now. Registration is fast and FREE... Join Now!




PhonePad

 
Reply to this topicStart new topic

PhonePad

enigma65
20 Aug, 2007 - 11:10 AM
Post #1

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 1


My Contributions
This is my assignment guys and i have some serius problems here..
I have to make a phonepad and i managed to make only the number
buttons but there are some other buttons that it impossible for me
to make them work..

These are the additional buttons that i have to add to the program bellow
but i can not manage to make them.

• A button labeled 'C' used to delete the latest digit entered
• A display showing the keyed-in number (which grows digit by digit as the user keys the number in)
• A button labeled "Dial" used to initiate a call
• A button labeled "Hang up" used to terminate the call
• An updating display monitoring the time taken for the call

If someone have any idea help me please...its urgent...
Thanx a lot...



CODE
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PhonePad implements ActionListener {

    private JFrame frame;
    private JPanel panel;
    private JTextField textField;

    public PhonePad() {
    }

    public void createWindow() {

        System.out.println("Initializing Window");

        frame = new JFrame("London Telepad");

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

        panel = new JPanel();
        panel.setLayout(new GridLayout(4, 3));
        
      
        JButton butt1 = new JButton("1");
        JButton butt2 = new JButton("2");
        JButton butt3 = new JButton("3");
        JButton butt4 = new JButton("4");
        JButton butt5 = new JButton("5");
        JButton butt6 = new JButton("6");
        JButton butt7 = new JButton("7");
        JButton butt8 = new JButton("8");
        JButton butt9 = new JButton("9" );
        JButton buttstar = new JButton("*");
        JButton butt0 = new JButton("0");
        JButton butthash = new JButton("#");

        buttc.addActionListener(this);
        butt0.addActionListener(this);
        butt1.addActionListener(this);
        butt7.addActionListener(this);
        butt8.addActionListener(this);
        
        
        panel.add(butt1);
        panel.add(butt2);
        panel.add(butt3);
        panel.add(butt4);
        panel.add(butt5);
        panel.add(butt6);
        panel.add(butt7);
        panel.add(butt8);
        panel.add(butt9);
        panel.add(buttstar);
        panel.add(butt0);
        panel.add(butthash);

        frame.getContentPane().add(panel);

        frame.setSize(200, 300);
        frame.setVisible(true);

        System.out.println("Window Initialized");
    }

    // method needed for testing with Robot class

    public JFrame getFrame() {

        return frame;
    }

    public static void main(String args[]) {

        PhonePad app = new PhonePad();
        app.createWindow();
    }

    public void actionPerformed(ActionEvent e) {

        // Display text of button on shell window
        System.out.print(e.getActionCommand());
    }
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: PhonePad
20 Aug, 2007 - 02:33 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You sure do have a lot of things to conquer here. Below I have provided some changes to your code that will give you the keyed in number text field and the delete mechanism... it should show you how to do things and get it working. With it you can implement what you need to do with regards to the keying in of digits and deleting them. That will answer your first two parts for you.

CODE

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PhonePad implements ActionListener {

    private JFrame frame;
    private JPanel panel;
    private JTextField textField = new JTextField();

    public PhonePad() {
    }

    public void createWindow() {

        System.out.println("Initializing Window");

        frame = new JFrame("London Telepad");

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

        // Lets set a layout for the frame.
        frame.setLayout(new BorderLayout());

        // At the top lets add our textField.
        // We align the numbers to the right and then add it to the top panel.
        textField.setHorizontalAlignment(JTextField.RIGHT);
        frame.add(textField, BorderLayout.NORTH);

        panel = new JPanel();
        panel.setLayout(new GridLayout(4, 3));
        
        
        JButton butt1 = new JButton("1");
        JButton butt2 = new JButton("2");
        JButton butt3 = new JButton("3");
        JButton butt4 = new JButton("4");
        JButton butt5 = new JButton("5");
        JButton butt6 = new JButton("6");
        JButton butt7 = new JButton("7");
        JButton butt8 = new JButton("8");
        JButton butt9 = new JButton("C" );
        JButton buttstar = new JButton("*");
        JButton butt0 = new JButton("0");
        JButton butthash = new JButton("#");


        butt0.addActionListener(this);
        butt1.addActionListener(this);
        butt2.addActionListener(this);
        butt3.addActionListener(this);
        butt4.addActionListener(this);
        butt5.addActionListener(this);
        butt6.addActionListener(this);
        butt7.addActionListener(this);
        butt8.addActionListener(this);
        butt9.addActionListener(this);
        
        
        panel.add(butt1);
        panel.add(butt2);
        panel.add(butt3);
        panel.add(butt4);
        panel.add(butt5);
        panel.add(butt6);
        panel.add(butt7);
        panel.add(butt8);
        panel.add(butt9);
        panel.add(buttstar);
        panel.add(butt0);
        panel.add(butthash);

    
        // Lets put our number pad in the middle
        frame.getContentPane().add(panel, BorderLayout.CENTER);

        frame.setSize(200, 300);
        frame.setVisible(true);

        System.out.println("Window Initialized");
    }

    // method needed for testing with Robot class

    public JFrame getFrame() {

        return frame;
    }

    public static void main(String args[]) {

        PhonePad app = new PhonePad();
        app.createWindow();
    }

    public void actionPerformed(ActionEvent e) {

        // Get text from textfield
        String keyedin = textField.getText();

        // If this was issued by a key that reads "Delete" and there is values in the textfiled
        // then remove the last digit. otherwise add to the field as normal.

        if (e.getActionCommand().equals("C")) {
                if (keyedin.length() > 0) {
                        textField.setText(keyedin.substring(0,keyedin.length() - 1));
                }
        }
        else {
        
                textField.setText(textField.getText() + e.getActionCommand());
        }
    }
}



Now the dial and hangup buttons are basically going to start and stop a timer. At least this would be the easiest way to do it. You will need to lookup how to do timers and create a timer when a person presses the dial button and stop it when they hang up. Each iteration of the timer you will simply output a number (which you increment) to a label or textfield. The trick with it is that you will have to know that when you hit sixty seconds you have to increment the minutes number and reset your seconds. Same with minutes to hours.

Your hangup and dial buttons will use the same ActionPerformed() method you already have defined there, you just have to check for a hangup and dial getActionCommand() call.

Work on this stuff and when you get stuck again, feel free to ask some more.

After all, Dream.in.Code.net kung-fu is the best! decap.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:09AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month