previous | start | next

File KeyPad.java

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    /**
16        Constructs the keypad panel.
17     */
18    public KeyPad()
19    {  
20       setLayout(new BorderLayout());
21    
22       // add display field
23    
24       display = new JTextField();
25       add(display, "North");
26
27       // make button panel
28
29       buttonPanel = new JPanel();
30       buttonPanel.setLayout(new GridLayout(4, 3));
31       
32       // add digit buttons
33       
34       addButton("7");
35       addButton("8");
36       addButton("9");
37       addButton("4");
38       addButton("5");
39       addButton("6");
40       addButton("1");
41       addButton("2");
42       addButton("3");
43       addButton("0");      
44       addButton(".");
45       
46       // add clear entry button
47       
48       clearButton = new JButton("CE");
49       buttonPanel.add(clearButton);
50
51       class ClearButtonListener implements ActionListener
52       {  
53          public void actionPerformed(ActionEvent event)
54          {  
55             display.setText("");
56          }
57       }
58       ActionListener listener = new ClearButtonListener();      
59
60       clearButton.addActionListener(new 
61          ClearButtonListener());      
62       
63       add(buttonPanel, "Center");
64    }
65
66    /**
67        Adds a button to the button panel 
68       @param label the button label
69     */
70    private void addButton(final String label)
71    {  
72       class DigitButtonListener implements ActionListener
73       {  
74          public void actionPerformed(ActionEvent event)
75          {  
76
77             // don't add two decimal points
78             if (label.equals(".") 
79                && display.getText().indexOf(".") != -1) 
80                return;
81
82             // append label text to button
83             display.setText(display.getText() + label);
84          }
85       }
86
87       JButton button = new JButton(label);
88       buttonPanel.add(button);
89       ActionListener listener = new DigitButtonListener();
90       button.addActionListener(listener);
91    }
92
93    /** 
94        Gets the value that the user entered. 
95       @return the value in the text field of the keypad
96     */
97    public double getValue()
98    {  
99       return Double.parseDouble(display.getText());
100    }
101    
102    /** 
103        Clears the dislay. 
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


previous | start | next