View包
display.java
package View;
import Controller.control;
import Model.Data;
import javax.swing.*;
import java.awt.*;
/**
* Created by My-Computer on 2016/5/15.
*/
public class display extends JFrame{
JTextField text = null;
JPanel buttonPanel = new JPanel();
MyButton[][] allButton = new MyButton[6][4];
Data data = new Data();
String[] buttonTitle = {"%", "sqrt", "square", "1/x", "CE", "C", "<-", "/", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "+/-", "0", ".", "="};
public display(){
GridBagConstraints c = new GridBagConstraints();
GridBagLayout grid = new GridBagLayout();
text = new JTextField();
setTitle("Calculator");
setLayout(grid);
text.setFont(new Font("微软雅黑", Font.PLAIN, 16));
c.weightx = 1.0;
c.fill = GridBagConstraints.BOTH;
c.gridwidth = GridBagConstraints.REMAINDER;
grid.setConstraints(text, c);
add(text);
c.weighty = 1.0;
grid.setConstraints(buttonPanel, c);
add(buttonPanel);
buttonPanel.setLayout(new GridLayout(6, 4));
for (int i = 0; i < 6; i++){
for (int j = 0; j < 4; j++){
allButton[i][j] = new MyButton(buttonTitle[i*4+j], buttonPanel, text, data);
}
}
setVisible(true);
pack();
}
public static void main(String[] args) {
new display();
}
}
class MyButton extends JButton{
public MyButton(String title, JPanel father, JTextField text, Data data){
setText(title);
father.add(this);
addActionListener(new control(text, data));
}
}
Model包
Data.java
package Model;
/**
* Created by My-Computer on 2016/5/15.
*/
public class Data {
public static double eps = 1e-5;
public double answer;
public double operation;
public boolean flag = false;
public boolean isEnd = false;
public int operator;
/*
* operator=0 +
* operator=1 -
* operator=2 *
* operator=3 /
* operator=4 %
*/
public Data(){
init();
}
public void init(){
answer = 0;
operation = 0;
operator = -1;
flag = false;
isEnd = false;
}
}
Operator.java
package Model;
/**
* Created by My-Computer on 2016/5/15.
*/
public class Operator{
public static double Calc(Data date, String op) throws ArithmeticException{
double ret = 0;
switch (date.operator){
case -1: date.answer = date.operation; break;
case 0: date.answer += date.operation; break;
case 1: date.answer -= date.operation; break;
case 2: date.answer *= date.operation; break;
case 3: if (Math.abs(date.operation) < date.eps){
throw new ArithmeticException();
}
date.answer /= date.operation; break;
case 4: date.answer %= date.operation; break;
}
if (op.equals("+")){
date.operator = 0;
}else if (op.equals("-")){
date.operator = 1;
}else if (op.equals("*")){
date.operator = 2;
}else if (op.equals("/")){
date.operator = 3;
}else if (op.equals("%")){
date.operator = 4;
}else if (op.equals("=")){
date.operator = -1;
}
return ret;
}
}
Controller包
control.java
package Controller;
import Model.Data;
import Model.Operator;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Arc2D;
/**
* Created by My-Computer on 2016/5/15.
*/
public class control implements ActionListener{
private JTextField text;
private Data data;
public control(JTextField text, Data data){
this.text = text;
this.data = data;
}
@Override
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (data.isEnd){
if (s=="C"){
data.init();
text.setText("");
}
return;
}
if (s.length()==1 && "0123456789.".indexOf(s) >= 0){
HandleNumber(s);
}else {
HandleOperator(s);
}
}
private void HandleNumber(String s){
String tmp = text.getText();
if (data.flag){
System.out.println(text.getText());
text.setText("");
System.out.println(text.getText());
data.flag = false;
}
if (s.equals(".")){
if(tmp.indexOf(".") >= 0){
return;
}else if (tmp.length()==0){
text.setText("0.");
}else{
text.setText(tmp+".");
}
}else if (s.equals("0")){
if (tmp.equals('0')){
return;
}
text.setText(tmp+"0");
}else{
text.setText(tmp+s);
}
}
private void HandleOperator(String s){
String tmp = text.getText();
try {
if (s.equals("<-")) {
if (data.flag){
text.setText("");
data.flag = false;
return;
}
if (tmp.length() == 0) {
return;
} else {
text.setText(tmp.substring(0, tmp.length() - 1));
}
} else if (s.equals("CE")) {
text.setText("");
} else if (s.equals("C")) {
data.init();
text.setText("");
} else if (s.equals("+/-")) {
if (tmp.indexOf('-') < 0) {
text.setText('-' + tmp);
} else {
text.setText(tmp.substring(1, tmp.length()));
}
} else if (s.equals("sqrt")) {
text.setText(Double.toString(Math.sqrt(Double.parseDouble(tmp))));
data.flag = true;
}else if (s.equals("1/x")){
if(Double.parseDouble(tmp) == 0){
throw new ArithmeticException();
}
text.setText(Double.toString(1/Double.parseDouble(tmp)));
data.flag = true;
}else if (s.equals("square")){
text.setText(Double.toString(Math.pow(Double.parseDouble(tmp) , 2)));
data.flag = true;
}else{
data.operation = Double.parseDouble(tmp);
Operator.Calc(data, s);
text.setText("");
if (s=="="){
text.setText(Double.toString(data.answer));
data.isEnd = true;
}
}
}catch (ArithmeticException e){
JOptionPane.showMessageDialog(null, "0不能作为除数!", "Error!", JOptionPane.ERROR_MESSAGE);
text.setText("");
}catch (NumberFormatException e){
return;
}
}
}