JAVA作业 简单的计算器

public class Main_Class {
	public static void main(String args[]) {
        ComputerFrame frame=new ComputerFrame("MyComputer");
        frame.setBounds(100,100,700,800);
	}
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComputerFrame extends JFrame {
	JMenuBar menubar = new JMenuBar();
	JMenu menu_help = new JMenu("帮助");
	JTextField display = new JTextField(20);
	JLabel m_status = new JLabel();
	JPanel console = new JPanel(new BorderLayout());
	JPanel north = new JPanel();
	JPanel west = new JPanel(new GridLayout(5, 1));
	JPanel center = new JPanel(new GridLayout(4, 3));
	JPanel east = new JPanel(new GridLayout(4, 2));
	private double result = 0;
	private char op = '\0';
	boolean append = false;
	String memory = null;

	ComputerFrame(String title) {
		init();
		this.setTitle(title);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	void init() {
		menubar.add(menu_help);
		this.setJMenuBar(menubar);
		display.setText("0");
		display.setFont(new Font("宋体", Font.BOLD, 32));
		display.setHorizontalAlignment(JTextField.RIGHT);
		this.add(display, BorderLayout.NORTH);
		this.add(console);
		init_north();
		init_west();
		init_center();
		init_east();
		this.add(console);
	}

	void init_north() {
		JButton Backspace = new JButton("Backspace");
		Backspace.setFont(new Font("宋体", Font.BOLD, 22));
		JButton CE = new JButton("CE");
		CE.setFont(new Font("宋体", Font.BOLD, 22));
		JButton C = new JButton("C");
		C.setFont(new Font("宋体", Font.BOLD, 22));
		Backspace.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				text = text.substring(0, text.length() - 1);
				display.setText(text);
			}
		});
		CE.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				display.setText("0");
				append=false;
			}
		});
		C.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				display.setText("0");
				result = 0;
				op = '\0';
				append=false;
			}
		});
		north.add(Backspace);
		north.add(CE);
		north.add(C);
		console.add(north, BorderLayout.NORTH);
	}

	void init_west() {
		JButton MC = new JButton("MC");
		MC.setFont(new Font("宋体", Font.BOLD, 22));
		JButton MR = new JButton("MR");
		MR.setFont(new Font("宋体", Font.BOLD, 22));
		JButton MS = new JButton("MS");
		MS.setFont(new Font("宋体", Font.BOLD, 22));
		JButton MPlus = new JButton("M+");
		MPlus.setFont(new Font("宋体", Font.BOLD, 22));
		MC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				memory = null;
				m_status.setText(null);
			}
		});
		MR.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (memory != null) {
					display.setText(memory);
					append=false;
				}
			}
		});
		MS.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!display.getText().equals("0")) {
					memory = display.getText();
				}
				m_status.setText("M");
			}
		});
		MPlus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				double n = Double.valueOf(display.getText());
				double m = Double.valueOf(memory);
				memory = Double.toString(m + n);
				m_status.setText("M");
				append=false;
			}
		});
		m_status.setFont(new Font("宋体", Font.BOLD, 40));
		west.add(m_status);
		west.add(MC);
		west.add(MR);
		west.add(MS);
		west.add(MPlus);
		console.add(west, BorderLayout.WEST);
	}

	void init_center() {
		JButton number[] = new JButton[12];
		for (int i = 0; i < 12; ++i) {
			number[i] = new JButton();
			number[i].setFont(new Font("宋体", Font.BOLD, 40));
		}
		for (int i = 2; i >= -1; --i)
			for (int j = 1; j <= 3; ++j) {
				int num;
				if (i == -1) {// 用于增加按钮"0"
					num = 0;
					j = 3;
				} else
					num = i * 3 + j;
				number[num].setText(Integer.toString(num));
				number[num].addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						String text;
						if (append == true)
							text = display.getText();
						else
							text = "";
						text += e.getActionCommand();
						display.setText(text);
						append = true;
					}
				});
				center.add(number[num]);
			}
		number[10].setText("+/-");
		number[10].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				if (text.charAt(0) == '-')
					text = "+".concat(text.substring(1, text.length()));
				else if (text.charAt(0) == '+')
					text = "-".concat(text.substring(1, text.length()));
				else
					text = "-".concat(text);
				display.setText(text);
			}
		});
		number[11].setText(".");
		number[11].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				text += ".";
				System.out.println(text);
				display.setText(text);
			}
		});
		center.add(number[10]);
		center.add(number[11]);
		console.add(center, BorderLayout.CENTER);
	}

	void init_east() {
		JButton operator[] = new JButton[8];
		operator[0] = new JButton("/");
		operator[0].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				double cur_num = Double.valueOf(text);
				if (op != '\0') {
					result = operate(result, cur_num, op);
					display.setText(Double.toString(result));
				} else
					result = cur_num;
				op = '/';
				append=false;
			}
		});
		operator[1] = new JButton("sqrt");
		operator[1].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				double cur_num = Double.valueOf(text);
				cur_num = Math.sqrt(cur_num);
				display.setText(Double.toString(cur_num));
				append=false;
			}
		});
		operator[2] = new JButton("*");
		operator[2].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				double cur_num = Double.valueOf(text);
				if (op != '\0') {
					result = operate(result, cur_num, op);
					display.setText(Double.toString(result));
				} else
					result = cur_num;
				op = '*';
				append=false;
			}
		});
		operator[3] = new JButton("%");
		operator[3].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				double cur_num = Double.valueOf(text);
				cur_num /= 100;
				display.setText(Double.toString(cur_num));
				append=false;
			}
		});
		operator[4] = new JButton("-");
		operator[4].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				double cur_num = Double.valueOf(text);
				if (op != '\0') {
					result = operate(result, cur_num, op);
					display.setText(Double.toString(result));
				} else
					result = cur_num;
				op = '-';
				append=false;
			}
		});
		operator[5] = new JButton("1/x");
		operator[5].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				double cur_num = Double.valueOf(text);
				if (cur_num == 0)
					display.setText("WRONG!");
				else {
					cur_num = operate(1, cur_num, '/');
					display.setText(Double.toString(cur_num));
				}
				append=false;
			}
		});
		operator[6] = new JButton("+");
		operator[6].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				double cur_num = Double.valueOf(text);
				if (op != '\0') {
					result = operate(result, cur_num, op);
					display.setText(Double.toString(result));
				} else
					result = cur_num;
				op = '+';
				append=false;
			}
		});
		operator[7] = new JButton("=");
		operator[7].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = display.getText();
				double cur_num = Double.valueOf(text);
				if (op != '\0') {
					result = operate(result, cur_num, op);
					display.setText(Double.toString(result));
				}
				op = '\0';
				append=false;
			}
		});
		for (int i = 0; i < 8; ++i) {
			operator[i].setFont(new Font("宋体", Font.BOLD, 30));
			east.add(operator[i]);
		}
		console.add(east, BorderLayout.EAST);
	}

	double operate(double a, double b, char op) {
		switch (op) {
		case '+':
			return a + b;
		case '-':
			return a - b;
		case '*':
			return a * b;
		default:
			return a / b;
		}
	}
}

 个人网站:https://jqh.zone

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值