现在的位置: 首页 > 综合 > 正文

java作业 简易计算器

2012年05月03日 ⁄ 综合 ⁄ 共 3447字 ⁄ 字号 评论关闭
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator implements ActionListener{
JFrame jf = new JFrame("Calculator by chenyadong");
JTextField tf = new JTextField();//输入数字的区域

double data;//用于记录输入符号前的数据
String operator;//符号位
boolean clickable = true, clear = true;// clickable 用于判定".",clear用于判定是否点击符号位 用于清屏

public void init() {
jf.setSize(300,200);//设置大小
jf.setVisible(true);//设置为可视

Container c = jf.getContentPane();//得到JFrame的内容窗格对象

tf.setHorizontalAlignment(JTextField.RIGHT);//右对齐
c.add(tf, "North");

JPanel pn1 = new JPanel(new GridLayout(4,4));//运算界面
c.add(pn1, "Center");

JButton b = new JButton("7");
b.addActionListener(this);
pn1.add(b);
b = new JButton("8");
b.addActionListener(this);
pn1.add(b);
b = new JButton("9");
b.addActionListener(this);
pn1.add(b);
b = new JButton("/");
b.addActionListener(this);
pn1.add(b);
b = new JButton("4");
b.addActionListener(this);
pn1.add(b);
b = new JButton("5");
b.addActionListener(this);
pn1.add(b);
b = new JButton("6");
b.addActionListener(this);
pn1.add(b);
b = new JButton("*");
b.addActionListener(this);
pn1.add(b);
b = new JButton("1");
b.addActionListener(this);
pn1.add(b);
b = new JButton("2");
b.addActionListener(this);
pn1.add(b);
b = new JButton("3");
b.addActionListener(this);
pn1.add(b);
b = new JButton("-");
b.addActionListener(this);
pn1.add(b);
b = new JButton("0");
b.addActionListener(this);
pn1.add(b);
b = new JButton(".");
b.addActionListener(this);
pn1.add(b);
b = new JButton("=");
b.addActionListener(this);
pn1.add(b);
b = new JButton("+");
b.addActionListener(this);
pn1.add(b);
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String temp = e.getActionCommand();//获取按钮的标签
if (temp == "0") {
if(clear == false) {//判断是否点击了符号位
tf.setText("");
}
tf.setText(tf.getText() + "0");
}
if (temp == "1") {
if(clear == false) {
tf.setText("");
}
tf.setText(tf.getText() + "1");
clear = true;
}
if (temp == "1") {
if(clear == false) {
tf.setText("");
}
tf.setText(tf.getText() + "1");
clear = true;
}
if (temp == "2") {
if(clear == false) {
tf.setText("");
}
tf.setText(tf.getText() + "2");
clear = true;
}
if (temp == "3") {
if(clear == false) {
tf.setText("");
}
tf.setText(tf.getText() + "3");
clear = true;
}
if (temp == "4") {
if(clear == false) {
tf.setText("");
}
tf.setText(tf.getText() + "4");
clear = true;
}
if (temp == "5") {
if(clear == false) {
tf.setText("");
}
tf.setText(tf.getText() + "5");
clear = true;
}
if (temp == "6") {
if(clear == false) {
tf.setText("");
}
tf.setText(tf.getText() + "6");
clear = true;
}
if (temp == "7") {
if(clear == false) {
tf.setText("");
}
tf.setText(tf.getText() + "7");
clear = true;
}
if (temp == "8") {
if(clear == false) {
tf.setText("");
}
tf.setText(tf.getText() + "8");
clear = true;
}
if (temp == "9") {
if(clear == false) {
tf.setText("");
}
tf.setText(tf.getText() + "9");
clear = true;
}

if (temp == ".") {
clickable = true;
for (int i = 0; i < tf.getText().length(); i++) {//遍历判断是否已有"."
if (tf.getText().charAt(i) == '.') {
clickable = false;
break;
}
}
if (clickable == true) {
tf.setText(tf.getText() + ".");
}
}

try {
if (temp == "+") {
data = Double.parseDouble(tf.getText());//字符串解析成double类型
operator = "+";
clear = false;
}
if (temp == "-") {
data = Double.parseDouble(tf.getText());
operator = "-";
clear = false;
}
if (temp == "*") {
data = Double.parseDouble(tf.getText());
operator = "*";
clear = false;
}
if (temp == "/") {
data = Double.parseDouble(tf.getText());
operator = "/";
clear = false;
}

if (temp == "=") {
double data2 = Double.parseDouble(tf.getText());
tf.setText("");
if (operator == "+")
tf.setText(data + data2 + "");// + "" 变为String类型
if (operator == "-")
tf.setText(data - data2 + "");
if (operator == "*")
tf.setText(data * data2 + "");
if (operator == "/")
tf.setText(data / data2 + "");
clear = false;
}
} catch (Exception ee) {
System.out.println("运算时,首先输入数字");
clear = false;
}

}

public static void main(String args[]) {
new Calculator().init();
}

}

抱歉!评论已关闭.