import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;

public class Calculator1 extends JFrame implements ActionListener {
JTextField text;
static boolean isFirst = true;
double number = 0.0;
public static String s = "0";
double value,sum;
int n;
//主函数
public static void main(String[] args){
new Calculator1();
}
//构造方法
public Calculator1(){
JFrame mywindow = new JFrame();
mywindow.setTitle("我的计算器V-1");
mywindow.setSize(250,250);
mywindow.setLocation(400,300);
mywindow.setResizable(false);
mywindow.setLayout(new BorderLayout());

JPanel panel1 = new JPanel(new FlowLayout());
JPanel panel2 = new JPanel(new GridLayout(4, 4));
text = new JTextField("0",20);
text.setEditable(false);
panel1.add(text);
mywindow.add("North", panel1);
mywindow.add("Center", panel2);

String[] array = {"7","8","9","C","4","5","6","+","1","2","3","-","0","*","/","="};
JButton[] buttons = new JButton[array.length];
for(int i=0;i<array.length;i++){
buttons[i] = new JButton(array[i]);
panel2.add(buttons[i]);
}
for(int i=0;i<buttons.length;i++){
buttons[i].addActionListener(this);
}
mywindow.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String oldstr = text.getText();
String label = e.getActionCommand();
if(label.equals("C")){
s="0";

value= 0;
sum = 0;

text.setText(s);
}
else if("+-*/=0123456789".indexOf(label) > 4){
handNumber(label);
}else{
handOperator(label);
}
}
//操作数字函数
public void handNumber(String num){
if(s.equals("0"))
s = num;
else
s = s + num;
text.setText(s);
}
//操作符号函数
public void handOperator(String operator){
value = Double.valueOf(s);
switch(n)
{
case 0:sum = value;break;
case 1:sum = sum + value;break;
case 2:sum = sum - value;break;
case 3:sum = sum * value;break;
case 4:sum = sum / value;break;
}
if(operator.equals("=")){n = 0;}
if(operator.equals("+")){n = 1;}
if(operator.equals("-")){n = 2;}
if(operator.equals("*")){n = 3;}
if(operator.equals("/")){n = 4;}
s = String.valueOf(sum);
if(operator.equals("=")){
text.setText(s);
} else{
text.setText(s+operator);
s= "0";
}

}
}

最新文章

  1. C# MVC 5 - 生命周期(应用程序生命周期&amp;请求生命周期)
  2. DDD开发框架ABP之导航菜单
  3. HDU5221 Occupation 树链剖分
  4. LCD 和 LED 的区别?
  5. iOS 利用Socket UDP协议广播机制的实现
  6. jQuery之动画效果
  7. MVC 后台DataTable 前台遍历
  8. Android 分享微信好友 朋友圈
  9. 关系网络理论︱细讲中介中心性(Betweeness Centrality)
  10. Hadoop集群环境安装
  11. ssh免密登陆:sshpass -p [passwd] ssh -p [port] root@192.168.X.X
  12. 将JSON格式数据转换为javascript对象 JSON.parse()
  13. Overloaded Fuction 调用——到底使用的是谁的函数
  14. php程序开发之实现网页跳转
  15. redis类与用法
  16. ida脚本函数
  17. climbing stairs leetcode java
  18. Java-简单的计算器(只能进行加法运算)
  19. mysql安装版多次安装导致安装失败的解决方法(windows)(直接使用免安装方法)
  20. learning uboot support web http function in qca4531 cpu

热门文章

  1. HDU-4643-GSM(DFS)
  2. [英语学习]微软面试前英语集训笔记-day 1
  3. JavaScript基础 -- BOM
  4. HTML DOM createTextNode() 方法
  5. HDU 1517:A Multiplication Game
  6. ThinkAndroid框架
  7. Python: PS 滤镜--马赛克
  8. pip 清华镜像
  9. linux下的C语言开发 GDB的例子
  10. tar zxvf 解压文件提示错误