java计算器

来源:互联网 发布:扇贝英语软件下载 编辑:程序博客网 时间:2024/05/02 00:22


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class Caculate
{
public static void main(String[] args)
{
CacuFrame cf=new CacuFrame();
cf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

cf.setVisible(true);
}
}
class CacuFrame extends JFrame implements ActionListener
{
public CacuFrame()
{
setTitle("计算器-xz");

answerField=new JTextField("",15);
answerField.setEditable(true);
answerField.setBackground(new Color(255,100,100));


Container cont=this.getContentPane();
panel=new JPanel();
panel.setLayout(new BorderLayout());
panel1=new JPanel();
panel.add(answerField,BorderLayout.CENTER);

panel3=new JPanel();
panel3.setLayout(new BorderLayout());

panel.add(panel3,BorderLayout.SOUTH);
panel1.setLayout(new GridLayout(4,5));

panel3.add(panel1,BorderLayout.SOUTH);


panel2=new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
panel3.add(panel2,BorderLayout.CENTER);
backspace=new JButton("后退",new ImageIcon("1.gif"));
backspace.addActionListener(this);




clear=new JButton("清零",new ImageIcon("20.gif"));
clear.addActionListener(this);


square=new JButton("平方",new ImageIcon("8.gif"));
square.addActionListener(this);
panel2.add(square);
panel2.add(clear);
panel2.add(backspace);
button=new JButton[10];
for(int i=0;i<10;i++){
button[i]=new JButton(""+i);
button[i].addActionListener(this);
panel1.add(button[i]);
}
add=new JButton("+");
add.addActionListener(this);
sub=new JButton("-");
sub.addActionListener(this);
muti=new JButton("*");
muti.addActionListener(this);
div=new JButton("/");
div.addActionListener(this);
equal=new JButton("=");
equal.addActionListener(this);
sqrt=new JButton("sqrt");
sqrt.addActionListener(this);
daoshu=new JButton("1/x");
daoshu.addActionListener(this);
baifen=new JButton("%");
baifen.addActionListener(this);
point=new JButton(".");
point.addActionListener(this);
add_sub=new JButton("+/-");
add_sub.addActionListener(this);
panel1.add(add);
panel1.add(sub);
panel1.add(muti);
panel1.add(div);
panel1.add(equal);
panel1.add(sqrt);
panel1.add(daoshu);
panel1.add(baifen);
panel1.add(point);
panel1.add(add_sub);
cont.add(panel);
pack();
}
public void actionPerformed(ActionEvent e){
Object temp=e.getSource();
try{
for(int j=0;j<10;j++){
if(temp==button[j]){
answerField.setText(answerField.getText()+Integer.toString(j));
}
}
if(temp==point){
boolean isDot=false;
if(answerField.getText().length()==0)
{
isDot=true;
}
for(int k=0;k<answerField.getText().length();k++){
if(´.´==answerField.getText().charAt(k)){
isDot=true;
break;
}

}
if(isDot==false){
answerField.setText(answerField.getText()+".");
}

}
else if(temp==add||temp==sub||temp==muti||temp==div){
answered=Double.parseDouble(answerField.getText());
if(temp==add)
key=0;
if(temp==sub)
key=1;
if(temp==muti)
key=2;
if(temp==div)
key=3;
answerField.setText("");
}

else if(temp ==equal)
{
vard=Double.parseDouble(answerField.getText());
if(key==0)answered+=vard;
if(key==1)answered-=vard;
if(key==2)answered*=vard;
if(key==3)
{
if(Double.parseDouble(answerField.getText())==0)
{
answerField.setText("除数不能为零");return;
}
else answered/=vard;
}
answerField.setText(String.valueOf(answered));
}
if(temp==baifen)
{
answered =Double.parseDouble(answerField.getText());
if(answered==0)
{

String s=answerField.getText();
System.out.println("answered"+answered+"/ts="+s);
answerField.setText(s);
}

{

boolean isDot=false;
for(int i=0;i<answerField.getText().length();i++)
{
if(´.´==answerField.getText().charAt(i))
{
isDot=true;
break;
}
if(isDot==true)
{
double dtemp=Double.parseDouble(answerField.getText());
dtemp/=100.0;
answerField.setText(Double.toString(dtemp));


}
else
{
if(Integer.parseInt(answerField.getText())%100==0)
{
int itemp=Integer.parseInt(answerField.getText());
itemp/=100;
answerField.setText(Integer.toString(itemp));

}
else
{
double dtemp=Double.parseDouble(answerField.getText());
dtemp/=100.0;
answerField.setText(Double.toString(dtemp));
}
}

}
}

}
if(temp==sqrt)
{
String s=answerField.getText();
if(´-´==s.charAt(0))
{
answerField.setText("负数不能被开方!");

}
else
{
answerField.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(answerField.getText()))));
}
}
if(temp==daoshu)
{
if(answerField.getText().charAt(0)==´0´&&answerField.getText().length()==1)
{
answerField.setText("零不能求倒数!");

}
else
{
double s=Double.parseDouble(answerField.getText());
s=1/s;
answerField.setText(Double.toString(s));
}
}
if(temp==backspace)
{
String s=answerField.getText();
answerField.setText("");
for(int i=0;i<s.length()-1;i++)
{
char a=s.charAt(i);
answerField.setText(answerField.getText()+a);
}
}
if(temp==clear)
{
answered=0;
answerField.setText("");
}
if(temp==square)
{
double s=Double.parseDouble(answerField.getText());
s*=s;
answerField.setText(Double.toString(s));
}
if(temp==add_sub)
{
boolean isNo=true;
String s=answerField.getText();
for(int i=0;i<s.length();i++)
{
if(!(s.charAt(i)>=´0´&&s.charAt(i)<=´9´||s.charAt(i)==´.´||s.charAt(i)==´-´))
{
isNo=false;
break;
}
if(isNo==true)
{
if(s.charAt(i)==´-´)
{
answerField.setText("");
for( i=0;i<s.length();i++)
{
char a=s.charAt(i);
answerField.setText(answerField.getText()+a);
}
}
else
answerField.setText(´-´+s);
}
}
}
}
catch(Exception e1){
System.out.println(e1.toString());
}
}
private JTextField answerField;
private JPanel panel,panel1,panel2,panel3;
private JButton button[];
private JButton add,sub,muti,div,equal,sqrt,daoshu,baifen,point,add_sub,backspace,clear,square;

private short key=-1;
private double answered,vard;
}