Aloha,
I'm new to Java and am having trouble understanding inheritance (super/subclass) and coding it. The assignment I have is to upgrade the CellPhoneGuiP that I created and add an address book to it. Here's my assignment:
"Upgrade your phone application to include a “phone book”. That is, create a list of contacts and phone numbers that can be called from your cell phone. You must create a separate class to represent a contact, instantiate at least five contacts storing them in an array or a dynamic data structure. Your phone book must display on your cell phone display or “pop up” as a separate JFrame."
Below is the my CellPhoneGui & the address Book code. I need some direction and understanding on how to do this. Any help will do.
My code is below:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.*; public class CellPhoneGuiP extends JFrame { private JPanel displayPanel = new JPanel(); //display panel private JPanel funcPanel = new JPanel(); //function buttons SEND, CLEAR, REDIAL & END private JPanel addBookPanel = new JPanel(); private JPanel buttonPanel = new JPanel(); //numerical buttons private JTextField display = new JTextField(25); //to reference a text field private JTextField addBookDisplay = new JTextField(20); private JLabel dlabel = new JLabel (" Cell Phone Simulator "); private final String WINDOW_TITLE = "Cell Phone Gui"; private JButton clearButton; //Clear display private JButton sendButton; //SEND button private JButton redialButton; //Redial number button private JButton endButton; //End the call private JButton exitButton; //Exit Button private JButton addBookButton; //Address Book button private JButton Button1; //Button #1 private JButton Button2; //Button #2 private JButton Button3; //Button #3 private JButton Button4; //Button #4 private JButton Button5; //Button #5 private JButton Button6; //Button #6 private JButton Button7; //Button #7 private JButton Button8; //Button #8 private JButton Button9; //Button #9 private JButton asterisk; //asterisk button private JButton Button0; //Button #0 private JButton lbButton; //pound sign button String numInput; //variable that holds number dialed private final int WINDOW_WIDTH = 350; //Window width private final int WINDOW_HEIGHT = 425; //Window height // AddressBook is the JFrame or child JFrame that will be opened later. AddressBook addBook = new AddressBook(this); //instantiate the parent CellPhoneGuiP (parent) public static void main(String[] args) { CellPhoneGuiP cp1 = new CellPhoneGuiP(); cp1.init(); cp1.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); } // this builds the GUI from the child void init() { addBook.addActionListener(new AddressBookListener()); addBookPanel.setLayout(new BorderLayout()); addBookPanel.add(addBookButton, BorderLayout.NORTH); addBookPanel.add(exitButton,BorderLayout.SOUTH); addBookPanel.add(display,BorderLayout.CENTER); add(addBookPanel); setSize(400,400); setVisible(true); } public CellPhoneGuiP() { super("Address Book"); setTitle(WINDOW_TITLE); //set the window title setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //set the size of the window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //specify an action for the close button buildDisplayPanel(); buildFunctionPanel(); //build the panel & add it to the frame buildButtonPanel(); setLayout(new BorderLayout()); //create a BorderLayout manager add(displayPanel,BorderLayout.NORTH); //add the panel to the frame's content pane add(buttonPanel,BorderLayout.CENTER); add(funcPanel,BorderLayout.SOUTH); clearScreen(); setVisible(true); //display the window } private void buildDisplayPanel() { displayPanel.setLayout(new BorderLayout()); displayPanel.add(dlabel,BorderLayout.NORTH); displayPanel.add(display,BorderLayout.CENTER); displayPanel.setBackground(Color.LIGHT_GRAY); } private void buildFunctionPanel() { funcPanel.setLayout(new GridLayout(3, 1)); sendButton = new JButton("SEND"); clearButton = new JButton("CLEAR"); redialButton = new JButton("REDIAL"); endButton = new JButton(" END "); addBookButton = new JButton("Address Book"); funcPanel.add(sendButton); funcPanel.add(clearButton); funcPanel.add(endButton); funcPanel.add(redialButton); funcPanel.add(addBookButton); //add action listeners to function buttons sendButton.addActionListener(new SendButtonListener()); clearButton.addActionListener(new ClearButtonListener()); endButton.addActionListener(new EndButtonListener()); redialButton.addActionListener(new RedialButtonListener()); addBookButton.addActionListener(new AddressBookListener()); //set color to background of function panel buttonPanel.setBackground(Color.LIGHT_GRAY); } private void buildButtonPanel() { buttonPanel.setLayout(new GridLayout(5, 5)); buttonPanel.setBackground(Color.BLUE); //create buttons with the appropriate captions Button1 = new JButton(" 1 "); Button2 = new JButton(" 2 "); Button3 = new JButton(" 3 "); Button4 = new JButton(" 4 "); Button5 = new JButton(" 5 "); Button6 = new JButton(" 6 "); Button7 = new JButton(" 7 "); Button8 = new JButton(" 8 "); Button9 = new JButton(" 9 "); asterisk = new JButton(" * "); Button0 = new JButton(" 0 "); lbButton= new JButton(" # "); //add the numeric buttons to the panel 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(asterisk); buttonPanel.add(Button0); buttonPanel.add(lbButton); //add action listener to numeric buttons Button1.addActionListener(new ButtonListener()); Button2.addActionListener(new ButtonListener()); Button3.addActionListener(new ButtonListener()); Button4.addActionListener(new ButtonListener()); Button5.addActionListener(new ButtonListener()); Button6.addActionListener(new ButtonListener()); Button7.addActionListener(new ButtonListener()); Button8.addActionListener(new ButtonListener()); Button9.addActionListener(new ButtonListener()); asterisk.addActionListener(new ButtonListener()); Button0.addActionListener(new ButtonListener()); lbButton.addActionListener(new ButtonListener()); } private class SendButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { numInput = display.getText(); send(); } } private class ClearButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { clearScreen(); } } private class RedialButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { numInput = display.getText(); redial(); } } private class EndButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { end(); } } private class AddressBookListener implements ActionListener { public void actionPerformed(ActionEvent e) { addBook.setVisible(true); addBook.toFront(); } } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == Button1) { display.setText(display.getText() + "1"); } else if (e.getSource() == Button2) { display.setText(display.getText() + "2"); } else if (e.getSource() == Button3) { display.setText(display.getText() + "3"); } else if (e.getSource() == Button4) { display.setText(display.getText() + "4"); } else if (e.getSource() == Button5) { display.setText(display.getText() + "5"); } else if (e.getSource() == Button6) { display.setText(display.getText() + "6"); } else if (e.getSource() == Button7) { display.setText(display.getText() + "7"); } else if (e.getSource() == Button8) { display.setText(display.getText() + "8"); } else if (e.getSource() == Button9) { display.setText(display.getText() + "9"); } else if (e.getSource() == asterisk) { display.setText(display.getText() + "*"); } else if (e.getSource() == Button0) { display.setText(display.getText() + "0"); } else if (e.getSource() == lbButton) { display.setText(display.getText() + "#"); } } } //end of ButtonListener class // Method to set the text field on CellPhoneGuiP (parent). Called from addressBook (child) upon hiding. public void setAddressBookDisplay(String x) { addBookDisplay.setText(x); } public void clearScreen() { display.setText(" "); } public void send() { display.setText("Calling " + numInput); } public void end() { display.setText("Call Ended"); } public void redial() { display.setText("Redialing " + numInput); } } =================address book==================================== import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AddressBook extends CellPhoneGuiP { JLabel firstNameLabel; //label for first name JLabel lastNameLabel; //label for last name JLabel cellNumberLabel; //label for cell number JTextField fNameTxtField; //text field for first name JTextField lNameTxtField; //text field for last name JTextField cNumTxtField; //text field for cell number JPanel addBook = new JPanel; //panel for address book JTextArea display; //display Area JButton addButton; //button to add information JButton callButton; //button to send a call JButton exitButton; //exit button //array for contact and cell numbers String cellContacts[] = new strName[5]; String cellNumbers[] = new cellNum[5]; //initialize i variable i = 0; /** Constructor: @param AddressBook -- Reference to CellPhoneGuiP, which is the parent JFrame */ public AddressBook(AddressBook addBook) { super("Address Book"); this.addBook = addBook; init(); } public void init() { JFrame addBook = getContentPane(); addBook.setLayout(new FlowLayout()); //set labels and text fields for the address book panel firstNameLabel = new JLabel ("First Name"); lastNameLabel = new JLabel ("Last Name"); cellNumLabel = new JLabel ("Cell Phone Number"); fNameTxtField = new JTextField(25); lNameTxtField = new JTextField(25); cNumTxtField = new JTextField(15); addButton = new JButton("ADD"); callButton = new JButton("CALL"); exitButton = new JButton("EXIT"); //setting for the display area display = new JTextArea (11, 40); //add labels and buttons addBook.add(firstNameLabel); addBook.add(lastNameLabel); addBook.add(cellNumLabel); addBook.add(fNameTxtField); addBook.add(lNameTxtField); addBook.add(cellNumTxtField); addBook.add(display); //add Action Listeners addButton.addActionListener(this); callButton.addActionListener(this); exitButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == addButton) addFunction(); else if (e.getSource() == callButton) callFunction(); else if (e.getSource() == exitButton) exitFunction(); } public void addFunction() { cellNames[i] = fNameTxtField.getText() + " " + lNameTxtField.getTextField(); cellNum[i] = cNumTxtField.getText(); fNameTxtField.setText(" "); lNameTxtField.setText(" "); cNumTxtField.setText(" "); display.append("Added: " + cellNames[i] + "\t\tCell Number: " + cellNum[i] + "\n"); i++; } public void callFunction() { super.send(display); } public void exitFunction() { system.exit(0); } }