java计算器(带图形界面)

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

public class JCalculator extends JFrame implements ActionListener {
    private final String[] str = { 
		"7", "8", "9", "/", 
		"4", "5", "6", "*",
		"1", "2", "3", "-", 
		".", "0", "=", "+"};
                                 
    JButton[] buttons = new JButton[str.length];  //buttons for numbers
    JButton reset = new JButton("CE");         //button for reset
    JTextField display = new JTextField("0");  //textfied for result

    public JCalculator() {
        super("Calculator");
		Font font = new Font("Consolas", Font.BOLD, 18);

		// add components
		JPanel pnlHead = new JPanel(new BorderLayout());
        pnlHead.add( display, BorderLayout.CENTER);
        pnlHead.add( reset, BorderLayout.EAST);
		display.setFont(font);
		reset.setFont(font);

        JPanel pnlBody = new JPanel(new GridLayout(4, 4));
        for (int i = 0; i < str.length; i++) {
            buttons[i] = new JButton(str[i]);
			buttons[i].setFont(font);
            pnlBody.add(buttons[i]);
        }

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(BorderLayout.NORTH, pnlHead);
        getContentPane().add(BorderLayout.CENTER, pnlBody);

        // register listeners
        for (int i = 0; i < str.length; i++)
            buttons[i].addActionListener(this);
        reset.addActionListener(this);
        display.addActionListener(this);

		// set frame properties
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(300, 280);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        String cmd = e.getActionCommand();
        if (source == reset)
            handleReset();
        else if ("0123456789.".indexOf(cmd) >= 0)
            handleNumber(cmd);
        else
            handleOperator(cmd);
    }

    boolean isFirstDigit = true;
    double number = 0.0;
    String operator = "=";
	
    public void handleNumber(String key) {
        if (isFirstDigit)
            display.setText(key);
        else if (!key.equals("."))
            display.setText(display.getText() + key);
        else if (display.getText().indexOf(".") < 0)
            display.setText(display.getText() + ".");
        isFirstDigit = false;
    }

    public void handleReset() {
        display.setText("0");
        isFirstDigit = true;
        operator = "=";
    }

    public void handleOperator(String cmd) {
		double dDisplay = Double.valueOf(display.getText());
		switch(operator){
			case "+": number += dDisplay; break;
			case "-": number -= dDisplay; break;
			case "*": number *= dDisplay; break;
			case "/": number /= dDisplay; break;
			case "=": number = dDisplay; break;
		}
		display.setText(String.valueOf(number));
        operator = cmd;
        isFirstDigit = true;
    }

	public static void main(String[] args) {
		SwingUtilities.invokeLater(()->{
			new JCalculator();
		});
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值