java实现计算器(最低版1.0,只能实现从左到右依次计算)——初学者入门

软件:eclipse
才开始用java设计东西,我觉得这个程序适用于初步想设计计算器的同学用,虽然很简单,但我会慢慢更新自己所写计算器,实现更强的逻辑。

例如:61-7*4=216(从左至右的简单逻辑,还不完善)

代码:(有详细注释)

//大佬不适合看!
package layout;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
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;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class CalculatorFrame extends JFrame {
	// 属性
	JTextField Text = null;
	boolean beginNum = false;
	String num1 = null;
	String num2 = null;
	String operator = null;

	// 方法
	public CalculatorFrame() {
		setTitle("Borderlayout Demo");
		setSize(250, 300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		// 获取主内容面板
		Container contentPane = this.getContentPane();
		// 设置布局方式:边框型布局
		contentPane.setLayout(new BorderLayout());

		// 单独处理NORTH布局
		// 获得中间容器面板并放在主面板的NORTH
		JPanel pnlNorth = new JPanel();
		// 边框型布局
		pnlNorth.setLayout(new BorderLayout());
		// 放置
		contentPane.add(BorderLayout.NORTH, pnlNorth);
		// 文本框
		Text = new JTextField();
		// 按钮(清零)
		JButton btnOK = new JButton("C");

		// 放置
		pnlNorth.add(BorderLayout.CENTER, Text);
		pnlNorth.add(BorderLayout.EAST, btnOK);

		// 单独处理CENTER布局
		// //获得中间容器面板并放在主面板的CENTER
		String[] s = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3","*", "0", "." ,"/", "="  };
		JPanel pnlNorthTow = new JPanel();
		// 放置
		contentPane.add(BorderLayout.CENTER, pnlNorthTow);

		// 网状布局
		pnlNorthTow.setLayout(new GridLayout(0, 4, 3, 3));
		// 内部类
		ActionListener listener = new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				String cmd = event.getActionCommand();
				// JOptionPane.showMessageDialog(null, cmd);
				/*if (cmd.equals("0") || cmd.equals("1") || cmd.equals("2") || cmd.equals("3") || cmd.equals("4")
						|| cmd.equals("5") || cmd.equals("6") || cmd.equals("7") || cmd.equals("8")
						|| cmd.equals("9")) {*/
				if("0123456789".contains(cmd)){
					// 输入1时(61显示)
					if (beginNum) {
						Text.setText(Text.getText() + cmd);
					}
					// 输入6时
					else {
						Text.setText(cmd);
						beginNum = true;
					}
				}
				
				// 输入“—”时
				//if (cmd.equals("+") || cmd.equals("-") || cmd.equals("*") || cmd.equals("/")) {
				if("+-*/".contains(cmd)){
					beginNum = false;
					// 当operator为空的时候,与equals的区别
					if (operator == null) {
						// 文本框内容赋给num1
						num1 = Text.getText();
						// 减号赋给operator
						operator = cmd;
						// 清空文本框
						Text.setText(null);
					}
					// operator不为空时(输入*的时候)
					else {
						// 把7赋给num2
						num2 = Text.getText();
						// 计算num1值并放在文本框里面
						if(operator.equals("-"))
							num1 = String.valueOf(Integer.parseInt(num1) - Integer.parseInt(num2));
						if(operator.equals("+"))
							num1 = String.valueOf(Integer.parseInt(num1) + Integer.parseInt(num2));
						if(operator.equals("*"))
							num1 = String.valueOf(Integer.parseInt(num1) * Integer.parseInt(num2));
						if(operator.equals("/"))
							num1 = String.valueOf(((double)Integer.parseInt(num1) / Integer.parseInt(num2)));
						Text.setText(num1);
						// 把*赋给operator
						operator = cmd;
					}
				}
				//当输等号时
					if ("=".equals(cmd)) {
						// 结束置为false
						beginNum = false;
						// 将4赋给num2
						num2 = Text.getText();
						if(operator.equals("-"))
							num1 = String.valueOf(Integer.parseInt(num1) - Integer.parseInt(num2));
						if(operator.equals("+"))
							num1 = String.valueOf(Integer.parseInt(num1) + Integer.parseInt(num2));
						if(operator.equals("*"))
							num1 = String.valueOf(Integer.parseInt(num1) * Integer.parseInt(num2));
						if(operator.equals("/"))
							num1 = String.valueOf(((double)Integer.parseInt(num1) / Integer.parseInt(num2)));
						Text.setText(String.valueOf(num1));
						num1 = null;
						//num2=null;
						operator = null;
					}
					if("C".equals(cmd)){
						Text.setText(null);
					}
			}		
		};
		for (int i = 0; i < 16; i++) {
			JButton btn = new JButton(s[i]);
			pnlNorthTow.add(btn); // 注意各个面板的add
			// 数字符号等输入放置
			btn.addActionListener(listener);
		}
		// 按钮输入放置
		btnOK.addActionListener(listener);
	}

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

}


你可能感兴趣的:(实践)