package apq.Calculate;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculate extends JFrame
implements ActionListener{
private JTextField display=new JTextField();
private JButton[] buttons=new JButton[16];
private String[] keys={"7","8","9","/",
"4","5","6","*",
"1","2","3","-",
"0","C","=","+"};
//to store the 2 inputs;
private String numStr1="";
private String numStr2="";
//to store the result or mid-result;
private String rstStr="";
//to define the first input is finished;
private boolean symb=false;
//to store the op;
private char op;
public Calculate(){
//Create the Frame;
setTitle("My Calculate");
setSize(230,200);
//Get the pane;
Container pane=this.getContentPane();
pane.setLayout(null);
//add the display;
display.setSize(200,30);
display.setLocation(10,10);
pane.add(display);
//add the buttons;
int x=10,y=40;
for(int i=0;i<16;i++){
buttons[i]=new JButton(keys[i]);
buttons[i].setSize(50,30);
buttons[i].setLocation(x,y);
buttons[i].addActionListener(this);
pane.add(buttons[i]);
if(i%4!=3){
x+=50;
}
else{
x=10;
y+=30;
}
}
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//implements the interface
public void actionPerformed(ActionEvent e){
String str=String.valueOf(e.getActionCommand());
char ch=str.charAt(0);
switch(ch){
case '1':case '2':case '3':
case '4':case '5':case '6':
case '7':case '8':case '9':case '0':
if(!symb){
numStr1+=ch;
display.setText(numStr1);
}
else{
numStr2+=ch;
display.setText(numStr2);
}
break;
case '/':case '*':case '-':case '+':
op=ch;
symb=true;
break;
case 'C':
display.setText("");
numStr1=numStr2="";
symb=false;
break;
case '=':
rstStr=evaluate();
display.setText(rstStr);
try{
int temp=Integer.parseInt(rstStr);
}
catch(NumberFormatException e2){
numStr1="";
numStr2="";
symb=false;
break;
}
numStr1=rstStr;
numStr2="";
break;
}
}
//used to evaluate the result;
private String evaluate(){
int result=0;
try{
int num1=Integer.parseInt(numStr1);
int num2=Integer.parseInt(numStr2);
switch (op){
case '+':result=num1+num2;
break;
case '-':result=num1-num2;
break;
case '*':result=num1*num2;
break;
case '/':result=num1/num2;
break;
}
return String.valueOf(result);
}
catch(ArithmeticException e){
return "ERROR:"+e.getMessage();
}
catch(NumberFormatException e){
if(numStr1.equals(""))
return "ERROR:Invalid First Number";
else
return "ERROR:Invalid Second Number";
}
catch(Exception e){
return "ERROR";
}
}
public static void main(String args[]){
Calculate myCalculate=new Calculate();
}
}
分享到:
相关推荐
子网掩码计算器下载/子网掩码计算器单机版--亲测好用 子网掩码计算器下载/子网掩码计算器单机版--亲测好用 子网掩码计算器下载/子网掩码计算器单机版--亲测好用 子网掩码计算器下载/子网掩码计算器单机版--亲测好用 ...
该系统的设计分为五个部分:电子计算器控制工艺流程分析、电子计算器控制系统总体方案设计、电子计算器控制系统梯形图程序设计、电子计算器监控系统设计和电子计算器系统调试及结果分析。 电子计算器控制工艺流程...
"非常实用的计算器工具,可以替换windows自带的计算器" 这个标题表明我们正在讨论的是一款功能强大的计算器应用,它不仅具备基本的计算功能,还能替代Windows操作系统默认提供的简单计算器。这意味着这款计算器可能...
微信小程序 简易计算器 (源码)微信小程序 简易计算器 (源码)微信小程序 简易计算器 (源码)微信小程序 简易计算器 (源码)微信小程序 简易计算器 (源码)微信小程序 简易计算器 (源码)微信小程序 简易计算器 (源码)微信...
小程序源码 高仿苹果计算器 (代码+截图)小程序源码 高仿苹果计算器 (代码+截图)小程序源码 高仿苹果计算器 (代码+截图)小程序源码 高仿苹果计算器 (代码+截图)小程序源码 高仿苹果计算器 (代码+截图)小程序源码 高仿...
题目要求:编写一计算器,仿照windows xp计算器界面,能实现基本的运算及一些科学运算。 能实现的运算如下: "+",加 "-",减 "*",乘 "/",除 "x^y",x的y次方 "Mod",取模 "And",相与 "Or",相或 "Xor",异或 "Lsh",左移 ...