1

私のコードによると、ユーザーが等号を押した後、アプレットはテキストフィールドに簡略化された結果を表示する必要がありますが、そうではありません。

import java.awt.*;
import java.applet.*;



import java.awt.event.*;

/*

<applet code="Calculator height=300 width=300 >

</applet>

*/


import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

import javax.script.ScriptException;


public class Calculator extends Applet implements ActionListener{

Button b[]=new Button[10];

String tx="";

TextField field;

Button plus,minus,div,mul,equal;

    public void init()
{
        field=new TextField(12);

        add(field);

        for(int i=0;i<10;i++)
{
        b[i]=new Button(String.valueOf(i));

        add(b[i]);

        b[i].addActionListener(this);

        }

        plus=new Button("+");

        add(plus);

        plus.addActionListener(this);

        minus=new Button("-");

        add(minus);

        minus.addActionListener(this);

        div=new Button("/");

        add(div);

        div.addActionListener(this);

        mul=new Button("*");

        add(mul);

        mul.addActionListener(this);

        equal=new Button("=");

        add(equal);

        equal.addActionListener(this);


}//end of init


    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String msg=e.getActionCommand();

        if(msg.equals("0")){
                tx=tx+msg;      
                repaint();
                }

        if(msg.equals("1")){
                tx=tx+msg;      
                repaint();
                }

        if(msg.equals("2")){
                tx=tx+msg;      
                repaint();
                }
        if(msg.equals("3")){
                tx=tx+msg;      
                repaint();
                }
        if(msg.equals("4")){
                tx=tx+msg;      
                repaint();
                }
        if(msg.equals("5")){
                tx=tx+msg;      
                repaint();
                }
        if(msg.equals("6")){
                tx=tx+msg;      
                repaint();
                }
        if(msg.equals("7")){
                tx=tx+msg;      
                repaint();
                }
        if(msg.equals("8")){
                tx=tx+msg;      
                repaint();
                }
        if(msg.equals("+")){
            tx=tx+msg;      
            repaint();
        }
        if(msg.equals("-")){
            tx=tx+msg;      
            repaint();
        }
        if(msg.equals("*")){
            tx=tx+msg;      
            repaint();
        }
        if(msg.equals("/")){
            tx=tx+msg;      
            repaint();
        }
        if(msg.equals("=")){
            calculate(tx);
        }

    }

        public void paint(Graphics g){
            field.setText(tx);

    }

        public  void calculate(String tx){

            ScriptEngineManager factory = new ScriptEngineManager();

            ScriptEngine engine = factory.getEngineByName("JavaScript");

            Object obj = null;

            try {
            obj = engine.eval(tx);
            } catch (ScriptException ex) {
            ex.printStackTrace();
            }

             tx=obj.toString();
             repaint();

        }

}
4

1 に答える 1

1

メソッドでは、calculate結果を のローカル コピーに割り当てるだけString txです。代わりにクラス メンバー変数に割り当てる必要があります。

this.tx = obj.toString();

ただし、ここで設計を再考することをお勧めします。paintメソッドは、テキストを に設定するなどの単純な機能に関与するべきではありませんTextField。そのために使えますTextField#setText

于 2013-02-28T13:49:06.000 に答える