前几天的作业刚做了一个
你留个 E_Mail 吧

解决方案 »

  1.   

    [email protected]
    非常感谢
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import javabook.*;class CalculatorMain
    {
       public static void main (String[] args)
       {
        Calculator calculator;
        calculator =new Calculator();
        calculator.setVisible(true);
       }
    }class Calculator extends Frame implements ActionListener,WindowListener
    {
        
        //data members
       
        private static final int LEFT=0;
        private static final int RIGHT=1;  
        private int status;
        private TextField     operand;    private Button         plusButton,
                               minusButton,
                               multiplyButton,
      divideButton,
      clearButton,
            setZeroButton,
      Button1, Button2,Button3,
      Button4, Button5, Button6,
      Button7, Button8, Button9,
      Button0,
      equalButton,
      pointButton;
        private StringBuffer  leftoper,rightoper;
        private String        opr,str;    //public methods
        public Calculator()
        {
    //set Window properties
    super        ("Calculator");
    setSize      (200,230);
    setResizable (false);
    setLayout    (null);
    setLocation  ( 250,250); initGUIComponents();
    operand.setText("0.");
    status=LEFT;
    opr="=";
    leftoper  =new StringBuffer("");
    rightoper =new StringBuffer("0");
    //create and lay out components

    addWindowListener( this );
        }    public void windowClosing(WindowEvent event)
        {
    System.exit(0);
        }
        public void windowActivated   (WindowEvent event) {}
        public void windowClosed      (WindowEvent event) {}
        public void windowDeactivated (WindowEvent event) {} 
        public void windowDeiconified (WindowEvent event) {} 
        public void windowIconified   (WindowEvent event) {}
        public void windowOpened      (WindowEvent event) {} 
        public void actionPerformed (ActionEvent event)
        {
            str=event.getActionCommand();
            if(str.equals("."))
            {
              {if(status==LEFT) {getLeftOpr();}
               else if(status==RIGHT) {getRightOpr();}
              }                                        
            }
            for(int i=0;i<=9;i++)
            {
              if(Convert.toString(i).equals(str))
              { if(status==LEFT) {getLeftOpr();}
                else if(status==RIGHT) { getRightOpr();}
              }
    }
            
    getcompute();
        }    //private methods
        private void initGUIComponents()
        {
    //create buttons
    operand =new TextField();
          
    Button1 =new Button("1");
        Button2 =new Button("2");
        Button3 =new Button("3");
    Button4 =new Button("4");
    Button5 =new Button("5");
    Button6 =new Button("6");
    Button7 =new Button("7");
    Button8 =new Button("8");
    Button9 =new Button("9");
    Button0   =new Button("0");
        equalButton =new Button("=");
        pointButton =new Button(".");
    plusButton      =new Button("+");
        minusButton     =new Button("-");
        multiplyButton  =new Button("*");
        divideButton    =new Button("/");
    clearButton     =new Button("CE");
        setZeroButton   =new Button("C"); //position buttons
    operand.         setBounds(20,40,95,25);        plusButton.      setBounds(125,145,60,25);
            minusButton.     setBounds(125,110,25,25);
            multiplyButton.  setBounds(125,75,25,25);
            divideButton.    setBounds(160,75,25,25);
            clearButton.     setBounds(125,180,60,25);
            setZeroButton.   setBounds(160,110,25,25);
            Button1.         setBounds(20,145,25,25);
            Button2.         setBounds(55,145,25,25);
            Button3.         setBounds(90,145,25,25);
            Button4.         setBounds(20,110,25,25);
            Button5.         setBounds(55,110,25,25);
            Button6.         setBounds(90,110,25,25);
            Button7.         setBounds(20,75,25,25);
    Button8.         setBounds(55,75,25,25);
    Button9.         setBounds(90,75,25,25);
    Button0.         setBounds(20,180,25,25);
    equalButton.     setBounds(90,180,25,25);
    pointButton.     setBounds(55,180,25,25); //add the Calculator class as an action listener to all buttons
            plusButton.      addActionListener(this);
            minusButton.     addActionListener(this);
            multiplyButton.  addActionListener(this);
            divideButton.    addActionListener(this);
            clearButton.     addActionListener(this);
            setZeroButton.   addActionListener(this);
            Button1.         addActionListener(this);
            Button2.         addActionListener(this);
            Button3.         addActionListener(this);
            Button4.         addActionListener(this);
            Button5.         addActionListener(this);
            Button6.         addActionListener(this);
            Button7.         addActionListener(this);
            Button8.         addActionListener(this);
            Button9.         addActionListener(this);
            Button0.         addActionListener(this);
            equalButton.     addActionListener(this);
            pointButton.     addActionListener(this); //add buttons to the calculator frame
    add(operand);
    add(plusButton);
    add(minusButton);
    add(multiplyButton);
    add(divideButton);
    add(clearButton);
    add(setZeroButton);
    add(Button1);
    add(Button2);
    add(Button3);
    add(Button4);
    add(Button5);
    add(Button6);
    add(Button7);
    add(Button8);
    add(Button9);
    add(Button0);
    add(equalButton);
    add(pointButton);
        }
        private void getcompute ()
        {

            if(str.equals("CE"))
              { clear();}
            if(str.equals("="))
              {
                compute();
                opr="=";
              }
            if(str.equals("+"))
              {
                compute();
                opr="+";
                status=RIGHT;
              }
            if(str.equals("-"))
              {
              compute();
              opr="-";
              status=RIGHT;
              }
            if(str.equals("*"))
              {
               compute();
               opr="*";
               status=RIGHT;
              }
            if(str.equals("/"))
              {
                compute();
                opr="/";
                status=RIGHT;
              }
            

        }    private void getLeftOpr()
        {
          leftoper.append(str);
          operand.setText(leftoper.toString());
        }   private void getRightOpr()
       {
          rightoper.append(str);
          operand.setText(rightoper.toString());
       }   private void clear()    
       {leftoper=new StringBuffer("");
        rightoper=new StringBuffer("0");
        status=LEFT;
        opr="=";
        operand.setText("0.");
       }    private void compute()   
       {
        double result=Convert.toDouble(leftoper.toString());
        if(opr.equals("+"))
        {
        result=Convert.toDouble(leftoper.toString())+Convert.toDouble(rightoper.toString());
        }
        if(opr.equals("-"))
        {
        result=Convert.toDouble(leftoper.toString())-Convert.toDouble(rightoper.toString());
        }
        if(opr.equals("*"))
        {
        result=Convert.toDouble(leftoper.toString())*Convert.toDouble(rightoper.toString());
        }
        if(opr.equals("/"))
        {
        result=Convert.toDouble(leftoper.toString())/Convert.toDouble(rightoper.toString());
        }
       
        leftoper=new StringBuffer(Convert.toString(result));
        operand.setText(leftoper.toString());
        rightoper=new StringBuffer("");
       }
    }
      

  3.   

    到 www.drcaffeine.com 下载 javabook 包