hi,
i suppose to make simulation to cellphone. when press any digit ,it should appears on the display textfield to form a complete number. and when press the send button a message with"redialling+ the gigits pressed" appears on the textfield also when press redial button a message with " redialling+ the digits being pressed" appears on the textfield. but i have many errors as the following:

CellPhone.java:228: <identifier> expected
Button1.addActionListener(listener);
^
CellPhone.java:228: <identifier> expected
Button1.addActionListener(listener);
^
CellPhone.java:229: <identifier> expected
Button2.addActionListener(listener);
^
CellPhone.java:229: <identifier> expected
Button2.addActionListener(listener);
^
CellPhone.java:230: <identifier> expected
Button3.addActionListener(listener);
^
CellPhone.java:230: <identifier> expected
Button3.addActionListener(listener);
^
CellPhone.java:231: <identifier> expected
Button4.addActionListener(listener);
^
CellPhone.java:231: <identifier> expected
Button4.addActionListener(listener);
^
CellPhone.java:232: <identifier> expected
Button5.addActionListener(listener);
^
CellPhone.java:232: <identifier> expected
Button5.addActionListener(listener);
^
CellPhone.java:233: <identifier> expected
Button6.addActionListener(listener);
^
CellPhone.java:233: <identifier> expected
Button6.addActionListener(listener);
^
CellPhone.java:234: <identifier> expected
Button7.addActionListener(listener);
^
CellPhone.java:234: <identifier> expected
Button7.addActionListener(listener);
^
CellPhone.java:235: <identifier> expected
Button8.addActionListener(listener);
^
CellPhone.java:235: <identifier> expected
Button8.addActionListener(listener);
^
CellPhone.java:236: <identifier> expected
Button9.addActionListener(listener);
^
CellPhone.java:236: <identifier> expected
Button9.addActionListener(listener);
^
CellPhone.java:237: <identifier> expected
Button0.addActionListener(listener);
^
CellPhone.java:237: <identifier> expected
Button0.addActionListener(listener);
^
CellPhone.java:238: <identifier> expected
astrButton.addActionListener(listener);
^
CellPhone.java:238: <identifier> expected
astrButton.addActionListener(listener);
^
CellPhone.java:239: <identifier> expected
boundButton.addActionListener(listener);
^
CellPhone.java:239: <identifier> expected
boundButton.addActionListener(listener);
^
CellPhone.java:279: reached end of file while parsing
}
^
CellPhone.java:286: reached end of file while parsing

/**
  This program simulates a cellphone.

*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class CellPhone extends JFrame {

	private final String WINDOW_TITLE = "CellPhone Simulation";
	private final int WINDOW_WIDTH = 200; 
	private final int WINDOW_HEIGHT = 300; 

	

	// The following named controls will appear in our GUI

    private JPanel textPanel = new JPanel();
    private JPanel digitPanel = new JPanel();
    private JPanel buttonPanel = new JPanel();

    
    private JLabel displayLabel = new JLabel ("                     NOKIA ");

    private JTextField displayTextField = new JTextField(20);
    
    private JButton sendButton = new JButton("Send");
    private JButton clearButton = new JButton("Clear");
    private JButton endButton = new JButton("End ");
    private JButton redialButton = new JButton("Redial");
    private JButton Button1 = new JButton("1");
    private JButton Button2 = new JButton("2");
    private JButton Button3 = new JButton("3");
    private JButton Button4 = new JButton("4");
    private JButton Button5 = new JButton("5");
    private JButton Button6 = new JButton("6");
    private JButton Button7 = new JButton("7");
    private JButton Button8 = new JButton("8");
    private JButton Button9 = new JButton("9");
    private JButton Button0 = new JButton("0");
    private JButton astrButton = new JButton("*");
    private JButton boundButton = new JButton("#");
    
/** We'll use DecimalFormat later to convert the result to a String with two decimal places.
 */

    private DecimalFormat df2 = new DecimalFormat("#,###.00");

    /**
      The main method creates an instance of the cellphone class, which displays
      its window on the screen.
    */

    public static void main(String[] args) {
        new CellPhone();
    }

/**
    The constructor for this application will initialize the JFrame
    by setting the window title bar, giving the frame a size,
    initializing all the controls that appear on the frame, and
    then make it visible.
 */
    public CellPhone()
    {

        setTitle(WINDOW_TITLE);
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildTextPanel();
        buildButtonPanel();
        buildDigitPanel();
        setLayout(new BorderLayout());
        add(textPanel,BorderLayout.NORTH);
        add(buttonPanel,BorderLayout.CENTER);
        add(digitPanel,BorderLayout.SOUTH);
        clearDisplay();
        setVisible(true);

    }

/**
    The buildTextPanel method creates a panel to hold the label and the textfield.
 */
	public void buildTextPanel () {
		textPanel.setLayout(new BorderLayout());
		textPanel.add(displayLabel,BorderLayout.NORTH);
		textPanel.add(displayTextField,BorderLayout.CENTER);
                textPanel.setBackground(Color.RED);
	}

/**
      The buildButtonPanel method creates a panel to hold the send,clear,end and redial buttons.
 */
	public void buildButtonPanel () {
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.add(sendButton);
		buttonPanel.add(clearButton);
		buttonPanel.add(endButton);
                buttonPanel.add(redialButton);
		sendButton.addActionListener(new SendButtonListener());
		clearButton.addActionListener(new ClearButtonListener());
		endButton.addActionListener(new EndButtonListener());
                redialButton.addActionListener(new RedialButtonListener());
                buttonPanel.setBackground(Color.RED);
	}

/**
      The buildDigitPanel method creates a panel to hold the digit buttons.
 */
	public void buildDigitPanel () {
		digitPanel.setLayout(new GridLayout(3,4));
		digitPanel.add(Button1);
		digitPanel.add(Button2);
                digitPanel.add(Button3);
                digitPanel.add(Button4);
                digitPanel.add(Button5);
                digitPanel.add(Button6);
                digitPanel.add(Button7);
                digitPanel.add(Button8);
                digitPanel.add(Button9);
                digitPanel.add(Button0);
                digitPanel.add(astrButton);
                digitPanel.add(boundButton);
	}



/**
      SendButtonListener is an action listener class for
      the Send button.
 */

	private class SendButtonListener implements ActionListener {
/**
      The actionPerformed method executes when the user clicks on the send button.
      @param e The event object.
 */

      	public void actionPerformed(ActionEvent e) {

	       	sendCalling();
		}
	}

/**
     ClearButtonListener is an action listener class for
      the Clear button.
 */

	private class ClearButtonListener implements ActionListener {

/**
     The actionPerformed method executes when the user clicks on the clear button.
      @param e The event object.
 */

      	public void actionPerformed(ActionEvent e) {

	       	clearDisplay();
		}
	}

/**
    EndButtonListener is an action listener class for the Exit button.
 */

	private class EndButtonListener implements ActionListener {
/**
    The actionPerformed method executes when the user
     clicks on the end button.
     @param e The event object.
 */

      	public void actionPerformed(ActionEvent e) {

	       	endCalling();
		}
	}

        private class RedialButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
                 redialCalling();
                 }
        }

   	
/**
    The clearDisplay method will clear the digit,send,end and redial fields.
 */

   	public void clearDisplay() {
		displayTextField.setText(" ");
		
	}
   	
/**
    The sendCalling method will dislay a message when calling.
 */
        public void sendCalling() {
		displayTextField.setText("Calling 000-000-0000");
		
	}
/**
    The endCalling method will dislay a message when call is ended.
 */
        public void endCalling() {
		displayTextField.setText("Call Ended");
		
	}
/**
    The redialCalling method will dislay a message when number is redialing.
 */
        public void redialCalling() {
		displayTextField.setText("Redialing 000-000-0000");
		
	}

/** create innerlistener to all buttons, then add all buttons to 
    this listener. 
 */
        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);
        astrButton.addActionListener(listener);
        boundButton.addActionListener(listener);
        
        private class InnerListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
                 if(e.getSource()==Button1) {
                    displayTextField.setText("1"); 
                  }
                   else if (e.getSource()==Button2) {
                     displayTextField.setText("2"); 
                   }
                   else if (e.getSource()==Button3) {
                     displayTextField.setText("3"); 
                   }
                   else if (e.getSource()==Button4) {
                     displayTextField.setText("4"); 
                   }
                   else if (e.getSource()==Button5) {
                     displayTextField.setText("5"); 
                   }
                   else if (e.getSource()==Button6) {
                     displayTextField.setText("6"); 
                   }
                   else if (e.getSource()==Button7) {
                     displayTextField.setText("7"); 
                   }
                   else if (e.getSource()==Button8) {
                     displayTextField.setText("8"); 
                   }
                   else if (e.getSource()==Button9) {
                     displayTextField.setText("9"); 
                   }
                   else if (e.getSource()==Button0) {
                     displayTextField.setText("0"); 
                   }
                   else if (e.getSource()==astrButton) {
                     displayTextField.setText("*"); 
                   }
                   else if (e.getSource()==boundButton) {
                     displayTextField.setText("#"); 
                   }

Recommended Answers

All 3 Replies

The following lines should be inside a method and not placed directly inside a class, you may place only member declarations / initialisations / methods or static blocks of code directly there.

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);
        astrButton.addActionListener(listener);
        boundButton.addActionListener(listener);

ya, i make it
but i need to make all the digits being pressed appear on the textfield with the statement for example" calling 345-675-4986" when press SEND button. Additionally i need to erase what ever is on the phone display screen and then display a message like (Redialing 276-5674-9853) when pressing REDIAL button ??

/**
  This program simulates a cellphone.

 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class CellPhone extends JFrame {

	private final String WINDOW_TITLE = "CellPhone Simulation";
	private final int WINDOW_WIDTH = 200; 
	private final int WINDOW_HEIGHT = 300; 
        String input;
	

	// The following named controls will appear in our GUI

    private JPanel textPanel = new JPanel();
    private JPanel digitPanel = new JPanel();
    private JPanel buttonPanel = new JPanel();

    
    private JLabel displayLabel = new JLabel ("                     NOKIA ");

    private JTextField displayTextField = new JTextField(20);
    
    private JButton sendButton = new JButton("Send");
    private JButton clearButton = new JButton("Clear");
    private JButton endButton = new JButton(" End ");
    private JButton redialButton = new JButton("Redial");
    private JButton Button1 = new JButton("1");
    private JButton Button2 = new JButton("2");
    private JButton Button3 = new JButton("3");
    private JButton Button4 = new JButton("4");
    private JButton Button5 = new JButton("5");
    private JButton Button6 = new JButton("6");
    private JButton Button7 = new JButton("7");
    private JButton Button8 = new JButton("8");
    private JButton Button9 = new JButton("9");
    private JButton Button0 = new JButton("0");
    private JButton astrButton = new JButton("*");
    private JButton boundButton = new JButton("#");
    
/** We'll use DecimalFormat later to convert the result to a String with two decimal places.
 */

    private DecimalFormat df2 = new DecimalFormat("#,###.00");

    /**
      The main method creates an instance of the cellphone class, which displays
      its window on the screen.
    */

    public static void main(String[] args) {
        new CellPhone();
    }

/**
    The constructor for this application will initialize the JFrame
    by setting the window title bar, giving the frame a size,
    initializing all the controls that appear on the frame, and
    then make it visible.
 */
    public CellPhone()
    {

        setTitle(WINDOW_TITLE);
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildTextPanel();
        buildButtonPanel();
        buildDigitPanel();
        setLayout(new BorderLayout());
        add(textPanel,BorderLayout.NORTH);
        add(buttonPanel,BorderLayout.CENTER);
        add(digitPanel,BorderLayout.SOUTH);
        clearDisplay();
        setVisible(true);

    }

/**
    The buildTextPanel method creates a panel to hold the label and the textfield.
 */
	public void buildTextPanel () {
		textPanel.setLayout(new BorderLayout());
		textPanel.add(displayLabel,BorderLayout.NORTH);
		textPanel.add(displayTextField,BorderLayout.CENTER);
                textPanel.setBackground(Color.RED);
	}

/**
      The buildButtonPanel method creates a panel to hold the send,clear,end and redial buttons.
 */
	public void buildButtonPanel () {
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.add(sendButton);
		buttonPanel.add(clearButton);
		buttonPanel.add(endButton);
                buttonPanel.add(redialButton);
		sendButton.addActionListener(new SendButtonListener());
		clearButton.addActionListener(new ClearButtonListener());
		endButton.addActionListener(new EndButtonListener());
                redialButton.addActionListener(new RedialButtonListener());
                buttonPanel.setBackground(Color.RED);
	}

/**
      The buildDigitPanel method creates a panel to hold the digit buttons.
 */
	public void buildDigitPanel () {
		digitPanel.setLayout(new GridLayout(4,3));
		digitPanel.add(Button1);
		digitPanel.add(Button2);
                digitPanel.add(Button3);
                digitPanel.add(Button4);
                digitPanel.add(Button5);
                digitPanel.add(Button6);
                digitPanel.add(Button7);
                digitPanel.add(Button8);
                digitPanel.add(Button9);
                digitPanel.add(astrButton);
                digitPanel.add(Button0);
                digitPanel.add(boundButton);

/** create innerlistener to all buttons, then add all buttons to 
    this listener. 
 */
            
           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);
                astrButton.addActionListener(listener);
                boundButton.addActionListener(listener);
	}

    

/**
     SendButtonListener is an action listener class for
      the Send button.
 */

	private class SendButtonListener implements ActionListener {
/**
      The actionPerformed method executes when the user clicks on the send button.
      @param e The event object.
 */

      	public void actionPerformed(ActionEvent e) {

              input= displayTextField.getText();
	       	sendCalling();
                
		}
	}

/**
     ClearButtonListener is an action listener class for
      the Clear button.
 */

	private class ClearButtonListener implements ActionListener {

/**
     The actionPerformed method executes when the user clicks on the clear button.
      @param e The event object.
 */

      	public void actionPerformed(ActionEvent e) {

	       	clearDisplay();
		}
	}

/**
    EndButtonListener is an action listener class for the Exit button.
 */

	private class EndButtonListener implements ActionListener {
/**
    The actionPerformed method executes when the user
     clicks on the end button.
     @param e The event object.
 */

      	public void actionPerformed(ActionEvent e) {

	       	endCalling();
		}
	}

        private class RedialButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
                  input= displayTextField.getText();
                 redialCalling();
                 }
        }

   	
/**
    The clearDisplay method will clear the digit,send,end and redial fields.
 */

   	public void clearDisplay() {
		displayTextField.setText(" ");
		
	}
   	
/**
    The sendCalling method will dislay a message when calling.
 */
        public void sendCalling() {
		displayTextField.setText("Calling " + input+ input+ input+ "-" + input+ input+input+ "-" +input+ input+ input+ input);
		
	}
/**
    The endCalling method will dislay a message when call is ended.
 */
        public void endCalling() {
		displayTextField.setText("Call Ended");
		
	}
/**
    The redialCalling method will dislay a message when number is redialing.
 */
        public void redialCalling() {
		displayTextField.setText("Redialing " + input+ input+ input+ "-" + input+ input+input+ "-" + input+ input+ input+ input );
		
	}

      
        
        private class InnerListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
                  if(e.getSource()==Button1) {
                    displayTextField.setText("1"); 
                  }
                   else if (e.getSource()==Button2) {
                     displayTextField.setText("2"); 
                   }
                   else if (e.getSource()==Button3) {
                     displayTextField.setText("3"); 
                   }
                   else if (e.getSource()==Button4) {
                     displayTextField.setText("4"); 
                   }
                   else if (e.getSource()==Button5) {
                     displayTextField.setText("5"); 
                   }
                   else if (e.getSource()==Button6) {
                     displayTextField.setText("6"); 
                   }
                   else if (e.getSource()==Button7) {
                     displayTextField.setText("7"); 
                   }
                   else if (e.getSource()==Button8) {
                     displayTextField.setText("8"); 
                   }
                   else if (e.getSource()==Button9) {
                     displayTextField.setText("9"); 
                   }
                   else if (e.getSource()==Button0) {
                     displayTextField.setText("0"); 
                   }
                   else if (e.getSource()==astrButton) {
                     displayTextField.setText("*"); 
                   }
                   else if (e.getSource()==boundButton) {
                     displayTextField.setText("#"); 
                   }
                   
         }       
     }     
 }
displayTextField.setText("3")

Now when a user dials a number I guess you might want to display the entire number to the user.
The way you are using setText() it would actually erase whatever is present in the Text Field with whatever value you set. If you want to display the entire number I replacing the above piece of code wherever it appears with:-

displayTextField.setText(displayTextField.getText() + "3");

The above piece of code will get whatever value is present in your TextField and append there to it, and set it as the new value in your text field. You will need to do the same for all your other numbers.
And when the user presses "SEND" store whatever value you have received for that Number in a variable for accessing it later and then prefix "Calling " to whatever you have in your text field like for ex:-

displayTextField.setText("Calling " + displayTextField.getText());

Similarly if say you have stored the original number in a variable called "number", if the user presses redial instead of prefixing "Calling " in the above statement use "Redialing " + number , when the "Redial" button is clicked

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.