菜鸟求教!一个java的计算器的按钮事件的设计思路应该怎样设计?

解决方案 »

  1.   

    "2""3" "+" "4" "=" 每次按button获取一个字符。 拼字符串会么?  或者.equals("+") 就标记了加法, 按"="的时候,根据的值动态的调用add方法。 
    public static double result (double a, double b, String ) {
        if(.equals("+")) {
            add(a,b);
        } else if () {}
    }
      

  2.   

    swing么?用swing的按钮事件就可以了 button.addActionListener
      

  3.   

    http://download.csdn.net/detail/wuhuipengwhp/3554540
      

  4.   

    计算器代码 供楼主参考:
    package work;import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    public class Calculator 

    public static void main(String[] args) 

    CalculatorFrame frame = new CalculatorFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(200,200); 
    frame.setVisible(true); 


    class CalculatorFrame extends JFrame 

    public CalculatorFrame() 

    setTitle("Calculator"); 
    CalculatorPanel panel = new CalculatorPanel(); 
    add(panel); 
    pack(); 



    class CalculatorPanel extends JPanel 

    private JLabel display; 
    private JPanel panel; 
    private double result; 
    private String lastCommand; 
    private boolean start; 

    public CalculatorPanel() 

    setLayout(new BorderLayout()); 

    result = 0; 
    lastCommand = "="; 
    start = true; 
    display = new JLabel("0",JLabel.RIGHT); 
    display.setEnabled(false); 
    add(display, BorderLayout.NORTH); 

    ActionListener insert = new InsertAction(); 
    ActionListener command = new CommandAction(); 
    panel = new JPanel(); 
    panel.setLayout(new GridLayout(4, 4)); 
    addButton("7", insert); 
    addButton("8", insert); 
    addButton("9", insert); 
    addButton("+", command); 
    addButton("4", insert); 
    addButton("5", insert); 
    addButton("6", insert); 
    addButton("-", command); 
    addButton("1", insert); 
    addButton("2", insert); 
    addButton("3", insert); 
    addButton("*", command); 
    addButton("0", insert); 
    addButton(".", insert); 
    addButton("=", command); 
    addButton("/", command); 
    add(panel, BorderLayout.CENTER); 


    private void addButton(String label, ActionListener listener) 

    JButton button = new JButton(label); 
    button.addActionListener(listener); 
    panel.add(button); 

    private class InsertAction implements ActionListener 

    public void actionPerformed(ActionEvent event) 

    String input = event.getActionCommand(); 
    if (start) 

    display.setText(""); 
    start = false; 

    display.setText(display.getText() + input); 


    private class CommandAction implements ActionListener 

    public void actionPerformed(ActionEvent event) 

    String command = event.getActionCommand(); 

    if (start) 

    if (command.equals("-")) 

    display.setText(command); 
    start = false; 

    else 
    lastCommand = command; 

    else 

    calculate(Double.parseDouble(display.getText())); 
    lastCommand = command; 
    start = true; 



    public void calculate(double x) 

    if (lastCommand.equals("+")) result += x; 
    else if (lastCommand.equals("-")) result -= x; 
    else if (lastCommand.equals("*")) result *= x; 
    else if (lastCommand.equals("/")) result /= x; 
    else if (lastCommand.equals("=")) result = x; 
    display.setText("" + result); 

    }
      

  5.   

    (上半部分代码)import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;/**********************Java计算器 主类*********************/
    public class SunnyCalculator implements ActionListener {
        JFrame f;
        JMenu mEdit;
        JMenu mView;
        JMenu mHelp;
        JMenuItem mCopy;
        JMenuItem mPaste;
        JTextField tResult;
        JButton bNumber;
        JButton bOperator;
        JButton bOther;
        JButton bM;
        boolean isDouble=false;//是否为实数
        int opFlag=-1;
        static double t1=0,t2=0,t3=0,result=0;
        static int opflag1=-1,opflag2=-1,flag=0,resflag=1;
        int preOp,currentOp=0;//标准位
        double op1=0,op2=0;//操作数
        double n3;
        StringBuffer buf=new StringBuffer(20);
        StringBuffer copyBoard=new StringBuffer(20);//剪贴板
        StringBuffer memory=new StringBuffer(20);//M系列
        StringBuffer str=new StringBuffer();
        //Java计算器 构造器
        public SunnyCalculator()
        {
            f = new JFrame("Sunny计算器_杨梅树的盔甲");
            Container contentPane = f.getContentPane();
            /**************************Java计算器 菜单的创建*****************************/
            JMenuBar mBar = new JMenuBar();
            mBar.setOpaque(true);
            mEdit = new JMenu("编辑(E)");
            mEdit.setMnemonic(KeyEvent.VK_E);
            mCopy = new JMenuItem("复制(C)");
            mEdit.add(mCopy);
            mPaste = new JMenuItem("粘贴(P)");
            mEdit.add(mPaste);
            mView = new JMenu("查看(V)");
            mView.setMnemonic(KeyEvent.VK_V);
            mView.add(new JMenuItem("标准型"));
            mView.add(new JMenuItem("科学型"));
            mView.addSeparator();
            mView.add(new JMenuItem("查看分组"));
            mHelp = new JMenu("帮助(H)");
            mHelp.setMnemonic(KeyEvent.VK_H);
            mHelp.add(new JMenuItem("帮助主题"));
            mHelp.addSeparator();
            mHelp.add(new JMenuItem("关于计算器"));
            mBar.add(mEdit);
            mBar.add(mView);
            mBar.add(mHelp);
            f.setJMenuBar(mBar);
            contentPane.setLayout(new BorderLayout());
            JPanel pTop = new JPanel();
            tResult = new JTextField("0.",26);
            tResult.setHorizontalAlignment(JTextField.RIGHT);
            tResult.setEditable(false);
            pTop.add(tResult);
            contentPane.add(pTop,BorderLayout.NORTH);
            JPanel pBottom = new JPanel();
            pBottom.setLayout(new BorderLayout());
            JPanel pLeft = new JPanel();
            pLeft.setLayout(new GridLayout(5,1,3,3));
            bM = new JButton(" ");
            bM.setEnabled(false);
            pLeft.add(bM);        /*************************Java计算器 功能键定义***************************/
            bOther = new JButton("MC");
            bOther.addActionListener(this);
            bOther.setForeground(Color.red);
            bOther.setMargin(new Insets(3,2,3,2));
            pLeft.add(bOther);        bOther = new JButton("MR");
            bOther.addActionListener(this);
            bOther.setForeground(Color.red);
            bOther.setMargin(new Insets(3,2,3,2));
            pLeft.add(bOther);        bOther = new JButton("MS");
            bOther.addActionListener(this);
            bOther.setForeground(Color.red);
            bOther.setMargin(new Insets(3,2,3,2));
            pLeft.add(bOther);        bOther = new JButton("M+");
            bOther.addActionListener(this);
            bOther.setForeground(Color.red);
            bOther.setMargin(new Insets(3,2,3,2));
            pLeft.add(bOther);        pBottom.add(pLeft,BorderLayout.WEST);
            JPanel pRight = new JPanel();
            pRight.setLayout(new BorderLayout());
            JPanel pUp = new JPanel();
            pUp.setLayout(new GridLayout(1,3,3,0));        bOther = new JButton("BackSpace");
            bOther.addActionListener(this);
            bOther.setForeground(Color.red);
            bOther.setMargin(new Insets(3,0,3,5));
            pUp.add(bOther);        bOther = new JButton("CE");
            bOther.addActionListener(this);
            bOther.setForeground(Color.red);
            pUp.add(bOther);        bOther = new JButton("C");
            bOther.addActionListener(this);
            bOther.setForeground(Color.red);
            pUp.add(bOther);        /***************************Java计算器 数字键盘区定义**************************/
            JPanel pDown = new JPanel();
            pDown.setLayout(new GridLayout(4,5,3,2));
            bNumber = new JButton("7");
            bNumber.setForeground(Color.blue);
            bNumber.addActionListener(this);
            bNumber.setMargin(new Insets(3,3,3,3));
            pDown.add(bNumber);
            bNumber = new JButton("8");
            bNumber.setForeground(Color.blue);
            bNumber.addActionListener(this);
            bNumber.setMargin(new Insets(3,3,3,3));
            pDown.add(bNumber);
            bNumber = new JButton("9");
            bNumber.setForeground(Color.blue);
            bNumber.addActionListener(this);
            bNumber.setMargin(new Insets(3,3,3,3));
            pDown.add(bNumber);
            bOperator = new JButton("/");
            bOperator.setForeground(Color.red);
            bOperator.addActionListener(this);
            bOperator.setMargin(new Insets(3,0,3,0));
            pDown.add(bOperator);
            bOperator = new JButton("sqrt");
            bOperator.addActionListener(this);
            bOperator.setForeground(Color.red);
            bOperator.setMargin(new Insets(3,0,3,0));
            pDown.add(bOperator);
            bNumber = new JButton("4");
            bNumber.setForeground(Color.blue);
            bNumber.addActionListener(this);
            bNumber.setMargin(new Insets(3,3,3,3));
            bNumber.setHorizontalTextPosition(JButton.LEFT);
            pDown.add(bNumber);
            bNumber = new JButton("5");
            bNumber.setForeground(Color.blue);
            bNumber.addActionListener(this);
            bNumber.setMargin(new Insets(3,3,3,3));
            pDown.add(bNumber);
            bNumber = new JButton("6");
            bNumber.setForeground(Color.blue);
            bNumber.addActionListener(this);
            bNumber.setMargin(new Insets(3,3,3,3));
            pDown.add(bNumber);
            bOperator = new JButton("*");
            bOperator.setForeground(Color.red);
            bOperator.addActionListener(this);
            bOperator.setMargin(new Insets(3,3,3,3));
            pDown.add(bOperator);
            bOperator = new JButton("%");
            bOperator.setForeground(Color.blue);
            bOperator.addActionListener(this);
            bOperator.setMargin(new Insets(3,3,3,3));
            pDown.add(bOperator);
            bNumber = new JButton("1");
            bNumber.setForeground(Color.blue);
            bNumber.addActionListener(this);
            bNumber.setMargin(new Insets(3,3,3,3));
            pDown.add(bNumber);
            bNumber = new JButton("2");
            bNumber.setForeground(Color.blue);
            bNumber.addActionListener(this);
            bNumber.setMargin(new Insets(3,3,3,3));
            pDown.add(bNumber);
            bNumber = new JButton("3");
            bNumber.setForeground(Color.blue);
            bNumber.addActionListener(this);
            bNumber.setMargin(new Insets(3,3,3,3));
            pDown.add(bNumber);
            bOperator = new JButton("-");
            bOperator.setForeground(Color.red);
            bOperator.addActionListener(this);
            bOperator.setMargin(new Insets(3,3,3,3));
            pDown.add(bOperator);
            bOperator = new JButton("1/x");
            bOperator.setForeground(Color.blue);
            bOperator.addActionListener(this);
            pDown.add(bOperator);
            bNumber = new JButton("0");
            bNumber.setForeground(Color.blue);
            bNumber.addActionListener(this);
            bNumber.setMargin(new Insets(3,3,3,3));
            pDown.add(bNumber);
            bOperator = new JButton("+/-");
            bOperator.setForeground(Color.blue);
            bOperator.addActionListener(this);
            bOperator.setMargin(new Insets(3,3,3,3));
            pDown.add(bOperator);
            bOperator = new JButton(".");
            bOperator.setForeground(Color.blue);
            bOperator.addActionListener(this);
            bOperator.setMargin(new Insets(3,3,3,3));
            pDown.add(bOperator);
            bOperator = new JButton("+");
            bOperator.setForeground(Color.blue);
            bOperator.addActionListener(this);
            bOperator.setMargin(new Insets(3,3,3,3));
            pDown.add(bOperator);
            bOperator = new JButton("=");
            bOperator.setForeground(Color.blue);
            bOperator.addActionListener(this);
            bOperator.setMargin(new Insets(3,3,3,3));
            pDown.add(bOperator);
            pRight.add(pUp,BorderLayout.NORTH);
            pRight.add(pDown,BorderLayout.SOUTH);
            pBottom.add(pRight,BorderLayout.EAST);
            contentPane.add(pBottom,BorderLayout.SOUTH);
            f.setSize(new Dimension(320,256));
            f.setResizable(false);
            f.setVisible(true);
            f.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            }
            );
        }
      

  6.   

    (下半部分代码)
        /************************Java计算器 计算方法区***************************/
        public void actionPerformed(ActionEvent e)
        {
            String s = e.getActionCommand();
            if(s.equals("复制(C)"))
            {
                String temp = tResult.getText().trim();
                copyBoard.replace(0, copyBoard.length(), temp);
                mPaste.setEnabled(true);
            }
            else if(s.equals("粘贴(p)"))
            {
                tResult.setText(copyBoard.toString()); 
            }
            else if(s.equals("CE"))
            {
                //如果是CE则清除文本框
                tResult.setText("0.");
            }
            else if(s.equals("BackSpace"))
            {
                if(!tResult.getText().trim().equals("0."))
                {
                    //如果文本框中有内容
                    if(str.length()!=1 && str.length()!=0)
                    {
                        tResult.setText(str.delete(str.length()-1,str.length()).toString());
                    }
                    else
                    {
                        tResult.setText("0.");
                        str.setLength(0);
                    }
                }
                op2 = Double.parseDouble(tResult.getText().trim());
            }
            else if(s.equals("C"))
            {
                //如果是C删除当前计算
                tResult.setText("0.");
                op1 = op2 = 0;
                str.replace(0, str.length(), " ");
                preOp = currentOp = 0;
            }
            else if(s.equals("MC"))
            {
                //如果是MC则清除缓冲区
                String temp = "";
                memory.replace(0, memory.length(), temp);
                bM.setText(" ");
            }
            else if(s.equals("MR"))
            {
                //如果按键为MR则恢复缓冲区的数到文本框
                tResult.setText(memory.toString());
            }
            else if(s.equals("MS"))
            {
                //如果按键为MS则将文本框的数存入缓冲区
                String s1 = tResult.getText().trim();
                memory.replace(0, memory.length(), s1);
                bM.setText("M");
            }
            else if(s.equals("M+"))
            {
                //如果按键为MS则将文本框值与缓冲区的数相加但不显示结果
                String temp1 = tResult.getText().trim();
                double dtemp = Double.parseDouble(temp1);
                String temp2 = memory.toString();
                dtemp += Double.parseDouble(temp2);
                temp1 = String.valueOf(dtemp);
                memory.replace(0, memory.length(), temp1);
            }
            else if(s.equals("1/x"))
            {
                //如果按键为1/x则将文本框中的数据为它的倒数
                String temp = tResult.getText().trim();
                double dtemp = Double.parseDouble(temp);
                tResult.setText(""+1/dtemp);
            }
            else if(s.equals("sqrt"))
            {
                //如果按键为sqrt则将文本框中的内容求平方根
                String temp = tResult.getText().trim();
                double dtemp = Double.parseDouble(temp);
                tResult.setText(""+Math.sqrt(dtemp));
            }
            else if(s.equals("+"))
            {
                str.setLength(0);
                if(currentOp==0)
                {
                    preOp = currentOp = 1;
                    op2 = 0;
                    tResult.setText(""+op1);
                }
                else
                {
                    currentOp = preOp;
                    preOp = 1;
                    switch(currentOp){
                    case 1:
                        op1 += op2;
                        tResult.setText(""+op1);
                        break;
                    case 2:
                        op1 -= op2;
                        tResult.setText(""+op1);
                        break;
                    case 3:
                        op1 *= op2;
                        tResult.setText(""+op1);
                        break;
                    case 4:
                        op1 /= op2;
                        tResult.setText(""+op1);
                        break;
                    }
                }
            }
            else if(s.equals("-")){ 
                str.setLength(0); 
                if(currentOp==0) 
                { 
                    preOp=currentOp=2;//op1=op2;op2=0; 
                    tResult.setText(""+op1); 
                } 
                else 
                { 
                    currentOp =preOp; 
                    preOp =2; 
                    switch(currentOp){ 
    case 1:op1=op1+op2;tResult.setText(""+op1);break; 
    case 2:op1=op1-op2;tResult.setText(""+op1);break; 
    case 3:op1=op1*op2;tResult.setText(""+op1);break; 
    case 4:op1=op1/op2;tResult.setText(""+op1);break; 
                    } 
                } 
            } 
            else if(s.equals("*"))//* 
            { 
                str.setLength(0); 
                if(currentOp==0) 
                { 
                    preOp=currentOp=3;//op1=op2;op2=1; 
                    tResult.setText(""+op1);//op1=op2; 
                } 
                else 
                { 
                    currentOp =preOp; 
                    preOp =3; 
                    switch(currentOp){ 
    case 1:op1=op1+op2;tResult.setText(""+op1);break; 
    case 2:op1=op1-op2;tResult.setText(""+op1);break; 
    case 3:op1=op1*op2;tResult.setText(""+op1);break; 
    case 4:op1=op1/op2;tResult.setText(""+op1);break; 
                    } 
                } 
            } 
            else if(s.equals("/"))// / 
            { 
                str.setLength(0); 
                if(currentOp==0) 
                { 
                    preOp=currentOp=4;//op2=1; 
                    tResult.setText(""+op1);//op1=op2; 
                } 
                else 
                { 
                    currentOp =preOp; 
                    preOp =4; 
                    switch(currentOp){ 
    case 1:op1=op1+op2;tResult.setText(""+op1);break; 
    case 2:op1=op1-op2;tResult.setText(""+op1);break; 
    case 3:op1=op1*op2;tResult.setText(""+op1);break; 
    case 4:op1=op1/op2;tResult.setText(""+op1);break; 
                    } 
                } 
            } 
            else if(s.equals("="))// = 
            { 
                if(currentOp==0) 
                { 
                    str.setLength(0); 
                    tResult.setText(""+op2); 
                } 
                else 
                { 
                    str.setLength(0); 
                    currentOp =preOp; 
                    switch(currentOp){ 
    case 1:op1=op1+op2;tResult.setText(""+op1);break; 
    case 2:op1=op1-op2;tResult.setText(""+op1);break; 
    case 3:op1=op1*op2;tResult.setText(""+op1);break; 
    case 4:op1=op1/op2;tResult.setText(""+op1);break; 
                    } 
                    currentOp=0; 
                    op2=0; 
                } 
            } 
            else if(s.equals(".")) 
            { 
                isDouble=true; 
                if(tResult.getText().trim().indexOf('.')!=-1); 
                else { 
                    if(tResult.getText().trim().equals("0")) { 
                        str.setLength(0); 
                        tResult.setText((str.append("0"+s)).toString()); 
                    } 
                    //else if(tResult.getText().trim().equals("")){}//如果初时显示为空则不做任何操作 
                    else { 
                        tResult.setText((str.append(s)).toString()); 
                    } 
                } 
            } 
            else if(s.equals("0"))//如果选择的是"0"这个数字键 
            { 
                if(tResult.getText().trim().equals("0.")){} 
                else{ 
                    tResult.setText(str.append(s).toString()); 
                    op2=Double.parseDouble(tResult.getText().trim()); 
                } 
            } 
            else{ 
                tResult.setText(str.append(s).toString()); 
                op2=Double.parseDouble(tResult.getText().trim()); 
                if(currentOp==0) 
                    op1=op2; 
            }
        }//end actionPerformed
        public static void main(String[] args) { 
            new SunnyCalculator(); 
        }
    }
      

  7.   

    表示我也写过 给你看看代码吧 我的逻辑实现不好
    ackage ziji;
    //按等号连加可以实现(正数)  减乘除不可以    一个数除以另一个数后 结果为小数  用Back那个键删除后 在小数点后接着继续加数字
    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.JTextField;public class text2 extends JFrame{
    JTextField textField;
     static String str=" ";
     double a1,a2;              //a1是计算前的值。a2是计算后的值。
     double resl;               //存放结果的变量 
     static String temp;            //判断的临时变量
     static boolean b=false;    //判断的临时变量
    public text2(){
    Container container=getContentPane();
    JMenuBar menu=new JMenuBar();
    JMenu file=new JMenu("编辑(E)");
    JMenu file1=new JMenu("查看(U)");
    JMenu file2=new JMenu("帮助(H)");
       
    JButton bt=new JButton("Backspace");
    JButton bt1=new JButton("CE");
    JButton bt2=new JButton("C");

    JButton c[]={new JButton("7"),
    new JButton("8"),
    new JButton("9"),
    new JButton("/"),
    new JButton("sqrt"),
    new JButton("4"),
    new JButton("5"),
    new JButton("6"),
    new JButton("*"),
    new JButton("%"),
    new JButton("1"),
    new JButton("2"),
    new JButton("3"),
    new JButton("-"),
    new JButton("1/x"),
    new JButton("0"),
    new JButton("+/-"),
    new JButton("."),
    new JButton("+"),
    new JButton("=")};

    file.add("复制(C)");
    file.add("粘贴(V)");

    file1.add("标准型(T)");
    file1.add("标准型(S)");
    file1.addSeparator();
    file1.add("数字分组(I)");

    file2.add("帮助主题(H)");
    file2.addSeparator();
    file2.add("关于计算机(A)");
    menu.add(file);
    menu.add(file1);
    menu.add(file2);
    JPanel jian=new JPanel();
    jian.setLayout(new GridLayout(4,5,3,3));
    for(int i=0;i<c.length;i++){
    jian.add(c[i]);}

    JPanel a=new JPanel();
    JPanel g=new JPanel();g.setLayout(new GridLayout(1,3));
    g.add(bt);
    g.add(bt1);
    g.add(bt2); 


     textField = new JTextField("0",35);
    textField.setHorizontalAlignment(JTextField.RIGHT);
    a.add(textField);
    a.add(g);

    container.setLayout(new BorderLayout());
    container.add(menu,BorderLayout.NORTH);
    container.add(a);
    container.add(jian,BorderLayout.SOUTH);

    bt2.addActionListener(new ActionListener(){      //为C键设置监听
                  public void actionPerformed(ActionEvent e) {
    textField.setText("0");
    b=false;
    }
    });

    bt.addActionListener(new ActionListener(){//为Backspace键设置监听
                public void actionPerformed(ActionEvent e) {
    if(textField.getText()!="0"&&textField.getText().length()!=1){
    b=false;
    textField.setText(textField.getText().substring(0,textField.getText().length()-1));
    }else{
    textField.setText("0");
    }
    }
    });
     class Action implements ActionListener{               //为数字按钮设置监听 public void actionPerformed(ActionEvent e) {
    if(textField.getText().equals("0")){
    textField.setText(e.getActionCommand());
    }else if(b==true){
    // str+=e.getActionCommand();
    // str.trim();
    textField.setText(e.getActionCommand());
    b=false;
    }
    else{
    String st=textField.getText()+e.getActionCommand();
    textField.setText(st);

    }


    }
    }
     Action action=new Action (); c[0].addActionListener(action);
    c[1].addActionListener(action);
    c[2].addActionListener(action);
    c[5].addActionListener(action);
    c[6].addActionListener(action);
    c[7].addActionListener(action);
    c[10].addActionListener(action);
    c[11].addActionListener(action);
    c[12].addActionListener(action);
    c[15].addActionListener(action);

    c[17].addActionListener(new ActionListener(){//为点号设置监听 public void actionPerformed(ActionEvent e) {
    if(!textField.getText().endsWith(".")&&!(textField.getText().contains(".")))
    textField.setText(textField.getText()+".");
    }
    });

    c[16].addActionListener(new ActionListener(){//为+/-号设置监听 public void actionPerformed(ActionEvent e) {
    if(textField.getText()!="0"){
    double a=-Double.valueOf(textField.getText());
    textField.setText(String.valueOf(a));
    }

    }

    });



    c[18].addActionListener(new ActionListener(){//为加号设置监听 public void actionPerformed(ActionEvent e) {
    b=true;
    temp="+";
    a1=Double.valueOf(textField.getText());
                                                                 System.out.println("a1="+a1);
    }});
    c[13].addActionListener(new ActionListener(){//为减号设置监听
    public void actionPerformed(ActionEvent e) {
    b=true;
    temp="-";
        a1=Double.valueOf(textField.getText());
        System.out.println(a1);
     }});
    c[8].addActionListener(new ActionListener(){//为乘号设置监听
                 public void actionPerformed(ActionEvent e) {
                  b=true;
                     a1=Double.valueOf(textField.getText());
     temp="*";
    }});
    c[3].addActionListener(new ActionListener(){//为除号设置监听   
                 public void actionPerformed(ActionEvent e) {
                  b=true;
     a1=Double.valueOf(textField.getText());
     temp="/";

    }});



    c[19].addActionListener(new ActionListener(){ //为=号注册监听器 public void actionPerformed(ActionEvent e) {
    b=true;
    double a2=Double.valueOf(textField.getText());
                                                  System.out.println("a2="+a2);
    if(temp=="+"){ 
           textField.setText(String.valueOf(a1+a2));
               a1=a2;                               //保证连续按=号输出正确结果
           }
    if(temp=="-"){ 
    textField.setText(String.valueOf(a1-a2));}

    if(temp=="*"){
    textField.setText(String.valueOf(a1*a2));}
    if(temp=="/"){ 
    textField.setText(String.valueOf(a1/a2));}
    }
    });

    setTitle("计算器 明明版");
    setSize(400, 250);
    setVisible(true) ; 
    setResizable(false);
    }      public static void main(String args[]) {
          text2 a=new text2  ();
             }
       
    }