Here's the assignment the teacher gave us:
"Problem 6.13 page 371 Design and implement an application that helps displays a numeric keypad that might appear on a phone. Above the keypad buttons, show a label that displays the numbers as they are picked. To the right of the keypad buttons, include another button to clear the display. Use a border layout to manage the overall presentation, and a grid layout to manage the keypad buttons. Put a border around the keypad buttons to group them visually and a border around the display. "
Here's my code so far:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
private class KeyPadPanel extends JFrame implements ActionListener
{
public KeyPadPanel()
{
JFrame window = new JFrame();
window.setBorder (BorderFactory.createLineBorder (Color.black, 2));
JButton b1 = new JButton ("1");
JButton b2 = new JButton ("2");
JButton b3 = new JButton ("3");
JButton b4 = new JButton ("4");
JButton b5 = new JButton ("5");
JButton b6 = new JButton ("6");
JButton b7 = new JButton ("7");
JButton b8 = new JButton ("8");
JButton b9 = new JButton ("9");
JButton bPound = new JButton ("#");
JButton b0 = new JButton ("0");
JButton bStar = new JButton ("*");
JButton bClear = new JButton("Clear");
//-------------------------------------
// The errors that say "tf.*" can't be
// found...
//-------------------------------------
JTextField tf = new JTextField(20);
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout ());
JPanel KeyPadPanel = new JPanel(new GridLayout(4,3));
KeyPadPanel.setBorder(BorderFactory.createLineBorder (Color.black, 2));
b1.addActionListener (new ButtonListener1());
b2.addActionListener (new ButtonListener2());
b3.addActionListener (new ButtonListener3());
b4.addActionListener (new ButtonListener4());
b5.addActionListener (new ButtonListener5());
b6.addActionListener (new ButtonListener6());
b7.addActionListener (new ButtonListener7());
b8.addActionListener (new ButtonListener8());
b9.addActionListener (new ButtonListener9());
bPound.addActionListener (new ButtonListenerPound());
b0.addActionListener (new ButtonListener0());
bStar.addActionListener (new ButtonListenerStar());
KeyPadPanel.add (b1);
KeyPadPanel.add (b2);
KeyPadPanel.add (b3);
KeyPadPanel.add (b4);
KeyPadPanel.add (b5);
KeyPadPanel.add (b6);
KeyPadPanel.add (b7);
KeyPadPanel.add (b8);
KeyPadPanel.add (b9);
KeyPadPanel.add (bPound);
KeyPadPanel.add (b0);
KeyPadPanel.add (bStar);
JPanel Clear = new JPanel();
bClear.addActionListener(new ButtonListenerClear);
Clear.add(bClear);
JPanel display = new JPanel();
JLabel tfbground = new JLabel();
display.add(tfbground);
tfbground.add(tf);
window.add(display, BorderLayout.NORTH);
window.add(KeyPadPanel, BorderLayout.WEST);
window.add(Clear, BorderLayout.EAST);
}
//--------------------------------------------------------------------------------------------------
// Button listener for b1
//--------------------------------------------------------------------------------------------------
private class ButtonListener1 implements ActionListener
{
//--------------------------------------------------------------
// Adds a "1" to the text field when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfText1 = tf.getText() + ("1");
tf.setText(tfText1);
}
}
//-------------------------------------------------------------------------------------------------
// Button listener for b2
//-------------------------------------------------------------------------------------------------
private class ButtonListener2 implements ActionListener
{
//--------------------------------------------------------------
// Adds a "2" to the display when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfText2 = tf.getText() + ("2");
tf.setText(tfText2);
}
}
//-----------------------------------------------------------------------------------------------------
// Button listener for b3
//-----------------------------------------------------------------------------------------------------
private class ButtonListener3 implements ActionListener
{
//--------------------------------------------------------------
// Adds "3" to the text field when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfText3 = tf.getText() + ("3");
tf.setText(tfText3);
}
}
//-----------------------------------------------------------------------------------------------------
// Button listener for b4
//-----------------------------------------------------------------------------------------------------
private class ButtonListener4 implements ActionListener
{
//--------------------------------------------------------------
// Adds "4" in the text field when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfText4 = tf.getText() + ("4");
tf.setText(tfText4);
}
}
//-------------------------------------------------------------------------------------------------------
// Button listener for b5
//-------------------------------------------------------------------------------------------------------
private class ButtonListener5 implements ActionListener
{
//--------------------------------------------------------------
// Adds "5" in the text field when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
String tfText5 = tf.getText() + ("5");
tf.setText(tfText5);
}
}
//-------------------------------------------------------------------------------------------------------
// Button listener for b6
//-------------------------------------------------------------------------------------------------------
private class ButtonListener6 implements ActionListener
{
//--------------------------------------------------------------
// Adds a "6" in the text field when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfText6 = tf.getText() + ("6");
tf.setText(tfText6);
}
}
//-------------------------------------------------------------------------------------------------------
// Button listener for b7
//-------------------------------------------------------------------------------------------------------
private class ButtonListener7 implements ActionListener
{
//--------------------------------------------------------------
// Adds "7" to the text field when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfText7 = tf.getText() + ("7");
tf.setText(tfText7);
}
}
//-------------------------------------------------------------------------------------------------------
// Button listener for b8
//-------------------------------------------------------------------------------------------------------
private class ButtonListener8 implements ActionListener
{
//--------------------------------------------------------------
// Adds "8" to the text field when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfText8 = tf.getText() + ("8");
tf.setText(tfText8);
}
}
//-------------------------------------------------------------------------------------------------------
// Button listener for b9
//-------------------------------------------------------------------------------------------------------
private class ButtonListener9 implements ActionListener
{
//--------------------------------------------------------------
// Adds "9" to the text field when button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfText9 = tf.getText + ("9");
tf.setText(tfText9);
}
}
//-------------------------------------------------------------------------------------------------------
// Button listener for bPound
//-------------------------------------------------------------------------------------------------------
private class ButtonListenerPound implements ActionListener
{
//--------------------------------------------------------------
// Adds a "#" to the text field when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfTextPound = tf.getText() + ("#");
tf.setText(tfTextPound);
}
}
//-------------------------------------------------------------------------------------------------------
// Button listener for b0
//-------------------------------------------------------------------------------------------------------
private class ButtonListener0 implements ActionListener
{
//--------------------------------------------------------------
// Adds a "0" to the text field when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfText0 = tf.getText() + ("0");
tf.setText(tfText0);
}
}
//-------------------------------------------------------------------------------------------------------
// Button listener for bStar
//-------------------------------------------------------------------------------------------------------
private class ButtonListenerStar implements ActionListener
{
//--------------------------------------------------------------
// Adds a "*" to the text field when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String tfTextStar = tf.getText() + ("*");
tf.setText(tfTextStar);
}
}
}
These are the errors I'm getting:
KeyPadPanel.java:88: cannot find symbol
symbol : class ActionListener
location: class KeyPadPanel
private class ButtonListener1 implements ActionListener
^
KeyPadPanel.java:93: cannot find symbol
symbol : class ActionEvent
location: class KeyPadPanel.ButtonListener1
public void actionPerformed (ActionEvent event)
^
KeyPadPanel.java:44: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (KeyPadPanel.ButtonListener1)
b1.addActionListener (new ButtonListener1());
^
(All of my button listeners are giving me the above three errors, just with the variable name changed.)
KeyPadPanel.java:13: cannot find symbol
symbol : method setBorder(javax.swing.border.Border)
location: class javax.swing.JFrame
window.setBorder (BorderFactory.createLineBorder (Color.black, 2));
^
(This one is by itself.)
KeyPadPanel.java:71: cannot find symbol
symbol : class ButtonListenerClear
location: class KeyPadPanel
bClear.addActionListener(new ButtonListenerClear());
^
(As is this one.)
KeyPadPanel.java:96: cannot find symbol
symbol : variable tf
location: class KeyPadPanel.ButtonListener1
String tfText1 = tf.getText() + ("1");
^
(This error is popping up every time I use my tf variable outside of when I created it.)
I'm sorry if I should have posted all of my errors instead of not including multiple instances of the same error but on a different variable, but there were over 60 with all the different buttons and all. I didn't want to overload the post.
Any help is greatly appreciated.
Thanks,
Jess

New Topic/Question
Reply



MultiQuote



|