- package test;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- /**
- * Java Application-calculate
- */
- public class Calc extends JFrame {
- private String oprecord="";//record the all operate contains number button and operate button
- public void recordOp(String str){
- oprecord+=str;
- // System.out.println(oprecord);
- }
- private class Listener implements ActionListener{
- public void judgeInit(){
- if(oprecord.length()>3){
- String str=oprecord.substring(oprecord.length()-2,oprecord.length()-1);
- if(str.equals("=")){
- tmpval="";
- }
- }
- }
- public void actionPerformed(ActionEvent e) {
- if(e.getSource()==btn1){
- recordOp("1");judgeInit();
- val+="1";
- txt.setText(val);
- }
- if(e.getSource()==btn2){
- recordOp("2");judgeInit();
- val+="2";
- txt.setText(val);
- }
- if(e.getSource()==btn3){
- recordOp("3");judgeInit();
- val+="3";
- txt.setText(val);
- }
- if(e.getSource()==btn4){
- recordOp("4");judgeInit();
- val+="4";
- txt.setText(val);
- }
- if(e.getSource()==btn5){
- recordOp("5");judgeInit();
- val+="5";
- txt.setText(val);
- }
- if(e.getSource()==btn6){
- recordOp("6");judgeInit();
- val+="6";
- txt.setText(val);
- }
- if(e.getSource()==btn7){
- recordOp("7");judgeInit();
- val+="7";
- txt.setText(val);
- }
- if(e.getSource()==btn8){
- recordOp("8");judgeInit();
- val+="8";
- txt.setText(val);
- }
- if(e.getSource()==btn9){
- recordOp("9");judgeInit();
- val+="9";
- txt.setText(val);
- }
- if(e.getSource()==btn0){
- if(val!="0"){
- recordOp("0");
- val+="0";
- txt.setText(val);
- }
- }
- if(e.getSource()==btnDot){
- if(val!=""&&!val.contains(".")){
- recordOp(".");
- val+=".";
- txt.setText(val);
- }
- }
- if(e.getSource()==btnAdd){//
- //该片段来自于http://www.codesnippet.cn/detail/280220148831.html
来源: http://www.codesnippet.cn/detail/280220148831.html