Java小程序-计算器

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
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.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import sun.applet.Main;

public class Calculator extends JFrame {

	private JPanel panelMain;
	private JPanel panelNum;
	private JPanel panelOperation;
	private JPanel panelResult;

	private JTextField resultField;
	private JButton btnC;
	private JButton btnBack;
	private double result;
	private String lastCommand;
	private boolean start;
	private JMenuBar menuBar;

	public Calculator(String title) {
		super(title);
		init();
	}

	/*
	 * 初始化
	 */
	public void init() {

		result = 0.0;
		lastCommand = "=";
		start = true;

		panelMain = new JPanel(new BorderLayout());
		panelNum = new JPanel(new GridLayout(4, 4, 5, 5));
		panelOperation = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		panelResult = new JPanel(new BorderLayout(5, 5));

		ActionListener insert = new InsertAction();
		ActionListener command = new CommandAction();

		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);

		// 菜单
		menuBar = new JMenuBar();
		String[] strings1 = { "clear", "back", "exit" };
		addMenuToBar("Edit", strings1, new MenuAction());
		String[] strings2 = { "about", "help" };
		addMenuToBar("View", strings2, new MenuAction());

		resultField = new JTextField("0.");
		// 设置文本对齐格式
		resultField.setHorizontalAlignment(JTextField.RIGHT);
		resultField.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		resultField.setEditable(false);

		btnC = new JButton("Clear");
		// 添加监听器,清空所有计算结果
		btnC.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ClearAct();
			}
		});

		btnBack = new JButton("Back");
		// 添加监听器,退格功能
		btnBack.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				BackAct();
			}
		});

		/* panelMain */
		panelOperation.add(btnC);
		panelOperation.add(btnBack);
		panelMain.add(panelNum);
		panelMain.add(panelOperation, BorderLayout.NORTH);

		/* this */
		panelResult.add(resultField, BorderLayout.SOUTH);
		panelResult.add(menuBar);

		this.add(panelMain);
		this.add(panelResult, BorderLayout.NORTH);

		/* 基本属性设置 */
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// this.setLocationRelativeTo(null);

		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int width = screenSize.width;
		int height = screenSize.height;
		this.setLocation(width * 2 / 5, height * 2 / 5);
		this.setVisible(true);
		this.pack();
	}

	/* 点击数字Button时 */
	private class InsertAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			String input = e.getActionCommand();
			if (start) {
				resultField.setText("");
				start = false;
			}
			resultField.setText(resultField.getText() + input);
		}
	}

	/* 点击加减乘除 等Button时 */
	private class CommandAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();
			if (start) {
				/* 处理负数的情况 */
				if (command.equals("-")) {
					resultField.setText(command);
					start = false;
				}
				/* 否则-为减操作 */
				else
					lastCommand = command;
			} else {
				calculate(Double.parseDouble(resultField.getText()));
				lastCommand = command;
				start = true;
			}
		}
	}

	/* 点击菜单时 */
	private class MenuAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();
			if ("clear".equals(command)) {
				ClearAct();
			} else if ("back".equals(command)) {
				BackAct();
			} else if ("exit".equals(command)) {
				System.exit(0);
			} else if ("about".equals(command)) {
				JOptionPane.showMessageDialog(Calculator.this,
						"this calculator is designed by zkj", "Message",
						JOptionPane.INFORMATION_MESSAGE);
			} else if ("help".equals(command)) {
				JOptionPane.showConfirmDialog(Calculator.this,
						"点击数字和计算符,back为退一格,clear为清空");
			}
		}
	}

	/* back所做的操作 */
	public void BackAct() {
		if (resultField.getText().length() > 2) {
			String s = resultField.getText().substring(0,
					resultField.getText().length() - 1);
			resultField.setText(s);
		} else {
			resultField.setText("0.");
		}
	}

	/* clear所做的操作 */
	public void ClearAct() {
		result = 0.0;
		lastCommand = "=";
		start = true;
		resultField.setText("0.");
	}

	/* 计算过程 */
	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;
		}
		resultField.setText("" + result);
	}

	// 添加按钮并注册事件
	public void addButton(String string, ActionListener listener) {
		JButton button = new JButton(string);
		button.addActionListener(listener);
		panelNum.add(button);
	}

	// 添加按钮并注册事件
	public void addMenuToBar(String menu, String[] items,
			ActionListener listener) {
		JMenu jmenu = new JMenu(menu);
		JMenuItem[] menuItems = new JMenuItem[items.length];
		for (int i = 0; i < menuItems.length; i++) {
			menuItems[i] = new JMenuItem(items[i]);
			// 注册事件
			menuItems[i].addActionListener(listener);
			jmenu.add(menuItems[i]);
		}
		menuBar.add(jmenu);
	}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值