Java计算器

本程序写于刚刚学习j2se的swing时,记得课堂上老师说起,他们那会儿毕业找工作,能够写出个计算器就算是会写程序的人,就不愁找不到工作了,于是我就写了这个程序,安慰一下失业不久的自己。回想起来,这个东西还是挺能够锻炼逻辑思维的,当初确实是费了不少心思,界面就可以看到,懒这个键是调用Windows系统的计算器,当初就是一步步照它而完成的,当然这个程序并没有实现科学计算。由于程序是刚刚系统学习编程不久时写的,很多地方存在不规范,而且一些地方绕了弯子,诸如界面按钮的定义并不是采用数组而成,而是手工画上去的,再有就是java监听器的使用。
以下为运行效果图:


以下为程序源代码:
package calculate;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

/**
 * <p>Title: 简单计算器</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: 无业游民</p>
 *
 * 
@author 非凡DZ
 * 
@version 1.0
 
*/

public class FrameCalculate extends JFrame {
    
private String front = "", behind = ""//分别用于记录加减乘除运算符之前,之后输入的内容
    private String op; //用于记录运算符
    private String re;//用于存储运算结果的字符串格式
    private boolean flag = false//用于记录是否按下了运算符
    private boolean flag1 = false;//用于判断是否输入了点运算符
    private double result;//用于存储运算结果
    private boolean flag2 = false;//用于判断是否输入了数字
    private boolean flag3 = false;//用于判断是否按下了等号运算符
    JPanel contentPane;
    JTextField txtResult 
= new JTextField();
    JButton btnNull 
= new JButton();
    JButton btnFour 
= new JButton();
    JButton btnFive 
= new JButton();
    JButton btnSix 
= new JButton();
    JButton btnDecrease 
= new JButton();
    JButton btnBegin 
= new JButton();
    JButton btnOne 
= new JButton();
    JButton btnTwo 
= new JButton();
    JButton btnThree 
= new JButton();
    JButton btnMultiply 
= new JButton();
    JButton btnCancel 
= new JButton();
    JButton btnZero 
= new JButton();
    JButton btnMinus 
= new JButton();
    JButton btnPoint 
= new JButton();
    JButton btnDivide 
= new JButton();
    JButton btnEqual 
= new JButton();
    JButton btnIncrease 
= new JButton();
    JButton btnSeven 
= new JButton();
    JButton btnEight 
= new JButton();
    JButton btnNine 
= new JButton();
    
public FrameCalculate() {
        
try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        }
 catch (Exception exception) {
            exception.printStackTrace();
        }

    }


    
/**
     * Component initialization.
     *
     * 
@throws java.lang.Exception
     
*/

    
private void jbInit() throws Exception {
        contentPane 
= (JPanel) getContentPane();
        contentPane.setLayout(
null);
        
this.setResizable(false);
        setSize(
new Dimension(400300));
        setTitle(
"计算器");
        txtResult.setEnabled(
false);
        txtResult.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        txtResult.setEditable(
false);
        txtResult.setText(
"0");
        txtResult.setHorizontalAlignment(SwingConstants.RIGHT);
        txtResult.setBounds(
new Rectangle(331931034));
        btnNull.setBounds(
new Rectangle(298704637));
        btnNull.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 12));
        btnNull.setText(
"");
        btnNull.addActionListener(
new FrameCalculate_btnNull_actionAdapter(this));
        btnFour.setBounds(
new Rectangle(331204637));
        btnFour.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnFour.setText(
"4");
        btnFive.setBounds(
new Rectangle(1011204637));
        btnFive.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnFive.setText(
"5");
        btnSix.setBounds(
new Rectangle(1671194637));
        btnSix.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnSix.setText(
"6");
        btnDecrease.setBounds(
new Rectangle(2341204637));
        btnDecrease.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnDecrease.setText(
"-");
        btnBegin.setBounds(
new Rectangle(2981214637));
        btnBegin.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 15));
        btnBegin.setText(
"C");
        btnBegin.addActionListener(
new FrameCalculate_btnBegin_actionAdapter(this));
        btnOne.setBounds(
new Rectangle(331724637));
        btnOne.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnOne.setText(
"1");
        btnTwo.setBounds(
new Rectangle(1011724637));
        btnTwo.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnTwo.setText(
"2");
        btnThree.setBounds(
new Rectangle(1671724637));
        btnThree.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnThree.setText(
"3");
        btnMultiply.setBounds(
new Rectangle(2341724637));
        btnMultiply.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnMultiply.setText(
"*");
        btnCancel.setBounds(
new Rectangle(2981724637));
        btnCancel.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 12));
        btnCancel.setText(
"");
        btnCancel.addActionListener(
new FrameCalculate_btnCancel_actionAdapter(this));
        btnZero.setBounds(
new Rectangle(332224637));
        btnZero.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnZero.setText(
"0");
        
//加载数字0-9的监听事件
        btnZero.addActionListener(new FrameCalculate_btnZero_actionAdapter(this));
        btnOne.addActionListener(
new FrameCalculate_btnZero_actionAdapter(this));
        btnTwo.addActionListener(
new FrameCalculate_btnZero_actionAdapter(this));
        btnThree.addActionListener(
new FrameCalculate_btnZero_actionAdapter(this));
        btnFour.addActionListener(
new FrameCalculate_btnZero_actionAdapter(this));
        btnFive.addActionListener(
new FrameCalculate_btnZero_actionAdapter(this));
        btnSix.addActionListener(
new FrameCalculate_btnZero_actionAdapter(this));
        btnSeven.addActionListener(
new FrameCalculate_btnZero_actionAdapter(this));
        btnEight.addActionListener(
new FrameCalculate_btnZero_actionAdapter(this));
        btnNine.addActionListener(
new FrameCalculate_btnZero_actionAdapter(this));
        btnMinus.setBounds(
new Rectangle(1012224637));
        btnMinus.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 10));
        btnMinus.setText(
"+/-");
        btnMinus.addActionListener(
new FrameCalculate_btnMinus_actionAdapter(this));
        btnPoint.setBounds(
new Rectangle(1672224637));
        btnPoint.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 30));
        btnPoint.setHorizontalTextPosition(SwingConstants.CENTER);
        btnPoint.setText(
".");
        btnPoint.addActionListener(
new FrameCalculate_btnPoint_actionAdapter(this));
        btnDivide.setBounds(
new Rectangle(2342224637));
        btnDivide.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnDivide.setText(
"/");
        btnEqual.setBounds(
new Rectangle(2982224637));
        btnEqual.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnEqual.setText(
"=");
        btnEqual.addActionListener(
new FrameCalculate_btnEqual_actionAdapter(this));
        btnIncrease.setBounds(
new Rectangle(234704637));
        btnIncrease.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnIncrease.setText(
"+");
        
//加载加减乘除运算符的监听事件
        btnIncrease.addActionListener(new
                                      FrameCalculate_btnIncrease_actionAdapter(
this));
        btnDecrease.addActionListener(
new
                                      FrameCalculate_btnIncrease_actionAdapter(
this));
        btnMultiply.addActionListener(
new
                                      FrameCalculate_btnIncrease_actionAdapter(
this));
        btnDivide.addActionListener(
new
                                    FrameCalculate_btnIncrease_actionAdapter(
this));
        btnSeven.setBounds(
new Rectangle(33704637));
        btnSeven.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnSeven.setText(
"7");
        btnEight.setBounds(
new Rectangle(101704637));
        btnEight.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnEight.setText(
"8");
        btnNine.setBounds(
new Rectangle(167704637));
        btnNine.setFont(
new java.awt.Font("Dialog", Font.PLAIN, 20));
        btnNine.setText(
"9");
        contentPane.add(btnFive);
        contentPane.add(btnSix);
        contentPane.add(btnDecrease);
        contentPane.add(btnBegin);
        contentPane.add(btnOne);
        contentPane.add(btnTwo);
        contentPane.add(btnThree);
        contentPane.add(btnMultiply);
        contentPane.add(btnCancel);
        contentPane.add(btnMinus);
        contentPane.add(btnPoint);
        contentPane.add(btnDivide);
        contentPane.add(btnEqual);
        contentPane.add(btnEight);
        contentPane.add(btnNine);
        contentPane.add(btnFour);
        contentPane.add(btnSeven);
        contentPane.add(btnIncrease);
        contentPane.add(btnNull);
        contentPane.add(txtResult);
        contentPane.add(btnZero);
    }


    
public void btnNull_actionPerformed(ActionEvent e) {
        
try {
            Runtime.getRuntime().exec(
"calc.exe"); //调用系统的计算器
        }
 catch (IOException ex) {
        }

    }


    
public void btnZero_actionPerformed(ActionEvent e) {
        
if (flag) //如果刚刚按下了运算符
            txtResult.setText("");
            
if (flag1) {//判断之前是否输入了点运算符
                txtResult.setText("0." + e.getActionCommand());
                flag1 
= false;
            }
 else {
                txtResult.setText(e.getActionCommand());
            }

            flag2 
= true;
        }
 else {
            
int num = txtResult.getText().indexOf(".");
            
if (num < 0 && !txtResult.getText().equals("0")) {
                txtResult.setText(txtResult.getText() 
+ e.getActionCommand());
            }
 else if (num < 0 && txtResult.getText().equals("0")) {
                txtResult.setText(e.getActionCommand());
            }
 else if (num >= 0 && txtResult.getText().equals("0")) {
                txtResult.setText(
"0." + e.getActionCommand());
            }
 else if (num >= 0 && !txtResult.getText().equals("0")) {
                txtResult.setText(txtResult.getText() 
+ e.getActionCommand());
            }

        }

        flag 
= false;
        flag3
=false;
    }


    
public void btnIncrease_actionPerformed(ActionEvent e) {
        
if(flag3){
            txtResult.setText(txtResult.getText());
            op 
= e.getActionCommand(); //得到刚刚按下的运算符
            front = txtResult.getText(); //记录加减乘除运算符之前输入的内容
        }

        
else if (flag2) {
            ActionEvent ee 
= new ActionEvent("qq"1"pp");
            btnEqual_actionPerformed(ee);
            op 
= e.getActionCommand(); //得到刚刚按下的运算符
            front = re;
            flag2 
= false;
        }
 else {
            front 
= txtResult.getText(); //记录加减乘除运算符之前输入的内容
            op = e.getActionCommand(); //得到刚刚按下的运算符
        }

        flag3
=false;
        flag 
= true//记录已经按下了加减乘除运算符的其中一个
    }


    
public void btnEqual_actionPerformed(ActionEvent e) {
        
if(!flag3)//未曾按下等于运算符
            behind = txtResult.getText();
        
else
            front 
= re;
        
try {
            
double a1 = Double.parseDouble(front);
            
double b1 = Double.parseDouble(behind);
            
if (op == "+"{
                result 
= a1 + b1;
            }
 else if (op == "-"{
                result 
= a1 - b1;
            }
 else if (op == "*"{
                result 
= a1 * b1;
            }
 else {
                result 
= a1 / b1;
            }

            Double r 
= new Double(result);
            re 
= r.toString(result);
            txtResult.setText(re);
            }
 catch (ArithmeticException ce) {
                txtResult.setText(
"除数不能为零");
            }
 catch (Exception ee) {
            }

            
if (!flag3)
                flag3 
= true;
    }


    
public void btnPoint_actionPerformed(ActionEvent e) {
        
int num=txtResult.getText().indexOf(".");
        
if(num<0 && !flag)
            txtResult.setText(txtResult.getText()
+e.getActionCommand());
        
if(flag)
            flag1
=true;
    }


    
public void btnBegin_actionPerformed(ActionEvent e) {
        flag
=false;
        flag1
=false;
        flag2
=false;
        flag3
=false;
        front
="";
        behind
="";
        re
="";
        txtResult.setText(
"0");
    }


    
public void btnMinus_actionPerformed(ActionEvent e) {//取反运算符事件处理
        if(txtResult.getText().equals("0")){//如果文本框内容为0
            txtResult.setText(txtResult.getText());
        }
else if(txtResult.getText().indexOf("-")>=0){//若文本框中含有负号
            String a=txtResult.getText().replaceAll("-","");
            txtResult.setText(a);
        }
else if(flag){
            txtResult.setText(
"0");
        }
else{
            txtResult.setText(
"-"+txtResult.getText());
        }

    }


    
public void btnCancel_actionPerformed(ActionEvent e) {//退格事件处理方法
        String str=txtResult.getText();
        
if(str.length() == 1){//如文本框中只剩下最后一个字符,将文本框内容置为0
            txtResult.setText("0");
        }

        
if(str.length()>1){
            str
=str.substring(0,str.length()-1);
            txtResult.setText(str);
        }

    }


    
public static void main(String[] args){
        FrameCalculate fc 
= new FrameCalculate();
        fc.setSize(
400,310);
        fc.setLocation(
200,150);
        fc.setVisible(
true);
    }

}



class FrameCalculate_btnCancel_actionAdapter implements ActionListener {
    
private FrameCalculate adaptee;
    FrameCalculate_btnCancel_actionAdapter(FrameCalculate adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnCancel_actionPerformed(e);
    }

}



class FrameCalculate_btnMinus_actionAdapter implements ActionListener {
    
private FrameCalculate adaptee;
    FrameCalculate_btnMinus_actionAdapter(FrameCalculate adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnMinus_actionPerformed(e);
    }

}



class FrameCalculate_btnBegin_actionAdapter implements ActionListener {
    
private FrameCalculate adaptee;
    FrameCalculate_btnBegin_actionAdapter(FrameCalculate adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnBegin_actionPerformed(e);
    }

}



class FrameCalculate_btnPoint_actionAdapter implements ActionListener {
    
private FrameCalculate adaptee;
    FrameCalculate_btnPoint_actionAdapter(FrameCalculate adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnPoint_actionPerformed(e);
    }

}



class FrameCalculate_btnEqual_actionAdapter implements ActionListener {
    
private FrameCalculate adaptee;
    FrameCalculate_btnEqual_actionAdapter(FrameCalculate adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnEqual_actionPerformed(e);
    }

}



class FrameCalculate_btnIncrease_actionAdapter implements ActionListener {
    
private FrameCalculate adaptee;
    FrameCalculate_btnIncrease_actionAdapter(FrameCalculate adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnIncrease_actionPerformed(e);
    }

}



class FrameCalculate_btnZero_actionAdapter implements ActionListener {
    
private FrameCalculate adaptee;
    FrameCalculate_btnZero_actionAdapter(FrameCalculate adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnZero_actionPerformed(e);
    }

}



class FrameCalculate_btnNull_actionAdapter implements ActionListener {
    
private FrameCalculate adaptee;
    FrameCalculate_btnNull_actionAdapter(FrameCalculate adaptee) 
{
        
this.adaptee = adaptee;
    }


    
public void actionPerformed(ActionEvent e) {
        adaptee.btnNull_actionPerformed(e);
    }

}

点击下载源代码:/Files/daizhenghenry/calculate.rar

posted on 2008-03-06 14:28 非凡DZ 阅读(595) 评论(0)  编辑  收藏 所属分类: J2SE


只有注册用户登录后才能发表评论。


网站导航:
 
<2008年3月>
2425262728291
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

常用链接

留言簿(2)

随笔分类(19)

随笔档案(19)

友情链接

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜