hi,
iam trying to create phone directory as a seperate Jframe. there is a button in the main JFrame that leads to the seperate Jframe. the seperate Jframe contains JList which includes names and numbers. when click on any name from the list, this name with the number should appear in the Jtextfield of the main JFrame.
here is my program ( it is not completed yet), but I have many errors ???
/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class NewCellPhone extends JFrame {
private final String WINDOW_TITLE = "CellPhone Simulation";
private final int WINDOW_WIDTH = 200;
private final int WINDOW_HEIGHT = 300;
String input="";
String[] list= {"Cathy 7654328901", " Jessica 1654223789", " Marry 9871265347",
"Sam 9871236453", " Smith 4512344879", "Larry 2766541397"};
// 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 JLabel phoneLabel = new JLabel (" Address Book");
private JList list= new JList();
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(-1);
private JTextField displayTextField = new JTextField(20);
private JFrame frame= new JFrame();
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 phoneButton = new JButton ("PhoneBook");
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 NewCellPhone();
}
/**
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 NewCellPhone(frame) {
super("Address Book");
this.frame=frame;
init();
}
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);
}
void init() {
{
JFrame frame = new JFrame (" Address Book");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildLabelPanel();
buildListPanel();
setLayout(new BorderLayout());
add(labelPanel,BorderLayout.NORTH);
add(listPanel,BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(false);
}
/**
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);
buttonPanel.add(phoneButton);
sendButton.addActionListener(new SendButtonListener());
clearButton.addActionListener(new ClearButtonListener());
endButton.addActionListener(new EndButtonListener());
redialButton.addActionListener(new RedialButtonListener());
phoneButton.addActionlistener(new PhoneButtonListener());
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) {
redialCalling();
}
}
/**
The clearDisplay method will clear the digit,send,end and redial fields.
*/
public void clearDisplay() {
input="";
displayTextField.setText(" ");
}
/**
The sendCalling method will dislay a message when calling.
*/
public void sendCalling() {
displayTextField.setText("Calling " + 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);
}
private class InnerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Button1) {
displayTextField.setText(input +"1");
input= input+"1";
}
else if (e.getSource()==Button2) {
displayTextField.setText(input +"2");
input= input+"2";
}
else if (e.getSource()==Button3) {
displayTextField.setText(input +"3");
input=input +"3";
}
else if (e.getSource()==Button4) {
displayTextField.setText(input +"4");
input=input +"4";
}
else if (e.getSource()==Button5) {
displayTextField.setText(input +"5");
input=input +"5";
}
else if (e.getSource()==Button6) {
displayTextField.setText(input +"6");
input=input +"6";
}
else if (e.getSource()==Button7) {
displayTextField.setText(input +"7");
input=input +"7";
}
else if (e.getSource()==Button8) {
displayTextField.setText(input +"8");
input=input +"8";
}
else if (e.getSource()==Button9) {
displayTextField.setText(input +"9");
input=input +"9";
}
else if (e.getSource()==Button0) {
displayTextField.setText(input +"0");
input=input +"0";
}
else if (e.getSource()==astrButton) {
displayTextField.setText(input +"*");
input=input +"*";
}
else if (e.getSource()==boundButton) {
displayTextField.setText(input +"#");
input=input +"#";
}
}
}
}