Javaの簡単な計算機

6194 ワード

これは私が送った最初のブログで、Javaで書いた最初のグラフィックプログラムで、簡単な4つの演算、開跟、逆数を行うことができます.
package com.UI.JavaUITest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator02 extends JPanel {
    private JTextField display; //   
    private JPanel jPanel; //  
    private double result; //    
    private String lastCommand; //    
    private boolean isStart; //    

//        
    public Calculator02() {
        setLayout(new BorderLayout()); //       BroderLayout()

        result = 0;
        lastCommand = "=";
        isStart = true;

        display = new JTextField("0"); //       
        display.setEnabled(false); //      ,             ,            ,              ,     
        display.setFont(new Font("    ", Font.BOLD, 20)); //          
        add(display, BorderLayout.NORTH); //          

        ActionListener insert = new InsertAction(); //             
        ActionListener command = new CommandAction(); //             

        jPanel = new JPanel(new GridLayout(5, 4));//    5 4    ,    

//            addButton  ,    、    
        addButton("C", command);
        addButton("CE", command);
        addButton("sqrt", command);
        addButton("1/x", command);

        addButton("7", insert);
        addButton("8", insert);
        addButton("9", insert);
        addButton("/", command);

        addButton("4", insert);
        addButton("5", insert);
        addButton("6", insert);
        addButton("*", command);

        addButton("1", insert);
        addButton("2", insert);
        addButton("3", insert);
        addButton("-", command);

        addButton("0", insert);
        addButton(".", insert);
        addButton("=", command);
        addButton("+", command);

        add(jPanel, BorderLayout.CENTER);//         
    }

//      、       
    public void addButton(String name, ActionListener listener) {
        JButton jButton = new JButton(name);
        jButton.addActionListener(listener); //         
        jButton.setFont(new Font("    ", Font.PLAIN, 20)); //         

        jPanel.add(jButton);//        
    }

//             ,        ,          
    private class InsertAction implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            String input = event.getActionCommand(); //      

//                    ,               
            try {
//                            ,       ,      
                if (isStart) {
                    display.setText("");
                    isStart = false;
                }
                display.setText(display.getText() + input); //              
            }catch (NumberFormatException e) { //         ,        
                display.setText("error");
                result = 0;
                lastCommand = "=";
                isStart = true;
            }catch (ArithmeticException e){ //      ,        
                display.setText("error");
                result = 0;
                lastCommand = "=";
                isStart = true;
            }
        }
    }

//               ,        ,          
    private class CommandAction implements ActionListener{
        public void actionPerformed(ActionEvent event) {
            String command = event.getActionCommand(); //      
//                    ,               
            try {
 //                      “C”         
                if (command.equals("C")) {
                    display.setText("0");
                    isStart = true;
                }
//                      “CE”         ,           
                else if (command.equals("CE")) {
                    display.setText("0");
                    result = 0;
                    lastCommand = "=";
                    isStart = true;
                }
//                         “-”,     
                else if (isStart) {
                    if (command.equals("-")) {
                        display.setText("-");
                        isStart = false;
                    }
                    else  lastCommand = command;
                }
                else{
//                      calculatorDemo  ,      
                    calculatorDemo(Long.parseLong(display.getText()));//               Long  
                    lastCommand = command;
                    isStart = true;
                }
            }catch (NumberFormatException e){ //         
                display.setText("error");
                result = 0;
                lastCommand = "=";
                isStart = true;
            }catch (ArithmeticException e){ //      
                display.setText("error");
                result = 0;
                lastCommand = "=";
                isStart = true;
            }
        }
    }

//        
    public void calculatorDemo(double num){
//          break         ,        ,       switch  ,       if    switch,        
        if (lastCommand.equals("+")) result += num;
        else if (lastCommand.equals("-")) result -= num;
        else if (lastCommand.equals("*")) result *= num;
        else if (lastCommand.equals("/")) result /= num;
        else if (lastCommand.equals("=")) result = num;
        else if (lastCommand.equals("sqrt")) result = Math.sqrt(num);
        else if (lastCommand.equals("1/x")) result = 1 / num;

        display.setText("" + result);
    }

    public static void main(String[] args) {
        JFrame jFrame = new JFrame("   "); //    
        JPanel jPanel2 = new Calculator02();
        jFrame.setContentPane(jPanel2);//        
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //         
        jFrame.pack();
        jFrame.setLocationRelativeTo(jPanel2); //         
        jFrame.setVisible(true); //    
    }
}