[Java] 计算器 →→→→→进入此内容的聊天室

来自 , 2021-03-07, 写在 Java, 查看 142 次.
URL http://www.code666.cn/view/dca5672f
  1. /*
  2.  * @(#)JCalculator.java 1.00 12/29/2009
  3.  *
  4.  * Email: light_warm@163.com
  5.  */
  6.  
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import javax.swing.*;
  10.  
  11. /**
  12.  * A simple calculator program.
  13.  * <p>I saw this program in a QQ group, and help a friend correct it.</p>
  14.  *
  15.  * @author Singyuen Yip
  16.  * @version 1.00 12/29/2009
  17.  * @see JFrame
  18.  * @see ActionListener
  19.  */
  20. public class JCalculator extends JFrame implements ActionListener {
  21.     /**
  22.      * Serial Version UID
  23.      */
  24.     private static final long serialVersionUID = -169068472193786457L;
  25.     /**
  26.      * This class help close the Window.
  27.      * @author Singyuen Yip
  28.      *
  29.      */
  30.     private class WindowCloser extends WindowAdapter {
  31.        public void windowClosing(WindowEvent we) {
  32.            System.exit(0);
  33.        }
  34.     }
  35.  
  36.     int i;
  37.     // Strings for Digit & Operator buttons.
  38.     private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*", "1",
  39.            "2", "3", "-", ".", "0", "=", "+" };
  40.     // Build buttons.
  41.     JButton[] buttons = new JButton[str.length];
  42.     // For cancel or reset.
  43.     JButton reset = new JButton("CE");
  44.     // Build the text field to show the result.
  45.     JTextField display = new JTextField("0");
  46.    
  47.     /**
  48.      * Constructor without parameters.
  49.      */
  50.     public JCalculator() {
  51.        super("Calculator");
  52.        // Add a panel.
  53.        JPanel panel1 = new JPanel(new GridLayout(4, 4));
  54.        // panel1.setLayout(new GridLayout(4,4));
  55.        for (i = 0; i < str.length; i++) {
  56.            buttons[i] = new JButton(str[i]);
  57.            panel1.add(buttons[i]);
  58.        }
  59.        JPanel panel2 = new JPanel(new BorderLayout());
  60.        // panel2.setLayout(new BorderLayout());
  61.        panel2.add("Center", display);
  62.        panel2.add("East", reset);
  63.        // JPanel panel3 = new Panel();
  64.        getContentPane().setLayout(new BorderLayout());
  65.        getContentPane().add("North", panel2);
  66.        getContentPane().add("Center", panel1);
  67.        // Add action listener for each digit & operator button.
  68.        for (i = 0; i < str.length; i++)
  69.            buttons[i].addActionListener(this);
  70.        // Add listener for "reset" button.
  71.        reset.addActionListener(this);
  72.        // Add listener for "display" button.
  73.        display.addActionListener(this);
  74.        // The "close" button "X".
  75.        addWindowListener(new WindowCloser());
  76.        // Initialize the window size.
  77.        setSize(800, 800);
  78.        // Show the window.
  79.        // show(); Using show() while JDK version is below 1.5.
  80.        setVisible(true);
  81.        // Fit the certain size.
  82.        pack();
  83.     }
  84.    
  85.     public void actionPerformed(ActionEvent e) {
  86.        Object target = e.getSource();
  87.        String label = e.getActionCommand();
  88.        if (target == reset)
  89.            handleReset();
  90.        else if ("0123456789.".indexOf(label) > 0)
  91.            handleNumber(label);
  92.        else
  93.            handleOperator(label);
  94.     }
  95.     // Is the first digit pressed?
  96.     boolean isFirstDigit = true;
  97.     /**
  98.      * Number handling.
  99.      * @param key the key of the button.
  100.      */
  101.     public void handleNumber(String key) {
  102.        if (isFirstDigit)
  103.            display.setText(key);
  104.        else if ((key.equals(".")) && (display.getText().indexOf(".") < 0))
  105.            display.setText(display.getText() + ".");
  106.        else if (!key.equals("."))
  107.            display.setText(display.getText() + key);
  108.        isFirstDigit = false;
  109.     }
  110.    
  111.     /**
  112.      * Reset the calculator.
  113.      */
  114.     public void handleReset() {
  115.        display.setText("0");
  116.        isFirstDigit = true;
  117.        operator = "=";
  118.     }
  119.  
  120.     double number = 0.0;
  121.     String operator = "=";
  122.    
  123.     /**
  124.      * Handling the operation.
  125.      * @param key pressed operator's key.
  126.      */
  127.     public void handleOperator(String key) {
  128.        if (operator.equals("+"))
  129.            number += Double.valueOf(display.getText());
  130.        else if (operator.equals("-"))
  131.            number -= Double.valueOf(display.getText());
  132.        else if (operator.equals("*"))
  133.            number *= Double.valueOf(display.getText());
  134.        else if (operator.equals("/"))
  135.            number /= Double.valueOf(display.getText());
  136.        else if (operator.equals("="))
  137.            number = Double.valueOf(display.getText());
  138.        display.setText(String.valueOf(number));
  139.        operator = key;
  140.        isFirstDigit = true;
  141.     }
  142.    
  143.     public static void main(String[] args) {
  144.        new JCalculator();
  145.     }
  146. }
  147.  

回复 "计算器"

这儿你可以回复上面这条便签

captcha