首页 > 代码库 > 我的计算器

我的计算器

package lab16;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class Calculator extends JFrame{

private JPanel jp;
private JButton btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,
btnAdd,btnSub,btnMul,btnExc,btnDot,btnSign;
private JTextField txtNum;
private List<JButton> btnNums;

private double num = 0;
private String symbol = null;
private int flag = 0;

private static final long serialVersionUID = 1L;

public Calculator(){
super("我的计算器");

jp = new JPanel();
jp.setLayout(new GridLayout(4,4,10,10));
txtNum = new JTextField("0");
txtNum.setHorizontalAlignment(JTextField.RIGHT);
btnNums = new ArrayList<JButton>();
this.add(txtNum,BorderLayout.NORTH);

btn7 = new JButton("7");
btn8 = new JButton("8");
btn9 = new JButton("9");
btnExc = new JButton("/");
jp.add(btn7);
jp.add(btn8);
jp.add(btn9);
jp.add(btnExc);

btn4 = new JButton("4");
btn5 = new JButton("5");
btn6 = new JButton("6");
btnMul = new JButton("*");
jp.add(btn4);
jp.add(btn5);
jp.add(btn6);
jp.add(btnMul);

btn1 = new JButton("1");
btn2 = new JButton("2");
btn3 = new JButton("3");
btnSub = new JButton("-");
jp.add(btn1);
jp.add(btn2);
jp.add(btn3);
jp.add(btnSub);

btn0 = new JButton("0");
btnDot = new JButton(".");
btnSign = new JButton("=");
btnAdd = new JButton("+");
jp.add(btn0);
jp.add(btnDot);
jp.add(btnSign);
jp.add(btnAdd);

btnNums.add(btn0);btnNums.add(btn1);btnNums.add(btn2);btnNums.add(btn3);
btnNums.add(btn4);btnNums.add(btn5);btnNums.add(btn6);btnNums.add(btn7);
btnNums.add(btn8);btnNums.add(btn9);btnNums.add(btnAdd);btnNums.add(btnSub);
btnNums.add(btnMul);btnNums.add(btnExc);btnNums.add(btnDot);btnNums.add(btnSign);
for (int i = 0; i < btnNums.size(); i++) {
btnNums.get(i).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

if(flag==1 && (e.getActionCommand().equals("0") || e.getActionCommand().equals("1") || e.getActionCommand().equals("2") ||
e.getActionCommand().equals("3") || e.getActionCommand().equals("4") || e.getActionCommand().equals("5") ||
e.getActionCommand().equals("6") || e.getActionCommand().equals("7") || e.getActionCommand().equals("8") ||
e.getActionCommand().equals("9") || e.getActionCommand().equals("."))){
txtNum.setText(null);
}
if(txtNum.getText().equals("0")){
if(e.getActionCommand().equals("0")){
return;
}else{
txtNum.setText(null);
}
if(e.getActionCommand().equals(".")){
txtNum.setText("0.");
return;
}
}
if(txtNum.getText().indexOf(".") != -1 && e.getActionCommand().equals(".")){
return;
}

if(txtNum.getText() == null || txtNum.getText().equals("")){
if(e.getActionCommand().equals("+") || e.getActionCommand().equals("-") ||
e.getActionCommand().equals("*") || e.getActionCommand().equals("/")){
return;
}
}else if(e.getActionCommand().equals("+") || e.getActionCommand().equals("-") ||
e.getActionCommand().equals("*") || e.getActionCommand().equals("/")){
num =Double.valueOf(txtNum.getText());
symbol = e.getActionCommand();
txtNum.setText(null);
return;
}

if(e.getActionCommand().equals("=") && symbol == null){
return;
}else if(e.getActionCommand().equals("=") && symbol.equals("+")){
num += Double.valueOf(txtNum.getText());
String s = String.valueOf(num);
txtNum.setText(s);
symbol = null;
flag = 1;
return;
}else if(e.getActionCommand().equals("=") && symbol.equals("-")){
num -= Double.valueOf(txtNum.getText());
String s = String.valueOf(num);
txtNum.setText(s);
symbol = null;
flag = 1;
return;
}else if(e.getActionCommand().equals("=") && symbol.equals("*")){
num *= Double.valueOf(txtNum.getText());
String s = String.valueOf(num);
txtNum.setText(s);
symbol = null;
flag = 1;
return;
}else if(e.getActionCommand().equals("=") && symbol.equals("/")){
num /= Double.valueOf(txtNum.getText());
String s = String.valueOf(num);
txtNum.setText(s);
symbol = null;
flag = 1;
return;
}
flag = 0;
txtNum.setText(txtNum.getText()+e.getActionCommand());
}
});
}

this.add(jp);
this.setSize(300, 250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
}

public static void main(String [] args){
new Calculator();
}

}

我的计算器