制作一个简易计算器


使用java的AWT包实现一个简易的计算器。

代码语言:javascript
复制
import java.awt.*;
import java.awt.event.*;

public class Calculator {
private Frame frame;
private Label label;
private Panel panel;
private Button button0,button1,button2,button3,button4,button5,button6,button7,button8,
button9,buttonequ,buttondiv,buttondot,buttonmul,buttonsub,buttonadd;
private ButtonHandler listener;

public Calculator() {
    frame = new Frame("My Calculator");
    label = new Label("0", Label.RIGHT);
    panel = new Panel(new GridLayout(4, 4));
    button0 = new Button("0");
    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");
    buttonequ = new Button("=");
    buttondiv = new Button("/");
    buttondot = new Button(".");
    buttonmul = new Button("*");
    buttonsub = new Button("-");
    buttonadd = new Button("+");
    panel.add(button7);
    panel.add(button8);
    panel.add(button9);
    panel.add(buttonadd);
    panel.add(button4);
    panel.add(button5);
    panel.add(button6);
    panel.add(buttonsub);
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(buttonmul);
    panel.add(button0);
    panel.add(buttondot);
    panel.add(buttonequ);
    panel.add(buttondiv);
    frame.add(label, BorderLayout.NORTH);
    frame.add(panel, BorderLayout.CENTER);
    frame.pack();
    frame.setSize(300,300);
    frame.setVisible(true);

    listener = new ButtonHandler();

    //对按钮添加监听
    button0.addActionListener(listener);
    button1.addActionListener(listener);
    button2.addActionListener(listener);
    button3.addActionListener(listener);
    button4.addActionListener(listener);
    button5.addActionListener(listener);
    button6.addActionListener(listener);
    button7.addActionListener(listener);
    button8.addActionListener(listener);
    button9.addActionListener(listener);
    buttonadd.addActionListener(listener);
    buttonsub.addActionListener(listener);
    buttonmul.addActionListener(listener);
    buttondiv.addActionListener(listener);
    buttondot.addActionListener(listener);
    buttonequ.addActionListener(listener);

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
}

class ButtonHandler implements ActionListener {
    int flag = 0; //存储运算符
    boolean equHappened = true;
    double arg1 = 0, arg2 = 0, result = 0;
    //@Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == buttonequ) {
            equHappened = true;
            if(flag==0) {
                result = arg1 + arg2;
            }
            else if(flag==1) {
                result = arg1 - arg2;
            }
            else if(flag==2) {
                result = arg1 * arg2;
            }
            else {
                result = arg1 / arg2;
            }
            label.setText(result+"");
        }
        else {
            if(equHappened == true) {
                label.setText("");
                equHappened = false;
            }

            if(e.getSource() == buttondot) {
                label.setText("0.");
            }
            else if(e.getSource() == button0) {
                label.setText(label.getText()+"0");
                arg2 = Double.parseDouble(label.getText());
            }
            else if(e.getSource() == button1) {
                label.setText(label.getText()+"1");
                arg2 = Double.parseDouble(label.getText());
            }
            else if(e.getSource() == button2) {
                label.setText(label.getText()+"2");
                arg2 = Double.parseDouble(label.getText());
            }
            else if(e.getSource() == button3) {
                label.setText(label.getText()+"3");
                arg2 = Double.parseDouble(label.getText());
            }
            else if(e.getSource() == button4) {
                label.setText(label.getText()+"4");
                arg2 = Double.parseDouble(label.getText());
            }
            else if(e.getSource() == button5) {
                label.setText(label.getText()+"5");
                arg2 = Double.parseDouble(label.getText());
            }
            else if(e.getSource() == button6) {
                label.setText(label.getText()+"6");
                arg2 = Double.parseDouble(label.getText());
            }
            else if(e.getSource() == button7) {
                label.setText(label.getText()+"7");
                arg2 = Double.parseDouble(label.getText());
            }
            else if(e.getSource() == button8) {
                label.setText(label.getText()+"8");
                arg2 = Double.parseDouble(label.getText());
            }
            else if(e.getSource() == button9) {
                label.setText(label.getText()+"9");
                arg2 = Double.parseDouble(label.getText());
            }
            else if(e.getSource() == buttonadd) {
                label.setText("");
                arg1 = arg2;
                flag = 0;
            }
            else if(e.getSource() == buttonsub) {
                label.setText("");
                arg1 = arg2;
                flag = 1;
            }
            else if(e.getSource() == buttonmul) {
                label.setText("");
                arg1 = arg2;
                flag = 2;
            }
            else if(e.getSource() == buttondiv) {
                label.setText("");
                arg1 = arg2;
                flag = 3;
            }
        }
    }
}

public static void main(String[] args) {
    Calculator cal = new Calculator();
}

}

并不难,只用到了AWT,一个小时做完,感兴趣的可以用swing改写一下。