java实现简易计算器

一个简易计算器的java实现,采用的只是最基础的jbutton组件和相对应得监听事件以及BorderLayout和GridLayout两种布局方式。

package com.phoenix;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class CaculatorPanel extends JPanel{
	private JButton display;
	private JPanel panel;
	private double result;
	private String lastCommand;
	private boolean start;
	
	public CaculatorPanel() {
		setLayout(new BorderLayout());
		result = 0;
		lastCommand = "=";
		start = true;
		// add the display
		display = new JButton("0");
		display.setEnabled(false);
		add(display, BorderLayout.NORTH);
		
		ActionListener insert = new InsertAction();
		ActionListener command = new CommandAction();
		
		// add the buttons in a 4 * 4 grid
		panel = new JPanel();
		panel.setLayout(new GridLayout(4, 4));
		
		addButtons("7", insert);
		addButtons("8", insert);
		addButtons("9", insert);
		addButtons("/", command);
		
		addButtons("4", insert);
		addButtons("5", insert);
		addButtons("6", insert);
		addButtons("*", command);
		
		addButtons("1", insert);
		addButtons("2", insert);
		addButtons("3", insert);
		addButtons("-", command);
		
		addButtons("0", insert);
		addButtons(".", insert);
		addButtons("=", command);
		addButtons("+", command);
		
		add(panel, BorderLayout.CENTER);
		
	}
	
	private void addButtons(String lable, ActionListener listener) {
		JButton button = new JButton(lable);
		button.addActionListener(listener);
		panel.add(button);
	}
	
	private class InsertAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String input = e.getActionCommand();
			if (start) {
				display.setText("");
				start = false;
			}
			display.setText(display.getText() + input);
		}	
	}
	
	
	private class CommandAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();
			
			if (start) {
				if (command.equals("-")) {
					display.setText(command);
					start = false;
				}else {
					lastCommand = command;
				}
			}else {
				caculate(Double.parseDouble(display.getText()));
				lastCommand = command;
				start = true;
			}
		}
	}
	
	public void caculate(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;
		}
		display.setText("" + result);
	}
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值