一个java简易计算器...
时间:2010-08-19 来源:mqslll594212
//简易计算器
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
//import java.lang.Math;
import java.math.BigDecimal;
public class SimpleCalculater extends JFrame{
private String[] names={"+","-","*","/","^","CLEAR"};
private JTextField
t1= new JTextField(10),
t2=new JTextField(10),
t3=new JTextField(10);
private JButton[] buttons=new JButton[6];
public static void main(String[] a){
new SimpleCalculater("mqslll");
}
public SimpleCalculater(String title){
super(title);
Container contentPane=getContentPane();
//contentPane.setLayout(new GridLayout(3,1));
JPanel buttonPanel=new JPanel();
JPanel textPanel=new JPanel();
JPanel x=new JPanel();
textPanel.setLayout(new GridLayout(1,3));
textPanel.add(t1);
textPanel.add(t2);
textPanel.add(t3);
t1.setSize(40,30);
t2.setSize(40,30);
t3.setSize(40,30);
buttonPanel.setLayout(new GridLayout(2,3));
for(int i=0;i<buttons.length;i++){
buttons[i]=new JButton(names[i]);
buttonPanel.add(buttons[i]);
}
contentPane.add(buttonPanel,BorderLayout.NORTH);
contentPane.add(textPanel,BorderLayout.CENTER);
contentPane.add(buttonPanel,BorderLayout.SOUTH);
buttons[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str1=t1.getText().toString();
double a =Double.parseDouble(str1);
String str2=t2.getText().toString();
double b=Double.parseDouble(str2);
double c=a+b; //M
t3.setText(new Double(c).toString());
}});
buttons[1].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str1=t1.getText().toString();
double a =Double.parseDouble(str1);
String str2=t2.getText().toString();
double b=Double.parseDouble(str2);
//double c=a-b;
double c=sub(a,b);
t3.setText(new Double(c).toString());
}});
buttons[2].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str1=t1.getText().toString();
double a =Double.parseDouble(str1);
String str2=t2.getText().toString();
double b=Double.parseDouble(str2);
double c=a*b;
t3.setText(new Double(c).toString());
}});
buttons[3].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str1=t1.getText().toString();
double a =Double.parseDouble(str1);
String str2=t2.getText().toString();
double b=Double.parseDouble(str2);
double c=a/b;
t3.setText(new Double(c).toString());
}});
buttons[4].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str1=t1.getText().toString();
double a =Double.parseDouble(str1);
String str2=t2.getText().toString();
double b=Double.parseDouble(str2);
double c=Math.pow(a,b);
t3.setText(new Double(c).toString());
}});
buttons[5].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
t1.setText(null);
t2.setText(null);
t3.setText(null);
}});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//pack();
setSize(300,180);
setVisible(true);
}
public double sub(double d1,double d2){
BigDecimal bd1 = new BigDecimal(Double.toString(d1));
BigDecimal bd2 = new BigDecimal(Double.toString(d2));
return bd1.subtract(bd2).doubleValue();
}
}