001: import java.awt.BorderLayout;
002: import java.awt.GridLayout;
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import javax.swing.JButton;
006: import javax.swing.JPanel;
007: import javax.swing.JTextField;
008: 
009: /**
010:    A component that lets the user enter a number, using 
011:    a button pad labeled with digits.
012: */
013: public class KeyPad extends JPanel
014: {
015:    /**
016:       Constructs the keypad panel.
017:    */
018:    public KeyPad()
019:    {  
020:       setLayout(new BorderLayout());
021:    
022:       // Add display field
023:    
024:       display = new JTextField();
025:       add(display, "North");
026: 
027:       // Make button panel
028: 
029:       buttonPanel = new JPanel();
030:       buttonPanel.setLayout(new GridLayout(4, 3));
031:       
032:       // Add digit buttons
033:       
034:       addButton("7");
035:       addButton("8");
036:       addButton("9");
037:       addButton("4");
038:       addButton("5");
039:       addButton("6");
040:       addButton("1");
041:       addButton("2");
042:       addButton("3");
043:       addButton("0");      
044:       addButton(".");
045:       
046:       // Add clear entry button
047:       
048:       clearButton = new JButton("CE");
049:       buttonPanel.add(clearButton);
050: 
051:       class ClearButtonListener implements ActionListener
052:       {  
053:          public void actionPerformed(ActionEvent event)
054:          {  
055:             display.setText("");
056:          }
057:       }
058:       ActionListener listener = new ClearButtonListener();      
059: 
060:       clearButton.addActionListener(new 
061:             ClearButtonListener());      
062:       
063:       add(buttonPanel, "Center");
064:    }
065: 
066:    /**
067:       Adds a button to the button panel 
068:       @param label the button label
069:    */
070:    private void addButton(final String label)
071:    {  
072:       class DigitButtonListener implements ActionListener
073:       {  
074:          public void actionPerformed(ActionEvent event)
075:          {  
076: 
077:             // Don't add two decimal points
078:             if (label.equals(".") 
079:                   && display.getText().indexOf(".") != -1) 
080:                return;
081: 
082:             // Append label text to button
083:             display.setText(display.getText() + label);
084:          }
085:       }
086: 
087:       JButton button = new JButton(label);
088:       buttonPanel.add(button);
089:       ActionListener listener = new DigitButtonListener();
090:       button.addActionListener(listener);
091:    }
092: 
093:    /** 
094:       Gets the value that the user entered. 
095:       @return the value in the text field of the keypad
096:    */
097:    public double getValue()
098:    {  
099:       return Double.parseDouble(display.getText());
100:    }
101:    
102:    /** 
103:       Clears the display. 
104:    */
105:    public void clear()
106:    {  
107:       display.setText("");
108:    }
109:    
110:    private JPanel buttonPanel;
111:    private JButton clearButton;
112:    private JTextField display;
113: }
114: