1  import java.awt.BorderLayout;
  2  import java.awt.GridLayout;
  3  import java.awt.event.ActionEvent;
  4  import java.awt.event.ActionListener;
  5  import javax.swing.JButton;
  6  import javax.swing.JPanel;
  7  import javax.swing.JTextField;
  8  
  9  /**
 10     A component that lets the user enter a number, using 
 11     a button pad labeled with digits.
 12  */
 13  public class KeyPad extends JPanel
 14  {
 15     private JPanel buttonPanel;
 16     private JButton clearButton;
 17     private JTextField display;
 18  
 19     /**
 20        Constructs the keypad panel.
 21     */
 22     public KeyPad()
 23     {  
 24        setLayout(new BorderLayout());
 25     
 26        // Add display field
 27     
 28        display = new JTextField();
 29        add(display, "North");
 30  
 31        // Make button panel
 32  
 33        buttonPanel = new JPanel();
 34        buttonPanel.setLayout(new GridLayout(4, 3));
 35        
 36        // Add digit buttons
 37        
 38        addButton("7");
 39        addButton("8");
 40        addButton("9");
 41        addButton("4");
 42        addButton("5");
 43        addButton("6");
 44        addButton("1");
 45        addButton("2");
 46        addButton("3");
 47        addButton("0");      
 48        addButton(".");
 49        
 50        // Add clear entry button
 51        
 52        clearButton = new JButton("CE");
 53        buttonPanel.add(clearButton);
 54  
 55        class ClearButtonListener implements ActionListener
 56        {  
 57           public void actionPerformed(ActionEvent event)
 58           {  
 59              display.setText("");
 60           }
 61        }
 62        ActionListener listener = new ClearButtonListener();      
 63  
 64        clearButton.addActionListener(new 
 65              ClearButtonListener());      
 66        
 67        add(buttonPanel, "Center");
 68     }
 69  
 70     /**
 71        Adds a button to the button panel 
 72        @param label the button label
 73     */
 74     private void addButton(final String label)
 75     {  
 76        class DigitButtonListener implements ActionListener
 77        {  
 78           public void actionPerformed(ActionEvent event)
 79           {  
 80  
 81              // Don't add two decimal points
 82              if (label.equals(".") 
 83                    && display.getText().indexOf(".") != -1) 
 84                 return;
 85  
 86              // Append label text to button
 87              display.setText(display.getText() + label);
 88           }
 89        }
 90  
 91        JButton button = new JButton(label);
 92        buttonPanel.add(button);
 93        ActionListener listener = new DigitButtonListener();
 94        button.addActionListener(listener);
 95     }
 96  
 97     /** 
 98        Gets the value that the user entered. 
 99        @return the value in the text field of the keypad
100     */
101     public double getValue()
102     {  
103        return Double.parseDouble(display.getText());
104     }
105     
106     /** 
107        Clears the display. 
108     */
109     public void clear()
110     {  
111        display.setText("");
112     }
113  }
114