⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator.java

📁 java编写的简单计算器代码
💻 JAVA
字号:
package calculator;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
 * this program shows a simple Calculator
 * it employs StringTokenizer to distinguish the type of the calculation
 * @author Nature
 *
 */
public class Calculator {

	public static void main(String[] args) {
		JFrame frame = new CalculatorFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}

class CalculatorFrame extends JFrame {
	public CalculatorFrame() {
		super("calculator");
		Container contentPane = getContentPane();
		CalculatorPanel panel = new CalculatorPanel();
		contentPane.add(panel);
		pack();
	}
}

class CalculatorPanel extends JPanel {

	private StringTokenizer st=null;

	private JLabel display=null;

	private JPanel panel=null;

	private static  double result=0;

	private StringBuffer sb =new StringBuffer();

	private String command=null;
	
	public CalculatorPanel() {
		setLayout(new BorderLayout());

		// add the display(display what is press and the result)
		display = new JLabel("0");
		add(display, BorderLayout.NORTH);

		// add the button in the 4*4 GridLayout
		panel = new JPanel();
		panel.setLayout(new GridLayout(4, 4));

		InsertAction listener = new InsertAction();

		addButton("7", listener);
		addButton("8", listener);
		addButton("9", listener);
		addButton("/", listener);

		addButton("4", listener);
		addButton("5", listener);
		addButton("6", listener);
		addButton("*", listener);

		addButton("1", listener);
		addButton("2", listener);
		addButton("3", listener);
		addButton("-", listener);

		addButton("0", listener);
		addButton(".", listener);
		addButton("=", listener);
		addButton("+", listener);

		add(panel, BorderLayout.CENTER);
	}

	/**
	 * add a button to panel
	 * @param label the name of the button
	 * @param listener the actionListener the button implements
	 */
	private void addButton(String label, InsertAction listener) {
		JButton button = new JButton(label);
		button.addActionListener(listener);
		panel.add(button);
	}
/**
 * the inner InsertAction class 
 * @author nature
 *
 */
	private class InsertAction implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			command = e.getActionCommand();
			if(!command.equals("=")){
			sb.append(command);
			display.setText(sb.toString());
			}else {
				calculate();
				display.setText("" + result);
				sb = new StringBuffer();
			}
		}

	}
/**
 * the calculate process
 *
 */
	public void calculate() {

		st = new StringTokenizer(sb.toString(),"+");		
		if (st.countTokens() == 2) {			//if there is a token "+" in the String
			double a = Double.parseDouble((String) st.nextToken());
			double b = Double.parseDouble((String) st.nextToken());
			result = a + b;
		} else {			                    //else
			st = new StringTokenizer(sb.toString(),"-");
			if (st.countTokens() == 2) {
				double a = Double.parseDouble((String) st.nextToken());
				double b = Double.parseDouble((String) st.nextToken());
				result = a - b;
			} else {
				st = new StringTokenizer(sb.toString(),"*");
				if (st.countTokens() == 2) {
					double a = Double.parseDouble((String) st.nextToken());
					double b = Double.parseDouble((String) st.nextToken());
					result = a * b;
				} else {
					st = new StringTokenizer(sb.toString(),"/");
					if (st.countTokens() == 2) {
						double a = Double
								.parseDouble((String) st.nextToken());
						double b = Double
								.parseDouble((String) st.nextToken());
						result = a / b;
					}
				}
			}
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -