日期:2014-05-20  浏览次数:20761 次

java计算器
最近刚学到javagui编程,自己写了一个java计算器但是还差按钮的事件处理没有搞好!我试了很多次都还是不会弄!我的源代码是这样的,就还差按钮事件处理,求高手帮忙写一下!
/**
 * @(#)Calculator.java
 *
 *
 * @author 
 * @version 1.00 2012/4/17
 */

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

class Calculatorframe extends Frame implements ActionListener
{
JTextField jtf;
MenuBar menubar;//菜单条
Menu m1,m2,m3;
MenuItem mi1,mi2,mi3,mi4,mi5,mi6;
//计算器中的按钮
Button MC,MR,MS,MJIA,MJIAN;
Button Back,CE,C,zheng_fu,sqrt;
Button qi,ba,jiu,chu,quyu;
Button si,wu,liu,cheng,yix;
Button yi,er,san,jian;
Button ling,point,jia;
Button deng;
StringBuffer str;//显示屏所显示的字符串 

double x,y;//定义的两个计算操作数

//String s1,s2;//

final int slength=30;//设置结果显示的有效长度

int z;//开关语句表达式用于确定加减乘除运算

Calculatorframe()
{
super("简单计算器");
this.setLayout(null);

menubar=new MenuBar();
m1=new Menu("查看(V)");
m2=new Menu("编辑(E)");
m3=new Menu("帮助(H)");
mi1=new MenuItem("标准型");
mi2=new MenuItem("科学型");
mi3=new MenuItem("程序员");
mi4=new MenuItem("复制");
mi5=new MenuItem("粘贴");
mi6=new MenuItem("关于计算器");
mi6.addActionListener(this);
//计算结果的显示框
jtf=new JTextField(35);
jtf.setBounds(10,55,195,46);
jtf.setEditable(true);
//jtf.setText();
jtf.setHorizontalAlignment(JTextField.RIGHT);
//按钮位置的设定以及为按钮注册监听器
MC=new Button("MC");
MC.setBounds(10,110,35,30);
MC.addActionListener(this);

MR=new Button("MR");
MR.setBounds(50,110,35,30);
MR.addActionListener(this);

MS=new Button("MS");
MS.setBounds(90,110,35,30);
MS.addActionListener(this);

MJIA=new Button("M+");
MJIA.setBounds(130,110,35,30);
MJIA.addActionListener(this);

MJIAN=new Button("M-");
MJIAN.setBounds(170,110,35,30);
MJIAN.addActionListener(this);

Back=new Button("Back");
Back.setBounds(10,145,35,30);
Back.addActionListener(this);

CE=new Button("CE");
CE.setBounds(50,145,35,30);
CE.addActionListener(this);

C=new Button("C");
C.setBounds(90,145,35,30);
C.addActionListener(this);

zheng_fu=new Button("+/-");
zheng_fu.setBounds(130,145,35,30);
zheng_fu.addActionListener(this);

sqrt=new Button("sqrt");
sqrt.setBounds(170,145,35,30);
sqrt.addActionListener(this);

qi=new Button("7");
qi.setBounds(10,180,35,30);
qi.addActionListener(this);

ba=new Button("8");
ba.setBounds(50,180,35,30);
ba.addActionListener(this);

jiu=new Button("9");
jiu.setBounds(90,180,35,30);
jiu.addActionListener(this);

chu=new Button("/");
chu.setBounds(130,180,35,30);
chu.addActionListener(this);

quyu=new Button("%");
quyu.setBounds(170,180,35,30);
quyu.addActionListener(this);

si=new Button("4");
si.setBounds(10,215,35,30);
si.addActionListener(this);

wu=new Button("5");
wu.setBounds(50,215,35,30);
wu.addActionListener(this);

liu=new Button("6");
liu.setBounds(90,215,35,30);
liu.addActionListener(this);

cheng=new Button("*");
cheng.setBounds(130,215,35,30);
cheng.addActionListener(this);

yix=new Button("1/x");
yix.setBounds(170,215,35,30);
yix.addActionListener(this);