package com.summit.view;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import javax.swing.text.NumberFormatter;
import com.summit.commons.CalcutorUtil;
import com.summit.commons.StringUtil;
public class RefactorCalculatorFrame extends JFrame {
private JButton[] btnNums = new JButton[10];
private JButton[] btnOpts = new JButton[4];
private JFormattedTextField txtResult = new JFormattedTextField();;
private JButton btnEqual = new JButton("=");
private JButton btnSpace = new JButton("BackSpace");
private JButton btnClear = new JButton("C");
private static final String[] opts = { "+", "-", "*", "/" };
private double result = 0;// 结果
private char currentOpration = '\0';// 当前操作符
private boolean isOperation = false;// 是否点击操作符
private boolean isEqualClick = false;// 判断是否已经点击 '='
private StringBuffer resultBuffer = new StringBuffer();
private boolean equalOperation = false;
public RefactorCalculatorFrame(String title) {
setTitle(title);
initComents();
addListeners();
txtResult.requestFocus();
setResizable(false);
}
private void initComents() {
this.setLayout(new GridLayout(6, 3));
this.setSize(313, 280);
this.setLocationRelativeTo(null);
addButtons();
this.setVisible(true);
}
private void addListeners() {
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
btnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
clearResult();
}
});
btnSpace.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String txt = txtResult.getText();
if (!StringUtil.isEmpty(txt)) {
resultBuffer = new StringBuffer(resultBuffer.substring(0,
resultBuffer.length() - 1));
txtResult.setText(txt.substring(0, txt.length() - 1));
}
}
});
btnEqual.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (isEqualClick) {
if (StringUtil.isEmpty(resultBuffer.toString())) {
return;
}
if (!CalcutorUtil.isCalcute(resultBuffer.toString())) {
resultBuffer = new StringBuffer(resultBuffer.substring(
0, resultBuffer.length() - 1));
}
result = CalcutorUtil.calcResult(resultBuffer.toString());
txtResult.setText(result + "");
isOperation = false;
isEqualClick = false;
equalOperation = true;
}
}
});
txtResult.addKeyListener(new KeyAdapter() {
int keyoptcount = 0;
@Override
public void keyReleased(KeyEvent e) {
char currentKeyChar = e.getKeyChar();
if (currentKeyChar == '=' || e.getKeyCode() == 10) {
if (StringUtil.isEmpty(resultBuffer.toString())) {
return;
}
if (isEqualClick) {
System.out.println(resultBuffer.toString());
if (!CalcutorUtil.isCalcute(resultBuffer.toString())) {
resultBuffer = new StringBuffer(resultBuffer
.substring(0, resultBuffer.length() - 1));
}
result = CalcutorUtil.calcResult(resultBuffer
.toString());
txtResult.setText(result + "");
isOperation = false;
isEqualClick = false;
equalOperation = true;
return;
}
}
isEqualClick = true;
if (e.getKeyCode() == 8) {
if (StringUtil.isEmpty(resultBuffer.toString())) {
return;
}
resultBuffer = new StringBuffer(resultBuffer.substring(0,
resultBuffer.length() - 1));
return;
}
if (isOprationChar(e.getKeyChar())) {
if(StringUtil.isEmpty(resultBuffer.toString())){
return;
}
if (!StringUtil.isEmpty(txtResult.getText())) {
txtResult.setText(txtResult.getText().substring(0,
txtResult.getText().length() - 1));
keyoptcount++;
if (keyoptcount > 1) {
resultBuffer.setCharAt(resultBuffer.length() - 1,' ');
}
resultBuffer.append(e.getKeyChar());
if (CalcutorUtil.isCalcute(resultBuffer.substring(0,
resultBuffer.length() - 1))) {
result = CalcutorUtil.calcResult(resultBuffer
.substring(0, resultBuffer.length() - 1));
txtResult.setText(result + "");
resultBuffer = new StringBuffer(result + ""
+ e.getKeyChar());
isOperation = true;
}
}
} else {
if (isOperation) {
txtResult.setText(e.getKeyChar() + "");
isOperation = false;
}
keyoptcount = 0;
String value = txtResult.getText();
if (!StringUtil.isNumber(value)) {
txtResult.setText("");
}
resultBuffer.append(e.getKeyChar());
}
txtResult.requestFocus();
}
});
}
private boolean isOprationChar(char keychar) {
for (String opt : opts) {
if (opt.equals(keychar + "")) {
return true;
}
}
return false;
}
private void clearResult() {
txtResult.setText("");
result = 0;
isOperation = false;
currentOpration = '\0';
isEqualClick = true;
resultBuffer = new StringBuffer();
}
private void addButtons() {
NumberActionListener numberActionListener = new NumberActionListener();
for (int i = 0; i < btnNums.length; i++) {
btnNums[i] = new JButton();
btnNums[i].setSize(30, 15);
btnNums[i].setText(i + "");
btnNums[i].addActionListener(numberActionListener);
this.add(btnNums[i]);
}
OprationActionListener optActionListener = new OprationActionListener();
for (int i = 0; i < btnOpts.length; i++) {
btnOpts[i] = new JButton();
btnOpts[i].setSize(30, 15);
btnOpts[i].setText(opts[i]);
btnOpts[i].addActionListener(optActionListener);
this.add(btnOpts[i]);
}
this.add(txtResult);
this.add(btnEqual);
this.add(btnSpace);
this.add(btnClear);
}
class NumberActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
txtResult.requestFocus();
txtResult.setFocusable(true);
if (equalOperation) {
clearResult();
equalOperation = false;
}
if (isOperation) {
isOperation = false;
txtResult.setText("");
}
JButton btn_num = (JButton) arg0.getSource();
resultBuffer.append(btn_num.getText());
txtResult.setText(txtResult.getText() + btn_num.getText());
isEqualClick = true;
}
}
class OprationActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
txtResult.requestFocus();
if (resultBuffer.length() == 0) {
return;
}
JButton btn_opts = (JButton) arg0.getSource();
if (!isOperation) {
currentOpration = btn_opts.getText().charAt(0);
resultBuffer.append(currentOpration);
isOperation = true;
}
if (isOperation) {// 连续点击运算符,按最后一次操作符计算
resultBuffer.setCharAt(resultBuffer.length() - 1, ' ');
currentOpration = btn_opts.getText().charAt(0);
resultBuffer.append(currentOpration);
}
if (CalcutorUtil.isCalcute(resultBuffer.toString().substring(0,
resultBuffer.length() - 1))) {
txtResult.setText("");
result = CalcutorUtil.calcResult(resultBuffer.toString()
.substring(0, resultBuffer.length() - 1));
txtResult.setText(result + "");
resultBuffer = new StringBuffer(result + "" + currentOpration);
}