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