Java课程设计-计算器

1、团队课程设计博客链接

http://www.cnblogs.com/yuanj/p/7072137.html

2.个人负责模块或任务说明

监听器的设置

3.自己的代码提交记录截图

//注册各个组件监听事件
    backspace.addMouseListener(new OperationMouseListener());
    CE.addMouseListener(new OperationMouseListener());
    C.addMouseListener(new OperationMouseListener());
    decimal.addMouseListener(new OperationMouseListener());
    equal.addMouseListener(new OperationMouseListener());
    neg.addMouseListener(new OperationMouseListener());
    
    //注册四则运算鼠标监听
    add.addMouseListener(new CalcMouseListener());
    sub.addMouseListener(new CalcMouseListener());
    mul.addMouseListener(new CalcMouseListener());
    div.addMouseListener(new CalcMouseListener());
    
    //注册数字监听事件
    zero.addMouseListener(new NumberMouseListener());
    one.addMouseListener(new NumberMouseListener());
    two.addMouseListener(new NumberMouseListener());
    three.addMouseListener(new NumberMouseListener());
    four.addMouseListener(new NumberMouseListener());
    five.addMouseListener(new NumberMouseListener());
    six.addMouseListener(new NumberMouseListener());
    seven.addMouseListener(new NumberMouseListener());
    eight.addMouseListener(new NumberMouseListener());
    nine.addMouseListener(new NumberMouseListener());
            
}

private class NumberMouseListener implements MouseListener{

    public void mouseClicked(MouseEvent e) {
        if(input.getText().trim().equals("0")){//如果文本框已经是0,结果还是0
            input.setText(((JButton)e.getSource()).getText().trim());
        }else{//否则的话,把0添加到后面,譬如文本框是1,结果就为10
            input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
        }
    }

    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}  
}


private class CalcMouseListener implements MouseListener{
    //如果输入的是运算符号,保存第一个结果和运算符号 
    public void mouseClicked(MouseEvent e) {
        num1 = input.getText().trim();
        input.setText("");
        operator = ((JButton)e.getSource()).getText().trim();
        input.setText(num1+operator);
    }

    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}  
}

Java课程设计-计算器_第1张图片

4、自己负责模块或任务详细说明

 设置监听器与字符运算符的判定存取。

5.课程设计感想

 通过课设,能初步掌握监听器的使用。
 发现自己不能很熟练的写出代码,应加强代码学习。

你可能感兴趣的:(Java课程设计-计算器)