package com.lu;

import java.awt.Dimension;
import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Window extends JFrame implements ActionListener {
	JTextField displayField;
	StringBuilder builder = new StringBuilder();
	String left;
	String right;
	String op;

	public Window() {
		super();
		this.setupUI();
	}

	private void genLayout(JPanel parent, String... menu) {
		for (String m : menu) {
			JButton temp = new JButton(m);
			temp.addActionListener(this::actionPerformed);
			parent.add(temp);
		}
	}

	private void setupUI() {
		Box vBox = Box.createVerticalBox();
		displayField = new JTextField();
		vBox.add(displayField);
		GridLayout grid = new GridLayout(5, 4);
		JPanel frame = new JPanel();
		frame.setVisible(true);
		frame.setLayout(grid);
		this.setPreferredSize(new Dimension(300, 100));
		// 控制菜单
		this.genLayout(frame, "AC", "BACK", "%", "/", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "",
				"0", ".", "=");
		this.setBounds(300, 300, 300, 240);
		vBox.add(frame);
		this.add(vBox);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String cmd = e.getActionCommand();
		// 清空
		if(cmd.equals("AC")){
			builder =new StringBuilder();
			left ="";
			right ="";
			op ="";
			this.displayField.setText("");
			return;
		}

		if(cmd.equals("BACK")){
			String buffer = this.builder.toString();
			if(buffer.length()-1<0){
				return;
			}
			buffer = buffer.subSequence(0, buffer.length()-1).toString();
			System.out.println(buffer);
			builder =new StringBuilder();
			builder.append(buffer);
			this.displayField.setText(builder.toString());
			return;
		}
		if (cmd.equals("+") || cmd.equals("-") || cmd.equals("*") || cmd.equals("/")||cmd.equals("%")) {
			left = builder.toString();
			op = cmd;
			// 扔掉之前的
			builder = new StringBuilder();
		} else if (cmd.equals("=")) {
			right = builder.toString().replace("=", "");
			this.getResult();
		} else {
			builder.append(cmd);
			this.displayField.setText(builder.toString());
		}

	}

	private void getResult() {
		// TODO Auto-generated method stub
		Double left = Double.parseDouble(this.left);
		Double right = Double.parseDouble(this.right);
		System.out.println(left);
		System.out.println(right);
		switch (op) {
		case "+": {
			this.displayField.setText(String.valueOf(left + right));
			break;
		}
		case "-": {
			this.displayField.setText(String.valueOf(left - right));
			break;
		}
		case "*": {
			this.displayField.setText(String.valueOf(left * right));
			break;
		}
		case "/": {
			this.displayField.setText(String.valueOf(left / right));
			break;
		}
		case "%": {
			this.displayField.setText(String.valueOf(left % right));
			break;
		}
		}
	}
}

上一篇 下一篇