import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.BoxLayout;

public class phone2 implements ActionListener {
	private String numstring = "";
    final JLabel numberLabel = new JLabel(" ");
    
    public Component createComponents() {
    	
    	// create each button and put it in a GridLayout - keyPane
        JButton button1 = new JButton("1");
        button1.addActionListener(this);
        button1.setActionCommand("1");

        JButton button2 = new JButton("2");
        button2.addActionListener(this);
        button2.setActionCommand("2");

        JButton button3 = new JButton("3");
        button3.addActionListener(this);
        button3.setActionCommand("3");

        JPanel keyPane = new JPanel(new GridLayout(0,3));   
        keyPane.add(button1);
        keyPane.add(button2);
        keyPane.add(button3);
        
        // dummy buttons for layout
        keyPane.add(new JButton("4"));
        keyPane.add(new JButton("5"));
        keyPane.add(new JButton("6"));
        keyPane.add(new JButton("7"));
        keyPane.add(new JButton("8"));
        keyPane.add(new JButton("9"));
        keyPane.add(new JButton("*"));
        keyPane.add(new JButton("0"));
        keyPane.add(new JButton("#"));
        
        keyPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        // modify JLabel object which displays typed number         
        numberLabel.setBackground(Color.WHITE);
        numberLabel.setBorder(BorderFactory.createEmptyBorder(5,15,10,15));
 
        // create a BoxLayout, groupPanel, to hold the keys and the number display
        JPanel groupPanel = new JPanel();
        groupPanel.setLayout(new BoxLayout(groupPanel, BoxLayout.Y_AXIS));

        keyPane.setAlignmentX(Component.LEFT_ALIGNMENT);
        numberLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        
        groupPanel.add(keyPane);
        groupPanel.add(numberLabel);

        return groupPanel;
    }

    public void actionPerformed(ActionEvent e) {
    	if (e.getActionCommand().equals("1")){
    		numstring = numstring + "1";
    		numberLabel.setText(numstring);   		
    	}
    	if (e.getActionCommand().equals("2")){
    		numstring = numstring + "2";
    		numberLabel.setText(numstring);   		
    	}
    	if (e.getActionCommand().equals("3")){
    		numstring = numstring + "3";
    		numberLabel.setText(numstring);   		
    	}

    }

    /**
     * Create the GUI and show it  For thread safety, this method should be 
     * invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {

        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        JFrame frame = new JFrame("MyButtonApplication");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        phone2 app = new phone2();
        Component contents = app.createComponents();
        frame.getContentPane().add(contents);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}