美文网首页
Java 简易计算器

Java 简易计算器

作者: beautymo | 来源:发表于2017-11-16 22:21 被阅读0次

    预想计算器图为:


    简易计算器
    如何实现?

    1.画出计算器框架图
    2.设置button的监听事件,在监听中实现将值传入到JTextField面板上,并在触发“+”等预算符时实现运算

    贴上代码画框代码 用Java里的swing和awt

    
    import javax.swing.*;
    import java.awt.*;
    
    
    public class CaculatorWindow extends JFrame {
        private final String names[] = {"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
        private JTextField textField;
        CaculatorWindow() {
    
            textField = new JTextField();
            GridLayout gridLayout = new GridLayout(4, 4);
            JPanel panel = new JPanel();//定义计算器下方面板
            panel.setLayout(gridLayout);
            JButton[] buttons = new JButton[names.length];
            for (int count = 0; count < names.length; count++) {
                buttons[count] = new JButton(names[count]);
                panel.add(buttons[count]);
            }
           
            //设置文本框位于窗体NORTH位置
            getContentPane().add(textField,BorderLayout.NORTH);
            //将面板放置在窗体CENTER位置
            getContentPane().add(panel,BorderLayout.CENTER);
    
            this.setTitle("Caculator");
            this.setBounds(80,80,300,200);
            this.setResizable(false);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        public static void main(String[] args) {
            new CaculatorWindow();
        }
    }
    
    

    再贴上实现监听和运算过程代码

     //数字按钮的响应事件
            class NumButtonHandler implements ActionListener{
    
                public void actionPerformed(ActionEvent event){
                    JButton object = (JButton)event.getSource();
                    char jButtonName = object.getText().charAt(0);
                    switch (jButtonName){
    
                        default:
                            setTextField(jButtonName);
                            break;
    
                    }
    
                }
                void setTextField(char jButttonName){
                    textField.setText(textField.getText()+String.valueOf(jButttonName));//使得之前textField上的字不会被覆盖
                }
    
            }
            //加减乘除等于的响应事件
            class opeButtonHandler implements  ActionListener{
                public void actionPerformed(ActionEvent event){
                    String command = event.getActionCommand();
                    if(command.equals("+")){
                        temp = Double.parseDouble(textField.getText()); //用于保存运算符号前一个数字
                        textField.setText("");
                        select = 1;
                    }
                    if(command.equals("-")){
                        temp = Double.parseDouble(textField.getText());
                        textField.setText("");
                        select = 2;
                    }
                    if(command.equals("*")){
                        temp = Double.parseDouble(textField.getText());
                        textField.setText("");
                        select = 3;
                    }
                    if(command.equals("/")){
                        temp = Double.parseDouble(textField.getText());
                        textField.setText("");
                        select = 4;
                    }
                    //在 = 处进行+-*/事件处理
                    if(command.equals("=")){
                        switch (select){
                            case 1:
                                number = Double.parseDouble(textField.getText());//用于保存运算符号后一个数字
                                textField.setText((temp+number)+"");
                                break;
                            case 2:
                                number = Double.parseDouble(textField.getText());
                                textField.setText((temp-number)+"");
                                break;
                            case 3:
                                number = Double.parseDouble(textField.getText());
                                textField.setText((temp*number)+"");
                                break;
                            case 4:
                                number = Double.parseDouble(textField.getText());
                                textField.setText((temp/number)+"");
                                break;
                        }
                    }
                }
            }
    
            NumButtonHandler numButtonHandler = new NumButtonHandler();
            for (int count = 0; count < names.length; count++) {
                if(count == 3 || count == 7 || count == 11  || count == 14 || count == 15)
                    buttons[count].addActionListener(new opeButtonHandler());
                else
                    buttons[count].addActionListener(numButtonHandler);
            }
    

    贴上完整代码

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class CaculatorWindow extends JFrame {
        private final String names[] = {"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
        private JTextField textField;
        private int select = 0;
        private double temp = 0.0;
        private double number;
        CaculatorWindow() {
    
            textField = new JTextField();
            GridLayout gridLayout = new GridLayout(4, 4);
            JPanel panel = new JPanel();//定义计算器下方面板
            panel.setLayout(gridLayout);
            JButton[] buttons = new JButton[names.length];
            for (int count = 0; count < names.length; count++) {
                buttons[count] = new JButton(names[count]);
                panel.add(buttons[count]);
            }
            //数字按钮的响应事件
            class NumButtonHandler implements ActionListener{
    
                public void actionPerformed(ActionEvent event){
                    JButton object = (JButton)event.getSource();
                    char jButtonName = object.getText().charAt(0);
                    switch (jButtonName){
    
                        default:
                            setTextField(jButtonName);
                            break;
    
                    }
    
                }
                void setTextField(char jButttonName){
                    textField.setText(textField.getText()+String.valueOf(jButttonName));//使得之前textField上的字不会被覆盖
                }
    
            }
            //加减乘除等于的响应事件
            class opeButtonHandler implements  ActionListener{
                public void actionPerformed(ActionEvent event){
                    String command = event.getActionCommand();
                    if(command.equals("+")){
                        temp = Double.parseDouble(textField.getText()); //用于保存运算符号前一个数字
                        textField.setText("");
                        select = 1;
                    }
                    if(command.equals("-")){
                        temp = Double.parseDouble(textField.getText());
                        textField.setText("");
                        select = 2;
                    }
                    if(command.equals("*")){
                        temp = Double.parseDouble(textField.getText());
                        textField.setText("");
                        select = 3;
                    }
                    if(command.equals("/")){
                        temp = Double.parseDouble(textField.getText());
                        textField.setText("");
                        select = 4;
                    }
                    //在 = 处进行+-*/事件处理
                    if(command.equals("=")){
                        switch (select){
                            case 1:
                                number = Double.parseDouble(textField.getText());//用于保存运算符号后一个数字
                                textField.setText((temp+number)+"");
                                break;
                            case 2:
                                number = Double.parseDouble(textField.getText());
                                textField.setText((temp-number)+"");
                                break;
                            case 3:
                                number = Double.parseDouble(textField.getText());
                                textField.setText((temp*number)+"");
                                break;
                            case 4:
                                number = Double.parseDouble(textField.getText());
                                textField.setText((temp/number)+"");
                                break;
                        }
                    }
                }
            }
    
            NumButtonHandler numButtonHandler = new NumButtonHandler();
            for (int count = 0; count < names.length; count++) {
                if(count == 3 || count == 7 || count == 11  || count == 14 || count == 15)
                    buttons[count].addActionListener(new opeButtonHandler());
                else
                    buttons[count].addActionListener(numButtonHandler);
            }
            //设置文本框位于窗体NORTH位置
            getContentPane().add(textField,BorderLayout.NORTH);
            //将面板放置在窗体CENTER位置
            getContentPane().add(panel,BorderLayout.CENTER);
    
            this.setTitle("Caculator");
            this.setBounds(80,80,300,200);
            this.setResizable(false);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        public static void main(String[] args) {
            new CaculatorWindow();
        }
    }
    

    现存问题
    只能实现简单的加减乘除运算,并且在触发运算符时无法再JTextField上显示运算符,之前的数字也将消失
    将要解决
    即使触发运算符,也能保留之前输入数字
    能实现运算符的优先等级,在计算器面板中加入更多运算符如%,(,)

    相关文章

      网友评论

          本文标题:Java 简易计算器

          本文链接:https://www.haomeiwen.com/subject/cggrvxtx.html