I need some help. I'm trying to create a number pad like a cellphone that captures user input by pressing buttons in a panel on the left and displaying what the user punches in the top panel. Then I have a clear button in the panel on the right to clear all, but I can't get it to work. Am I close?
package keypad;
import java.awt.*;
import javax.swing.*;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class KeyPadPanel extends JFrame implements ActionListener
{
private JLabel display;
private JButton one, two, three, four, five, six, seven, eight, nine, zero,
asterick, pound, clear;
private JPanel pad, title;
public KeyPadPanel()
{
setLayout (new BorderLayout());
setBackground(Color.BLACK);
display = new JLabel("Key Pad");
title = new JPanel();
title.setLayout(new BorderLayout());
title.add(display);
add(title, BorderLayout.NORTH);
pad = new JPanel();
pad.setBackground(Color.DARK_GRAY);
pad.setPreferredSize(new Dimension (150,168));
one = new JButton ("1");
one.addActionListener(this);
two = new JButton ("2");
two.addActionListener(this);
three = new JButton ("3");
three.addActionListener(this);
four = new JButton ("4");
four.addActionListener(this);
five = new JButton ("5");
five.addActionListener(this);
six = new JButton ("6");
six.addActionListener(this);
seven = new JButton ("7");
seven.addActionListener(this);
eight = new JButton ("8");
eight.addActionListener(this);
nine = new JButton ("9");
nine.addActionListener(this);
zero = new JButton ("0");
zero.addActionListener(this);
asterick = new JButton ("*");
asterick.addActionListener(this);
pound = new JButton ("#");
pound.addActionListener(this);
pad.add(one);
pad.add(two);
pad.add(three);
pad.add(four);
pad.add(five);
pad.add(six);
pad.add(seven);
pad.add(eight);
pad.add(nine);
pad.add(asterick);
pad.add(zero);
pad.add(pound);
JPanel clearPane = new JPanel();
clear = new JButton ("CLR");
clearPane.setBackground(Color.DARK_GRAY);
clearPane.setPreferredSize( new Dimension(50, 50));
Component add = clearPane.add(clear);
add(pad, BorderLayout.CENTER);
add(clearPane, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e)
{
System.out.println(((JButton)e.getSource()).getText());
}
}
My Driver
package keypad;
import javax.swing.*;
import java.awt.Container.*;
public class KeyPad
{
public static void main(String[] args)
{
JFrame frame = new JFrame ("KeyPad");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new KeyPadPanel());
frame.pack();
frame.setVisible(true);
}
}