首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Java AWT 簡易計算機程式

     編輯:Java 程式設計入門

使用 AWT 元件 (Frame, Button, TextField, Panel) 實作基本四則運算與開平方功能。透過 ActionListener 處理數字鍵、運算鍵、等號與清除鍵事件,支援連續運算 (如 1+2+3=)。

import java.awt.*;
import java.awt.event.*;

public class Calculator extends Frame implements ActionListener {
    TextField display = new TextField("", 15);
    Button[] numBtn = new Button[10];
    Button btnAdd, btnSub, btnMul, btnDiv, btnDot, btnEq, btnSqrt, btnClear;
    String s1 = "", s2 = "", op = null;
    boolean first = true;
    int opCount = 0;
    double result = 0;

    public Calculator() {
        super("Calculator");
        display.setEditable(false);
        Panel p1 = new Panel(new GridLayout(4,4,5,5));
        Panel p2 = new Panel(new FlowLayout());
        for (int i = 0; i < 10; i++) {
            numBtn[i] = new Button(String.valueOf(i));
            numBtn[i].setActionCommand("num");
            numBtn[i].addActionListener(this);
            p1.add(numBtn[i]);
        }
        btnDot = new Button("."); btnDot.setActionCommand("num"); btnDot.addActionListener(this); p1.add(btnDot);
        btnAdd = new Button("+"); btnAdd.setActionCommand("op"); btnAdd.addActionListener(this); p1.add(btnAdd);
        btnSub = new Button("-"); btnSub.setActionCommand("op"); btnSub.addActionListener(this); p1.add(btnSub);
        btnMul = new Button("*"); btnMul.setActionCommand("op"); btnMul.addActionListener(this); p1.add(btnMul);
        btnDiv = new Button("/"); btnDiv.setActionCommand("op"); btnDiv.addActionListener(this); p1.add(btnDiv);
        btnEq = new Button("="); btnEq.addActionListener(this); p1.add(btnEq);
        btnSqrt = new Button("√"); btnSqrt.addActionListener(this); p2.add(btnSqrt);
        btnClear = new Button("C"); btnClear.addActionListener(this); p2.add(btnClear);

        add(display, BorderLayout.NORTH);
        add(p1, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
        setSize(250, 250); setLocation(400,200); setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) { System.exit(0); }
        });
    }

    public void actionPerformed(ActionEvent e) {
        Button src = (Button)e.getSource();
        String cmd = e.getActionCommand();
        if ("num".equals(cmd)) {
            if (first) { s1 += src.getLabel(); display.setText(s1); }
            else { s2 += src.getLabel(); display.setText(s2); }
        } else if ("op".equals(cmd)) {
            if (s1.isEmpty()) { opCount=0; first=true; }
            else {
                if (++opCount > 1) calculate();
                op = src.getLabel(); first = false;
            }
        } else if (src == btnSqrt) {
            if (s1.isEmpty()) { opCount=0; first=true; }
            else {
                if (++opCount > 1) calculate();
                double v = Math.sqrt(Double.parseDouble(s1));
                s1 = String.valueOf(v); display.setText(s1); first = false;
            }
        } else if (src == btnClear) {
            s1 = s2 = ""; op = null; opCount = 0; first = true; display.setText("");
        } else if (src == btnEq) {
            if (s1.isEmpty() || s2.isEmpty()) { opCount=0; first=true; }
            else { calculate(); opCount = 0; }
        }
    }

    void calculate() {
        double n1 = Double.parseDouble(s1), n2 = Double.parseDouble(s2);
        switch (op) {
            case "+": result = n1 + n2; break;
            case "-": result = n1 - n2; break;
            case "*": result = n1 * n2; break;
            case "/": result = (n2 == 0) ? Double.NaN : n1 / n2; break;
        }
        display.setText(Double.isNaN(result) ? "Error" : String.valueOf(result));
        s1 = display.getText(); s2 = "";
    }

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

改進方向:

  • 改用 Swing (JFrame, JButton) 獲得更好外觀與跨平台一致性。
  • 使用 ScriptEngine (JavaScript 引擎) 直接評估運算式字串,簡化邏輯。
  • 加入鍵盤事件支援 (數字鍵、Enter、Esc)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved