java实现精简版计算器

  最近没事干,想起来原来上课时老师留过一个作业,就是做一个简单的计算器,实现简单的加减乘除功能,当时学的时候那是一个愁啊,最后也是不了了之了,很明显,不会做啊,今天闲了,突然想起来,应该做做试试吧,当然还是很顺利的,虽然格式规范什么的有点乱,但是功能还是可以实现的,当然由于处理逻辑较简单,只能实现两个数的四则运算,每次计算完后需要按“CE”将结果清除后重新输入。这里把代码共享出来,给刚学习java的童鞋做个参考:

下边是运行的界面:

  1 package com.test.JPanel;
  2 
  3 import java.awt.BorderLayout;
  4 import java.awt.GridLayout;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 import javax.swing.JButton;
  8 import javax.swing.JFrame;
  9 import javax.swing.JPanel;
 10 import javax.swing.JTextArea;
 11 
 12 public class Calculator extends JFrame {
 13 
 14     /**
 15      * @param args
 16      * @return
 17      * @author zaj
 18      * @since 2013/10/6
 19      * @
 20      */
 21     JTextArea f1;
 22     String s = "";
 23     String s2 = "";
 24     String a1;
 25     Double sub1;
 26     Double sub2;
 27     String operator;
 28 
 29     public class ButtonHandler implements ActionListener {
 30 
 31         @Override
 32         public void actionPerformed(ActionEvent e) {
 33             // TODO Auto-generated method stub
 34             if (!"CE".equals(e.getActionCommand())
 35                     && !"=".equals(e.getActionCommand())
 36                     && !"+".equals(e.getActionCommand())
 37                     && !"-".equals(e.getActionCommand())
 38                     && !"/".equals(e.getActionCommand())
 39                     && !"*".equals(e.getActionCommand())) {
 40                 System.out.println("action occurred" + e.getActionCommand());
 41                 a1 = e.getActionCommand();
 42                 s = s + String.valueOf(a1);
 43                 if (s2.contains("+") || s2.contains("-") || s2.contains("*")
 44                         || s2.contains("/")) {
 45                     s2 = s2 + a1;
 46                 }
 47                 f1.append(a1);
 48             } else if ("+".equals(e.getActionCommand())) {
 49                 sub1 = Double.parseDouble(s);
 50                 System.out.println(sub1);
 51 
 52                 s = s + e.getActionCommand();
 53                 f1.append(e.getActionCommand() + "\n");
 54                 s2 = s.substring(s.length() - 1);
 55                 operator = s2;
 56                 System.out.println("s2==========" + s2);
 57             } else if ("-".equals(e.getActionCommand())) {
 58                 sub1 = Double.parseDouble(s);
 59                 System.out.println(sub1);
 60 
 61                 s = s + e.getActionCommand();
 62                 f1.append(e.getActionCommand() + "\n");
 63                 s2 = s.substring(s.length() - 1);
 64                 operator = s2;
 65                 System.out.println("s2==========" + s2);
 66             } else if ("*".equals(e.getActionCommand())) {
 67                 sub1 = Double.parseDouble(s);
 68                 System.out.println(sub1);
 69 
 70                 s = s + e.getActionCommand();
 71                 f1.append(e.getActionCommand() + "\n");
 72                 s2 = s.substring(s.length() - 1);
 73                 operator = s2;
 74                 System.out.println("s2==========" + s2);
 75             } else if ("/".equals(e.getActionCommand())) {
 76                 sub1 = Double.parseDouble(s);
 77                 System.out.println(sub1);
 78 
 79                 s = s + e.getActionCommand();
 80                 f1.append(e.getActionCommand() + "\n");
 81                 s2 = s.substring(s.length() - 1);
 82                 operator = s2;
 83                 System.out.println("s2==========" + s2);
 84             } else if ("=".equals(e.getActionCommand())) {
 85                 s2 = s2.substring(1);
 86                 sub2 = Double.parseDouble(s2);
 87                 System.out.println(sub2);
 88                 Double result = 0.0;
 89                 if (operator.equals("+")) {
 90                     result = sub1 + sub2;
 91                     System.out.println(result);
 92                 }
 93                 if (operator.equals("-")) {
 94                     result = sub1 - sub2;
 95                     System.out.println(result);
 96                 }
 97                 if (operator.equals("*")) {
 98                     result = sub1 * sub2;
 99                     System.out.println(result);
100                 }
101                 if (operator.equals("/")) {
102                     result = sub1 / sub2;
103                     System.out.println(result);
104                 }
105 
106                 f1.append("\n" + String.valueOf(result));
107 
108             } else if ("CE".equals(e.getActionCommand())) {
109 
110                 System.out.println(e.getActionCommand());
111                 s = "";
112                 s2 = "";
113                 sub1 = 0.0;
114                 sub2 = 0.0;
115                 f1.setText("");
116             }
117 
118         }
119 
120     }
121 
122     public Calculator() {
123         setLayout(new BorderLayout());
124         f1 = new JTextArea(3, 15);
125         add(f1, "North");
126         JPanel p1 = new JPanel();
127         add(p1, "Center");
128 
129         p1.setLayout(new GridLayout(5, 4));
130         JButton b1 = new JButton("1");
131         JButton b2 = new JButton("2");
132         JButton b3 = new JButton("3");
133         JButton b4 = new JButton("4");
134         JButton b5 = new JButton("5");
135         JButton b6 = new JButton("6");
136         JButton b7 = new JButton("7");
137         JButton b8 = new JButton("8");
138         JButton b9 = new JButton("9");
139         JButton b10 = new JButton("+");
140         JButton b11 = new JButton("-");
141         JButton b12 = new JButton("*");
142         JButton b13 = new JButton("0");
143         JButton b14 = new JButton(".");
144         JButton b15 = new JButton("=");
145         JButton b16 = new JButton("/");
146         JButton b17 = new JButton("CE");
147 
148         b1.addActionListener(new ButtonHandler());
149         b2.addActionListener(new ButtonHandler());
150         b3.addActionListener(new ButtonHandler());
151         b4.addActionListener(new ButtonHandler());
152         b5.addActionListener(new ButtonHandler());
153         b6.addActionListener(new ButtonHandler());
154         b7.addActionListener(new ButtonHandler());
155         b8.addActionListener(new ButtonHandler());
156         b9.addActionListener(new ButtonHandler());
157         b10.addActionListener(new ButtonHandler());
158         b11.addActionListener(new ButtonHandler());
159         b12.addActionListener(new ButtonHandler());
160         b13.addActionListener(new ButtonHandler());
161         b14.addActionListener(new ButtonHandler());
162         b15.addActionListener(new ButtonHandler());
163         b16.addActionListener(new ButtonHandler());
164         b17.addActionListener(new ButtonHandler());
165 
166         p1.add(b7);
167         p1.add(b8);
168         p1.add(b9);
169         p1.add(b10);
170         p1.add(b4);
171         p1.add(b5);
172         p1.add(b6);
173         p1.add(b11);
174         p1.add(b1);
175         p1.add(b2);
176         p1.add(b3);
177         p1.add(b12);
178         p1.add(b13);
179         p1.add(b14);
180         p1.add(b15);
181         p1.add(b16);
182         p1.add(b17);
183     }
184 
185     public static void main(String[] args) {
186         Calculator window = new Calculator();
187         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
188         window.setSize(480, 260);
189         window.setTitle("Calculator-只能计算两数四则运算,使用完需要按“CE”清除");
190         //window.pack();
191         window.setVisible(true);
192     }
193 
194 }

 

posted on 2013-10-06 15:24  273度  阅读(375)  评论(0编辑  收藏  举报

导航