首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

计算器开发四则运算计算过程中得到最后操作符后,前一个操作符的值为null解决办法

2012-03-30 
计算器开发四则运算计算过程中得到最后操作符后,前一个操作符的值为null问题描述:    执行9+2操作,我先得

计算器开发四则运算计算过程中得到最后操作符后,前一个操作符的值为null
问题描述:
    执行9+2操作,我先得到“=”号的前一操作符“+”,执行“+”时得到了此符号,当执行“=”号后,“+”号这个    操作符的值为null
调试图为:
    
    

Java code
package com.zhyx.swing;import java.awt.BorderLayout;import java.awt.Button;import java.awt.Color;import java.awt.GridLayout;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;/** * 计算器功能实现类 * @author zhyx */public class TestCalculator extends JFrame {    /**     * TestCalculator初始化方法     */    public TextField t1;    public JButton btn[]= new JButton[17];;    public TestCalculator() {        setLayout(new BorderLayout());        JPanel p1=new JPanel();        t1=new TextField();        JPanel p2=new JPanel();        p2.setLayout(new GridLayout(4,4));        String []text={"7","8","9","/","4","5","6","*","1","2","3","-","0","+/-",".","+","="};                 int i;        for(i=0;i<btn.length;i++){            btn[i]=new JButton(text[i]);            p2.add(btn[i]);            btn[i].addActionListener(new Listener(this)); //持有外部类的引用        }        btn[0].setSize(20,30);         p1.setSize(300,10);        p1.setBackground(Color.gray);        p2.setSize(500,30);        t1.setBackground(Color.RED);        t1.setText("0.");        add(p1,BorderLayout.NORTH);        add(t1,BorderLayout.CENTER);        add(p2,BorderLayout.SOUTH);        setDefaultCloseOperation(EXIT_ON_CLOSE);        pack();        setVisible(true);                  }        /**     * @param args     */    public static void main(String[] args) {        TestCalculator test=new TestCalculator();            }                                                         //Button在之前定义的是数组类型,传到的是Button类型是否也可以    }    class Listener implements ActionListener{        private String lastop;  //存储上一操作符        private String thisnum;  //存储新输入的值        private String totalnum;        public Listener(){                    }        //获得外部类的t1参数方法2:持有外部类的引用          TestCalculator tc;        public Listener(TestCalculator tc){            this.tc=tc;        }        public void actionPerformed(ActionEvent e) {            Object obj = e.getSource();            String gl=((JButton) obj).getText();//            TextField t1=null //     应该得到TestCalculator类中的t1,初始化后会不会显示结果,所以不能这样写        //获得外部类的变量这种方法不成,T1传过来的值会为空值//            TextField t1=new TextField();            if(obj instanceof JButton){                //如果按钮里的String内容为int型,则返回到text上                try{                    //判断是否为数字                    if(Character.isDigit(gl.charAt(0))){                        int a=Integer.parseInt(((JButton) obj).getText());                        tc.t1.setText(""+a);                        totalnum=tc.t1.getText();                    }else{//对于操作符的操作                                                calulateByOpretor(gl);                                            }                }catch(Exception e1){                    //lastop=gl;  在此处建立传入的操作符和前一个操作符赋值是不正确的                    e1.printStackTrace();                }            }             //每一次点击完按钮后Text上的内容要清空//            t1.setText("");        }        private void calulateByOpretor(String op){            //改为传入操作符参数的做法            if(! op.equals("=")){                lastop=op; //对前一个操作符赋值            }else if{lastop!=null && "=".equals(op)){  //到等号时,前面的加号为null了,怎么回事                oprateByOp(lastop);            }        }        public void oprateByOp(String op){            if("+".equals(op)){                totalnum+=thisnum;                tc.t1.setText(totalnum);            }        }            }     

    


[解决办法]
一个listener对象持有一个lastop引用,按下+号的时候=号对应的listener中的lastop并没有值

热点排行
Bad Request.