码迷,mamicode.com
首页 > 其他好文 > 详细

简单的计算器

时间:2014-12-15 09:03:55      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   sp   for   java   

[1].[文件] MyCalculator.java ~ 3KB    下载(2) 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
 
public class MyCalculator extends JFrame{
     
    private static final long serialVersionUID = 1L;
     
    private String showStr="",remainStr="",method="";
    private boolean isCalculated=false;
    private void setShowStr(String showStr){
        this.showStr=showStr;
    }
    private String[] str={"C", "CE", "+/-",
            "7", "8", "9", "4", "5", "6", "1", "2", "3","0",
            ".", "=", "+", "-", "*", "/"};
    JButton[] jbt=new JButton[str.length];
    JTextField jtf=new JTextField();
     
    public MyCalculator(){
        for (int i=0;i<str.length;i++){
            jbt[i]=new JButton(str[i]);
        }
         
        JPanel p1=new JPanel();
        p1.setLayout(new GridLayout(5,3));
        for (int i=0;i<str.length-4;i++){
            p1.add(jbt[i]);
        }
         
        JPanel p2=new JPanel();
        p2.setLayout(new GridLayout(4,1));
        for (int i=str.length-4;i<str.length;i++){
            p2.add(jbt[i]);
        }
        add(jtf,BorderLayout.NORTH);
        add(p1,BorderLayout.CENTER);
        add(p2,BorderLayout.EAST);
         
        ButtonListener listener=new ButtonListener();
        for (int i=0;i<str.length;i++){
            jbt[i].addActionListener(listener);
        }
    }
     
    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e){
            String s=e.getActionCommand();
            if (s=="C"&&showStr.length()>0){
                showStr=showStr.substring(0,showStr.length()-1);
                jtf.setText(showStr);
            }//clear
            else if (s=="CE"){
                showStr="";
                remainStr="";
                isCalculated=false;
                jtf.setText(showStr);
            }
            //clear all
            else if (s=="+"||s=="-"||s=="*"||s=="/"){
                remainStr=showStr;
                showStr="";
                method=s;
                jtf.setText(showStr);
            }
            else if (s=="+/-"&&showStr==""){
                showStr+="-";
                jtf.setText(showStr);
            }
            else if (s=="="){
                jtf.setText(calculation(remainStr,showStr,method));
            }
            else {
                if(isCalculated){
                    showStr="";
                    jtf.setText(showStr);
                    isCalculated=false;
                }
                showStr+=s;
                jtf.setText(showStr);
            }//number
        }
    }
    private String calculation(String s1,String s2,String s3){
        Double num1=Double.parseDouble(s1);
        Double num2=Double.parseDouble(s2);
        isCalculated=true;
        if (s3=="+"){
            setShowStr(num1+num2+"");
            return num1+num2+"";
        }
        else if (s3=="-"){
            setShowStr(num1-num2+"");
            return num1-num2+"";
        }
        else if (s3=="*"){
            setShowStr(num1*num2+"");
            return num1*num2+"";
        }
        else if (s3=="/"){
            setShowStr(num1/num2+"");
            return num1/num2+"";
        }
        return "error";
    }
    public static void main(String[] args){
        JFrame frame=new MyCalculator();
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

简单的计算器

标签:style   http   io   ar   color   os   sp   for   java   

原文地址:http://blog.csdn.net/u014311051/article/details/41930225

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!