Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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");
            }
        }
    
    }
    
    
        
    
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •