import java.awt.* ;
import java.awt.event.*;
class Mypane extends Panel
{
Button buttonadd,buttonsub,buttonmul,buttondiv,button1,button2,button3,
button4,button5,button6,button7,button8,button9,button0,buttondot,buttonclear,
buttonsin,buttoncos,buttonxx,buttonequal;
Mypane()
{
setLayout(new GridLayout(4,5));
button1=new Button("1");
buttonsin=new Button("sin");
buttoncos=new Button("cos");
buttonxx=new Button("*2");
buttonequal=new Button("=");
button2=new Button("2");
button3=new Button("3");
buttonadd=new Button("+");
button4=new Button("4");
button5=new Button("5");
button6=new Button("6");
buttonsub=new Button("-");
button7=new Button("7");
button8=new Button("8");
button9=new Button("9");
buttonmul=new Button("*");
button0=new Button("0");
buttondiv=new Button("%");
buttondot=new Button(".");
buttonclear=new Button("CE");
add(button1);
add(button2);
add(button3);
add(buttonadd);
add(buttonsin);
add(button4);
add(button5);
add(button6);
add(buttonsub);
add(buttoncos);
add(button7);
add(button8);
add(button9);
add(buttonmul);
add(buttonxx);
add(button0);
add(buttondot);
add(buttonclear);
add(buttondiv);
add(buttonequal);
button1.setFont(new java.awt.Font("隶书", 1, 36));
button2.setFont(new java.awt.Font("隶书", 1, 36));
button3.setFont(new java.awt.Font("隶书", 1, 36));
button4.setFont(new java.awt.Font("隶书", 1, 36));
button5.setFont(new java.awt.Font("隶书", 1, 36));
button6.setFont(new java.awt.Font("隶书", 1, 36));
button7.setFont(new java.awt.Font("隶书", 1, 36));
button8.setFont(new java.awt.Font("隶书", 1, 36));
button9.setFont(new java.awt.Font("隶书", 1, 36));
button0.setFont(new java.awt.Font("隶书", 1, 36));
buttondot.setFont(new java.awt.Font("隶书", 1, 36));
buttonxx.setFont(new java.awt.Font("隶书", 1, 36));
buttoncos.setFont(new java.awt.Font("隶书", 1, 36));
buttonsin.setFont(new java.awt.Font("隶书", 1, 36));
buttonequal.setFont(new java.awt.Font("隶书", 1, 36));
buttonadd.setFont(new java.awt.Font("隶书", 1, 36));
buttonsub.setFont(new java.awt.Font("隶书", 1, 36));
buttonmul.setFont(new java.awt.Font("隶书", 1, 36));
buttondiv.setFont(new java.awt.Font("隶书", 1, 36));
buttonclear.setFont(new java.awt.Font("隶书", 1, 36));
setBackground(Color.LIGHT_GRAY);
}
}
class ButtonWindow extends Frame implements ActionListener
{
TextField text;
Mypane pane;
Button buttonesc;
double a,b,c;
String str;
String num1,num2,v,s,op;
int x,y,n;
ButtonWindow()
{
pane=new Mypane();
text=new TextField("0.",15);
buttonesc=new Button("");
text.setBackground(Color.GREEN);
add(text);
text.setBounds(160, 110, 370, 40);
text.setFont(new java.awt.Font("隶书", 1, 36));
add(pane);
pane.setBounds(100, 190, 480, 300);
add(buttonesc);
buttonesc.setBounds(170, 500, 100, 100);
buttonesc.setBackground(Color.BLACK);
setBounds(100,100,640,550);
setVisible(true);
buttonesc.addActionListener(this);
pane.button1.addActionListener(this);
pane.button2.addActionListener(this);
pane.button3.addActionListener(this);
pane.button4.addActionListener(this);
pane.button5.addActionListener(this);
pane.button6.addActionListener(this);
pane.button7.addActionListener(this);
pane.button8.addActionListener(this);
pane.button9.addActionListener(this);
pane.button0.addActionListener(this);
pane.buttondot.addActionListener(this);
pane.buttonclear.addActionListener(this);
pane.buttonadd.addActionListener(this);
pane.buttonsub.addActionListener(this);
pane.buttonmul.addActionListener(this);
pane.buttonmul.addActionListener(this);
pane.buttondiv.addActionListener(this);
pane.buttonequal.addActionListener(this);
pane.buttonxx.addActionListener(this);
pane.buttoncos.addActionListener(this);
pane.buttonsin.addActionListener(this);
addWindowListener(new WindowAdapter(){ //关闭窗口
public void windowClosing(WindowEvent e){
dispose();
}
});
text.setEnabled(false);
setResizable(false);
validate();
}
public void actionPerformed(ActionEvent e)
{
s= e.getActionCommand();//得到按钮上的值
v =text.getText();// 得到输入到文本上的值
if(v.equals("0."))
{
text.setText("");
v= text.getText();// 得到输入到文本上的值
}
text.setText(v+s);
if(e.getSource()==pane.buttonadd||
e.getSource()==pane.buttonmul||
e.getSource()==pane.buttonsub||
e.getSource()==pane.buttondiv||
e.getSource()==pane.buttonclear||
e.getSource()==buttonesc||
e.getSource()==pane.buttonequal||
e.getSource()==pane.buttonxx||
e.getSource()==pane.buttoncos||
e.getSource()==pane.buttonsin)
{
if(e.getSource()==pane.buttonadd||
e.getSource()==pane.buttonmul||
e.getSource()==pane.buttonsub||
e.getSource()==pane.buttondiv||
e.getSource()==pane.buttonxx||
e.getSource()==pane.buttoncos||
e.getSource()==pane.buttonsin)
{
op=s;
}
if(e.getSource()==pane.buttonclear)
{
v="";
s="";
str="";
text.setText("");
}
if(e.getSource()==pane.buttonequal)
{
text.setText("");
operator(op);
text.setText(str);
}
if(e.getSource()==buttonesc)
{
dispose();
}
}
}
void operator(String op)
{
if(op.equals("+"))
n=1;
System.out.println(n);
if(op.equals("-"))
n=2;
if(op.equals("*"))
n=3;
if(op.equals("%"))
n=4;
if(op.equals("*2"))
n=7;
if(op.equals("cos"))
n=6;
if(op.equals("sin"))
n=5;
try
{
switch(n)
{
case 1:
{
x=v.indexOf("+");
y=v.length();
num1=v.substring(0, x);
num2=v.substring(x,y);
a=Double.parseDouble(num1);
b=Double.parseDouble(num2);
c=a+b;
System.out.println(c);
str=String.valueOf(c);
System.out.println(str);
break;
}
case 2:
{
x=v.indexOf("-");
y=v.length();
num1=v.substring(0, x);
num2=v.substring(x,y);
a=Double.parseDouble(num1);
b=Double.parseDouble(num2);
c=a-b;
System.out.println(c);
str=String.valueOf(c);
System.out.println(str);
break;
}
case 3:
{
x=v.indexOf("*");
y=v.length();
num1=v.substring(0, x);
num2=v.substring(x+2,y);
a=Double.parseDouble(num1);
b=Double.parseDouble(num2);
c=a*b;
System.out.println(c);
str=String.valueOf(c);
System.out.println(str);
break;
}
case 4:
{
x=v.indexOf("%");
y=v.length();
num1=v.substring(0, x);
num2=v.substring(x+1,y);
a=Double.parseDouble(num1);
b=Double.parseDouble(num2);
c=a/b;
System.out.println(c);
str=String.valueOf(c);
System.out.println(str);
break;
}
case 5:
{
x=v.indexOf("sin");
num1=v.substring(0, x);
a=Double.parseDouble(num1);
c=Math.sin(a*3.14159265358979323846264338327950288419716939937510/180);
System.out.println(c);
str=String.valueOf(c);
str=str.substring(0, 17);
System.out.println(str);
break;
}
case 6:
{
x=v.indexOf("cos");
num1=v.substring(0, x);
a=Double.parseDouble(num1);
c=Math.cos(a*3.14159265358979323846264338327950288419716939937510/180);
System.out.println(c);
str=String.valueOf(c);
str=str.substring(0, 17);
System.out.println(str.substring(0, 17));
break;
}
case 7:
{
x=v.indexOf("*2");
num1=v.substring(0, x);
a=Double.parseDouble(num1);
c=a*a;
System.out.println(c);
str=String.valueOf(c);
System.out.println(str);
break;
}
}
}
catch(Exception a)
{
text.setText("Error");
System.out.println("error");
}
}
}
public class Calculator {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
ButtonWindow cal=new ButtonWindow();
}
}