keypad.java

来自「用jsp+servlet」· Java 代码 · 共 114 行

JAVA
114
字号
import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JPanel;import javax.swing.JTextField;/**   A component that lets the user enter a number, using    a button pad labeled with digits*/public class KeyPad extends JPanel{   /**      Constructs the keypad panel.   */   public KeyPad()   {        setLayout(new BorderLayout());         // add display field         display = new JTextField();      add(display, "North");      // make button panel      buttonPanel = new JPanel();      buttonPanel.setLayout(new GridLayout(4, 3));            // add digit buttons            addButton("7");      addButton("8");      addButton("9");      addButton("4");      addButton("5");      addButton("6");      addButton("1");      addButton("2");      addButton("3");      addButton("0");            addButton(".");            // add clear entry button            clearButton = new JButton("CE");      buttonPanel.add(clearButton);      class ClearButtonListener implements ActionListener      {           public void actionPerformed(ActionEvent event)         {              display.setText("");         }      }      clearButton.addActionListener(new          ClearButtonListener());                  add(buttonPanel, "Center");   }   /**      Adds a button to the button panel       @param label the button label   */   private void addButton(final String label)   {        class DigitButtonListener implements ActionListener      {           public void actionPerformed(ActionEvent event)         {              // don't add two decimal points            if (label.equals(".")                && display.getText().indexOf(".") != -1)                return;            // append label text to button            display.setText(display.getText() + label);         }      }      JButton button = new JButton(label);      buttonPanel.add(button);      ActionListener listener = new DigitButtonListener();      button.addActionListener(listener);   }   /**       Gets the value that the user entered.       @return the value in the text field of the keypad   */   public double getValue()   {        return Double.parseDouble(display.getText());   }      /**       Clears the dislay.    */   public void clear()   {        display.setText("");   }      private JPanel buttonPanel;   private JButton clearButton;   private JTextField display;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?