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(
 65              new 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              // Don't add two decimal points
 81              if (label.equals(".") 
 82                    && display.getText().indexOf(".") != -1) 
 83                 return;
 84  
 85              // Append label text to button
 86              display.setText(display.getText() + label);
 87           }
 88        }
 89  
 90        JButton button = new JButton(label);
 91        buttonPanel.add(button);
 92        ActionListener listener = new DigitButtonListener();
 93        button.addActionListener(listener);
 94     }
 95  
 96     /** 
 97        Gets the value that the user entered. 
 98        @return the value in the text field of the keypad
 99     */
100     public double getValue()
101     {  
102        return Double.parseDouble(display.getText());
103     }
104     
105     /** 
106        Clears the dislay. 
107     */
108     public void clear()
109     {  
110        display.setText("");
111     }
112  }
113