简易计算器java版

按照java核心技术卷一上敲的,做了一点改动
import java.awt.EventQueue;

import javax.swing.*;

import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.border.StandardBorderPainter;
import org.jvnet.substance.button.ClassicButtonShaper;
import org.jvnet.substance.skin.MagmaSkin;
import org.jvnet.substance.watermark.SubstanceBubblesWatermark;

public class CaclulatorFrame { 

	public static void main(String[] args) { 
		// TODO Auto-generated method stub
		EventQueue.invokeLater(new Runnable() { 
			public void run() { 
		        /**这一段调用了substance里的外观,开源的东西感觉还不错,可以试一下
                          try {   
					UIManager.setLookAndFeel(new SubstanceLookAndFeel());
					JFrame.setDefaultLookAndFeelDecorated(true);
					SubstanceLookAndFeel.setSkin(new MagmaSkin());
					SubstanceLookAndFeel.setCurrentWatermark (new SubstanceBubblesWatermark());
					SubstanceLookAndFeel.setCurrentBorderPainter(new StandardBorderPainter()); 
				} catch (UnsupportedLookAndFeelException e) { 
					// TODO Auto-generated catch block 
					e.printStackTrace();
				} 
                        **/
				JFrame frame = new JFrame();
				CalculatorPanel panel = new CalculatorPanel();
				
				frame.setSize(300, 200);
				frame.setTitle("Calculator"); 
				frame.setLocationRelativeTo(null); 
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
				frame.add(panel); 
				
				frame.setVisible(true);
			} 
		});
	}
}


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

import javax.swing.*;

/**
 * @version 2015-12-8
 * @author tbcnscc3c2
 * 
 */
public class CalculatorPanel extends JPanel {

	private JButton display;
	private JPanel panel;
	private double result;
	private String lastCommand;
	private boolean start, flag;

	public CalculatorPanel() {
		setLayout(new BorderLayout());
		result = 0;
		lastCommand = "=";
		start = true;
		flag = true;

		// add the display

		display = new JButton("0");
		display.setEnabled(false);
		add(display, BorderLayout.NORTH);
               
                //监听器
		ActionListener insert = new InsertAction();
		ActionListener command = new CommandAction();
		ActionListener insertPoint = new InsertPointAction();

		//把按钮按GridLayout进行4 x 4的网格布局
		panel = new JPanel();
		panel.setLayout(new GridLayout(4, 4));

		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(".", insertPoint);//原文可以无限输入小数点的,这里做了改动防止无限输入小数点
		addButton("=", command);
		addButton("+", command);

		add(panel, BorderLayout.CENTER);
	}

	/**
	 * Adds a button to the center panel.
	 * 
	 * @param label
	 *            the button label
	 * @param listener
	 *            the button listener
	 */
	private void addButton(String label, ActionListener listener) {

		JButton button = new JButton(label);
		button.addActionListener(listener);
		panel.add(button);
	}

	/**
	 * This action inserts the button action string to the end of the display
	 * text.
	 */
	private class InsertAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String input = event.getActionCommand();
			if (start) {
				display.setText("");
				start = false;
			}
			display.setText(display.getText() + input);
		}
	}
	
	/**
	 * Core Java源代码可以无限输入小数点bug,本方法解决此bug
	 */
	private class InsertPointAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String input = event.getActionCommand();
			if (start) {
				start = false;
			}
			if(display.getText().indexOf(".")==-1)
				display.setText(display.getText() + input);
		}
	}

	/**
	 * This action executes the command that the button action string denotes.
	 */
	private class CommandAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String command = event.getActionCommand();

			if (start) {
				if (command.equals("-")) {
					display.setText(command);
					start = false;
				} else
					lastCommand = command;
			} else {
				calculate(Double.parseDouble(display.getText()));
				lastCommand = command;
				start = true;
			}
		}
	}

	/**
	 * Carries out the pending calculation.
	 * 
	 * @param x
	 *            the value to be accumulated with the prior result.
	 */
	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;
		display.setText("" + result);
	}
}


本来的样纸:


用的substance里的外观:


酒红色的、不喜轻喷



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值