Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-27-2011, 09:58 PM   PM User | #1
Sghtblinder
New to the CF scene

 
Join Date: Apr 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Sghtblinder is an unknown quantity at this point
cellphone GUI help

Howdy guys.

i am working on a homework assignment that requires us to create somewhat of a working cellphone.

i have my code written out and it works partially. the textpanel is supposed to display the numbers pressed like this xxx-xxxx for a local number called and x-xxx-xxx-xxxx for a long distance called, and shows up as invalid number on the screen when the numbers entered do not match that criteria.

mine however only shows one number on the screen when i press a button and so always shows up as an invalid number and i do not know what i have done wrong.

i will post my entire code here in a second minus the main class because all it has is the new CellPhone() tag to run the program.

i will warn you. i am a big newb on coding and so my code is clunky and i apologize right now for that, but i have to start somewhere and i know i will get better with practice, but if someone would be able to take a look at my code and point in me in the right direction i would be much appreciated.

here is my code

Code:

package cellphone;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Color;


/**
 *
 * @author Leslie
 */
public class CellPhone extends JFrame {
    private final String WINDOW_TITLE = "CellPhone";
    private static final Dimension SIZE = new Dimension(200,300);
    protected String input;
    protected String localText;
    protected String longText;


    //Creates the controls for our GUI
    protected JPanel textPanel = new JPanel();
    protected JPanel buttonPanel = new JPanel();
    protected JTextField display = new JTextField(20);

    //creates the buttons for the cellphone
    protected JButton sendButton = new JButton("Send");
    protected JButton endButton = new JButton("End");
    protected JButton Button1 = new JButton("1");
    protected JButton Button2 = new JButton("2");
    protected JButton Button3 = new JButton("3");
    protected JButton Button4 = new JButton("4");
    protected JButton Button5 = new JButton("5");
    protected JButton Button6 = new JButton("6");
    protected JButton Button7 = new JButton("7");
    protected JButton Button8 = new JButton("8");
    protected JButton Button9 = new JButton("9");
    protected JButton Button0 = new JButton("0");


    public CellPhone(){
        setTitle(WINDOW_TITLE);
        setSize(SIZE);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildTextPanel();
        buildButtonPanel();
        setLayout(new BorderLayout());
        add(textPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.CENTER);
        clearDisplay();
        pack();
        setVisible(true);

    }

    /**
     *
     * @param buildTextPanel builds our text Panel for our GUI
     *
     */
    public void buildTextPanel(){
        textPanel.setLayout(new BorderLayout() );
        textPanel.add(display, BorderLayout.NORTH);
        textPanel.setBackground(Color.BLUE);
    }

    public void buildButtonPanel(){
        buttonPanel.setLayout(new GridLayout(4,3));
        buttonPanel.add(Button1);
        buttonPanel.add(Button2);
        buttonPanel.add(Button3);
        buttonPanel.add(Button4);
        buttonPanel.add(Button5);
        buttonPanel.add(Button6);
        buttonPanel.add(Button7);
        buttonPanel.add(Button8);
        buttonPanel.add(Button9);
        buttonPanel.add(endButton);
        buttonPanel.add(Button0);
        buttonPanel.add(sendButton);
        sendButton.addActionListener(new SendButtonListener());
        endButton.addActionListener((ActionListener) new EndButtonListener());

          //creates innerListeners for all buttons
    InnerListener listener = new InnerListener();
    Button1.addActionListener(listener);
    Button2.addActionListener(listener);
    Button3.addActionListener(listener);
    Button4.addActionListener(listener);
    Button5.addActionListener(listener);
    Button6.addActionListener(listener);
    Button7.addActionListener(listener);
    Button8.addActionListener(listener);
    Button9.addActionListener(listener);
    Button0.addActionListener(listener);
    

    }

    public String setLocalText(){
        display.setText("Calling: " + input + input + input + "-" + input +
                input + input + input);
        return localText;

    }

    public String setLongText(){
        display.setText("Calling: " + input + "-" + input + input + input + "-"
                + input + input + input + "-" + input + input + input + input);
        return longText;
    }

    public void clearDisplay(){
        display.setText(" ");
    }

    public void sendCalling(){
            if(input.equals(setLocalText())){
            if(input.equals(setLongText())){
                sendCalling();
            }

        }
        else{
            display.setText("Invalid Number");
        }
    


    }

    public void endCalling(){
        display.setText("Call Ended");
        clearDisplay();

    }
    private class SendButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e){
        input = display.getText();
        sendCalling();
        sendButton.setText("Send");
    }




}
    private class EndButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            endCalling();
            clearDisplay();
            endButton.setText("End");
        }
    }
    private class InnerListener implements ActionListener {
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == Button1){
            display.setText("1");
        }
        else if(e.getSource() == Button2){
            display.setText("2");
        }
        else if(e.getSource() == Button3){
            display.setText("3");
        }
        else if(e.getSource() == Button4){
            display.setText("4");
        }
        else if(e.getSource() == Button5){
            display.setText("5");
        }
        else if(e.getSource() == Button6){
            display.setText("6");
        }
        else if(e.getSource() == Button7){
            display.setText("7");
        }
        else if(e.getSource() == Button8){
            display.setText("8");
        }
        else if(e.getSource() == Button9){
            display.setText("9");
        }
        else if(e.getSource() == Button0){
            display.setText("0");
        }
    }

}


    

}
Sghtblinder is offline   Reply With Quote
Old 04-27-2011, 10:34 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
display.setText within the innerlistener will overwrite the previous data within the JTextField. You can append by using a display.setText in combination with display.getText and concatenating the strings. Alternatively, you can extend the JTextField class and override the setText method to append instead of overwrite.

You'll need to work on the displayLong/shortText methods as well. The input itself would be the full string each time once you execute the SendButtonListener, so it would appear as a horrendously long number. I myself am lazy, what I would do is take input as an ArrayList, keep pushing numbers into it, then output the numbers. Super easy, you can simply count the number of items in the list, and display as desired. String route is similar in how its done, but now you need to substring it out in order to add the -'s. I myself much prefer collections or arrays over strings (though you can of course use a char[] from the string as well).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:48 PM.


Advertisement
Log in to turn off these ads.