文件名称:JAVA编写计算器
文件大小:37KB
文件格式:DOC
更新时间:2011-01-26 14:07:56
5
import java.awt.*;//导入AWT包
import java.awt.event.*;//导入事件处理包
import javax.swing.*;
//继承一个类
public class Untitled1
extends Frame {
//构造各种组件
TextField t = new TextField(" ");
TextField tt = new TextField(" ");
Label l = new Label(" 我的计算器");
Panel p = new Panel();
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel p4 = new Panel();
Panel p5 = new Panel();
Panel p6 = new Panel();
Button bbb = new Button("EXIT");
Button b0 = new Button("0");
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button ba = new Button("+");
Button bb = new Button("-");
Button bc = new Button("*");
Button bd = new Button("/");
Button be = new Button(".");
Button bg = new Button("sin");
Button bh = new Button("cos");
Button bi = new Button("tan");
Button bj = new Button("sqrt");
Button bk = new Button("CLEAR");
Button bl = new Button("exp");
Button bm = new Button("log");
Button bn = new Button("abs");
Button bf = new Button("=");
//声明几个变量
String s1, s2, s3;
double u1, u2, u3;
char op;
//设置各种方法参数
public Untitled1() {
this.setTitle("我的计算器");
this.setLayout(new GridLayout(7, 1, 5, 5));
this.setBackground(Color.blue);
t.setFont(new java.awt.Font("Dialog", 0, 30));
l.setFont(new java.awt.Font("Dialog", 0, 30));
tt.setFont(new java.awt.Font("Dialog", 0, 30));
//为按钮和面板添加底色
//添加面板
this.add(p);
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
this.add(p5);
this.add(p6);
//设置面板
p.setLayout(new BorderLayout());
p.add(l);
p1.setLayout(new BorderLayout());
p1.add(t);
p2.setLayout(new GridLayout(1, 6, 5, 5));
p3.setLayout(new GridLayout(1, 6, 5, 5));
p4.setLayout(new GridLayout(1, 6, 5, 5));
p5.setLayout(new GridLayout(1, 6, 5, 5));
p6.setLayout(new GridLayout(1, 7, 5, 5));
//添加按钮
p2.add(b0);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bg);
p2.add(bk);
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(bh);
p3.add(bl);
p4.add(b8);
p4.add(b9);
p4.add(ba);
p4.add(bb);
p4.add(bm);
p4.add(bi);
p5.add(bc);
p5.add(bd);
p5.add(be);
p5.add(bf);
p5.add(bj);
p5.add(bn);
p6.add(bbb);
//添加各种监听器
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += "0";
t.setText(s);
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += "1";
t.setText(s);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += "2";
t.setText(s);
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += "3";
t.setText(s);
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += "4";
t.setText(s);
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += "5";
t.setText(s);
}
});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += "6";
t.setText(s);
}
});
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += "7";
t.setText(s);
}
});
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += "8";
t.setText(s);
}
});
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += "9";
t.setText(s);
}
});
bg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
double b = Double.parseDouble(s);
b *= Math.PI / 180;
b = Math.sin(b);
b = b * 10000;
b = Math.rint(b);
b = b / 10000;
t.setText("" + b);
}
});
bh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
double b = Double.parseDouble(s);
b *= Math.PI / 180;
b = Math.cos(b);
b = b * 10000;
b = Math.rint(b);
b = b / 10000;
t.setText("" + b);
}
});
bi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
double b = Double.parseDouble(s);
b *= Math.PI / 180;
b = Math.tan(b);
b = b * 10000;
b = Math.rint(b);
b = b / 10000;
t.setText("" + b);
}
});
bl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
double b = Double.parseDouble(s);
b = Math.exp(b);
b = b * 10000;
b = Math.rint(b);
b = b / 10000;
t.setText("" + b);
}
});
bj.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
double b = Double.parseDouble(s);
b = Math.sqrt(b);
b = b * 10000;
b = Math.rint(b);
b = b / 10000;
t.setText("" + b);
}
});
//开始设置功能键
bm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
double b = Double.parseDouble(s);
b = Math.log(b);
b = b * 10000;
b = Math.rint(b);
b = b / 10000;
t.setText("" + b);
}
});
bn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
double b = Double.parseDouble(s);
b = Math.abs(b);
b = b * 10000;
b = Math.rint(b);
b = b / 10000;
t.setText("" + b);
}
});
bk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
t.setText("");
}
});
ba.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s1 = t.getText();
op = '+';
t.setText("");
}
});
bb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s1 = t.getText();
op = '-';
t.setText("");
}
});
bc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s1 = t.getText();
op = '*';
t.setText("");
}
});
bd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s1 = t.getText();
op = '/';
t.setText("");
}
});
be.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
s += ".";
t.setText(s);
}
});
bf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s2 = t.getText();
double u1 = Double.parseDouble(s1);
double u2 = Double.parseDouble(s2);
if (op == '+')
u3 = u1 + u2;
if (op == '-')
u3 = u1 - u2;
if (op == '*')
u3 = u1 * u2;
if (op == '/')
u3 = u1 / u2;
t.setText("" + u3);
}
});
//添加退出按钮
bbb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
//设置主函数
public static void main(String[] args) {
Untitled1 w = new Untitled1();
w.pack();
w.show();
}
}