各位大虾请问下J2ME代码在WTK中的如下问题怎么解决啊?

时间:2021-03-27 03:23:04
本人刚学J2ME不久,是菜鸟。遇到了这样的问题:Project "Calculator" loaded
Project settings saved
Building "Calculator"
C:\WTK22\apps\Calculator\src\CalcForm.java:15: 错误: 找不到符号
              showArea = new CalcScreen();
              ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:19: 错误: 找不到符号
              showArea.setLayout(
              ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:21: 错误: 找不到符号
              append(showArea);
                     ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:43: 错误: 找不到符号
               && showArea.getText().indexOf(CalcKeyboard.SYMBOL_DOT)==-1){
                  ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:47: 错误: 找不到符号
             String text=showArea.getText();
                         ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:50: 错误: 找不到符号
                 showArea.setText(text);
                 ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:54: 错误: 找不到符号
             showArea.setText("0.");
             ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:63: 错误: 找不到符号
              String s=showArea.getText();
                       ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:66: 错误: 找不到符号
              showArea.setText(String.valueOf(acc));
              ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:69: 错误: 找不到符号
              String str=showArea.getText();
                         ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:71: 错误: 找不到符号
                  showArea.setText(str.substring(1,str.length()));
                  ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:74: 错误: 找不到符号
                 showArea.setText("-"+str);
                 ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:131: 错误: 找不到符号
               showArea.setText("0.");
               ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:136: 错误: 找不到符号
              showArea.setText(showArea.getText()+str);
                               ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:136: 错误: 找不到符号
              showArea.setText(showArea.getText()+str);
              ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcForm.java:139: 错误: 找不到符号
              showArea.setText(str);
              ^
  符号:   变量 showArea
  位置: 类 CalcForm
C:\WTK22\apps\Calculator\src\CalcKeyboard.java:24: 错误: 不兼容的类型
      private static final String PRESSED=0;
                                          ^
  需要: String
  找到:    int
C:\WTK22\apps\Calculator\src\CalcKeyboard.java:25: 错误: 不兼容的类型
      private static final String RELEASED=1;
                                           ^
  需要: String
  找到:    int
C:\WTK22\apps\Calculator\src\CalcKeyboard.java:35: 错误: 不兼容的类型
      private int keyState=RELEASED;
                           ^
  需要: int
  找到:    String
C:\WTK22\apps\Calculator\src\CalcKeyboard.java:72: 错误: 对于==(int,String), 找不到合适的方法
        if(keyState==RELEASED){
                   ^
    方法 <匿名>.==(int,int)不适用
      (无法通过方法调用转换将实际参数String转换为int)
    方法 <匿名>.==(long,long)不适用
      (无法通过方法调用转换将实际参数String转换为long)
    方法 <匿名>.==(float,float)不适用
      (无法通过方法调用转换将实际参数String转换为float)
    方法 <匿名>.==(double,double)不适用
      (无法通过方法调用转换将实际参数String转换为double)
    方法 <匿名>.==(boolean,boolean)不适用
      (无法通过方法调用转换将实际参数int转换为boolean)
    方法 <匿名>.==(Object,Object)不适用
      (无法通过方法调用转换将实际参数int转换为Object)
C:\WTK22\apps\Calculator\src\CalcKeyboard.java:75: 错误: 对于==(int,String), 找不到合适的方法
        else if(keyState== PRESSED){
                        ^
    方法 <匿名>.==(int,int)不适用
      (无法通过方法调用转换将实际参数String转换为int)
    方法 <匿名>.==(long,long)不适用
      (无法通过方法调用转换将实际参数String转换为long)
    方法 <匿名>.==(float,float)不适用
      (无法通过方法调用转换将实际参数String转换为float)
    方法 <匿名>.==(double,double)不适用
      (无法通过方法调用转换将实际参数String转换为double)
    方法 <匿名>.==(boolean,boolean)不适用
      (无法通过方法调用转换将实际参数int转换为boolean)
    方法 <匿名>.==(Object,Object)不适用
      (无法通过方法调用转换将实际参数int转换为Object)
C:\WTK22\apps\Calculator\src\CalcKeyboard.java:89: 错误: 不兼容的类型
     keyState=PRESSED;
              ^
  需要: int
  找到:    String
C:\WTK22\apps\Calculator\src\CalcKeyboard.java:96: 错误: 不兼容的类型
       keyState=RELEASED;
                ^
  需要: int
  找到:    String
23 个错误
com.sun.kvem.ktools.ExecutionException
Build failed

2 个解决方案

#1


这是CalcForm.java里的程序代码:
import javax.microedition.lcdui.*;
public class CalcForm extends Form implements CalcKeyboardListener{
       private CalcScreen ShowArea;//计算器的显示区
       private CalcKeyboard ckeyboard;//计算器键盘

       private boolean hasNewOperand=false;
       private boolean numInputing=false;
       private double acc=0.0;
       private String operator="";
       private double operand=0.0;

       public CalcForm(){
              super("计算器");
              showArea = new CalcScreen();
              ckeyboard = new CalcKeyboard(4,5);
              ckeyboard.setCalcKeyboardListener(this);

              showArea.setLayout(
                     Item.LAYOUT_2|Item.LAYOUT_CENTER|Item.LAYOUT_NEWLINE_AFTER);
              append(showArea);
              append(new Spacer(this.getWidth(),5));
              ckeyboard.setLayout(Item.LAYOUT_2|Item.LAYOUT_CENTER);
              append(ckeyboard);

              reset();
              }

     public void actionPerformmed(CalcKeyboard btn,String symbol){
        if(symbol==CalcKeyboard.NUM_ZERO||
              symbol==CalcKeyboard.NUM_ONE||
              symbol==CalcKeyboard.NUM_TWO||
              symbol==CalcKeyboard.NUM_THREE||
              symbol==CalcKeyboard.NUM_FOUR||
              symbol==CalcKeyboard.NUM_FIVE||
              symbol==CalcKeyboard.NUM_SIX||
              symbol==CalcKeyboard.NUM_SEVEN||
              symbol==CalcKeyboard.NUM_EIGHT||
              symbol==CalcKeyboard.NUM_NINE) {
               inputNum(symbol);
              }
         else if(symbol==CalcKeyboard.SYMBOL_DOT
               && showArea.getText().indexOf(CalcKeyboard.SYMBOL_DOT)==-1){
                 inputNum(symbol);
               }
         else if(symbol == CalcKeyboard.BACKSPACE){
             String text=showArea.getText();
               if(text.length()>0){
                 text = text.substring(0,text.length()-1);
                 showArea.setText(text);
                  }
                 }
         else if(symbol==CalcKeyboard.CE){
             showArea.setText("0.");
             }
         else if(symbol==CalcKeyboard.C){
             reset();
             }
         else if(symbol==CalcKeyboard.ADD||symbol==CalcKeyboard.MINUS||
              symbol==CalcKeyboard.MULT||symbol==CalcKeyboard.DIVIDE||
                    symbol==CalcKeyboard.EQUALS){
              numInputing=false;
              String s=showArea.getText();
              double d=Double.parseDouble(s);
              jisuan(d,symbol);
              showArea.setText(String.valueOf(acc));
               }
          else if(symbol==CalcKeyboard.SYMBOL_MINUS){
              String str=showArea.getText();
               if(str.charAt(0)=='-'){
                  showArea.setText(str.substring(1,str.length()));
                 }
                else {
                 showArea.setText("-"+str);
                }
            }
        }
     private void jisuan(double exp,String oper){
              if(operator.equals("")){
               acc=exp;
               operand=exp;
              }
           else{
               if(hasNewOperand){
                   operand=exp;
                   if(operator.equals(CalcKeyboard.ADD)){
                      acc+=operand;
                      }
                    
                else if (operator.equals(CalcKeyboard.MINUS)){
                      acc-=operand;
                      }
                else if (operator.equals(CalcKeyboard.MULT)){
                      acc*=operand;
                      } 
                else if (operator.equals(CalcKeyboard.DIVIDE)){
                      acc/=operand;
                      }
                 }
                 
               else{
                  if(oper.equals(CalcKeyboard.EQUALS)){
                    if(operator.equals(CalcKeyboard.ADD)){
                       acc+=operand;
                       }
                     else if(operator.equals(CalcKeyboard.MINUS)){
                        acc-=operand;
                       }
                     else if(operator.equals(CalcKeyboard.MULT)){
                        acc*=operand;
                       }
                     else if(operator.equals(CalcKeyboard.DIVIDE)){
                       acc/=operand;
                       }
                     if(!oper.equals(CalcKeyboard.EQUALS)){
                        operator=oper;
                       }
                     }
                   }
                 }
               if(!oper.equals(CalcKeyboard.EQUALS)){
                   operator=oper;
                  }
                  hasNewOperand=false;
          }
          private void reset(){
               hasNewOperand=false;
               numInputing=false;
               acc=0.0;
               operator="";
               showArea.setText("0.");
              }

          private void inputNum(String str){
               if(numInputing){
              showArea.setText(showArea.getText()+str);
              }
           else{
              showArea.setText(str);
              numInputing=true;
               }
               hasNewOperand=true;
          }
}//类的

#2


这是CalcKeyboard .java的代码
import javax.microedition.lcdui.*;
public class CalcKeyboard extends CustomItem{
      public static final String BACKSPACE="<-";
      public static final String CE="CE";
      public static final String C="C";
      public static final String SYMBOL_MINUS="+/-";
      public static final String NUM_ZERO="0";
      public static final String NUM_ONE="1";
      public static final String NUM_TWO="2";
      public static final String NUM_THREE="3";
      public static final String NUM_FOUR="4";
      public static final String NUM_FIVE="5";
      public static final String NUM_SIX="6";
      public static final String NUM_SEVEN="7";
      public static final String NUM_EIGHT="8";
      public static final String NUM_NINE="9";
      public static final String SYMBOL_DOT=".";
      public static final String ADD="+";
      public static final String MINUS="-";
      public static final String MULT="*";
      public static final String DIVIDE="/";
      public static final String EQUALS="=";

      private static final String PRESSED=0;
      private static final String RELEASED=1;

      private CalcKeyboardListener ckListener;
      private Font textFont;
      private int col;
      private int row;
      private int btnWidth;
      private int btnHeight;
      private int hSpace=4;
      private int vSpace=4;
      private int keyState=RELEASED;
      private String[] keyLabel={
          BACKSPACE,CE,C,SYMBOL_MINUS,
          NUM_SEVEN,NUM_EIGHT,NUM_NINE,DIVIDE,
          NUM_FOUR,NUM_FIVE,NUM_SIX,MULT,
          NUM_ONE,NUM_TWO,NUM_THREE,MINUS,
          NUM_ZERO,SYMBOL_DOT,EQUALS,ADD
          };

      public CalcKeyboard(int col,int row){
         super(null);
         textFont=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE);
         this.col=col;
         this.row=row;
         btnHeight=textFont.getHeight()+4;
         btnWidth=btnHeight+10;
        }
      protected int getMinContentHeight(){
         return row*(btnHeight+vSpace)-vSpace;
        }
      protected int getMinContentWidth(){
         return col*(btnWidth+hSpace)-hSpace;
        }
      protected int getPrefContentHeight(int width){
         return getMinContentHeight();
        }
     protected int getPrefContentWidth(int height){
         return getMinContentWidth();
        }
     protected void paint(Graphics g,int w,int h){
        for(int i=0;i<keyLabel.length;i++){
            drawButton(g,keyLabel[i],i%col*(btnWidth+hSpace),i/col*                          

(btnHeight+vSpace),btnWidth,btnHeight);
           }
       }
    private void drawButton(Graphics g,String str, int x,int y,int w,int h){
        g.setColor(160,160,255);
        g.drawRect(x,y,w-1,h-1);
        if(keyState==RELEASED){
         g.setColor(240,240,255);
        }
        else if(keyState== PRESSED){
           g.setColor(210,210,255);
           }
        g.fillRect(x+2,y+2,w-4,h-4);
        g.setColor(0,0,0);
        g.setFont(textFont);
        g.drawString(str,x+w/2,y+h,Graphics.BOTTOM|Graphics.HCENTER);
       }
    private int getIndex(int x,int y){
      int j=x/(btnWidth+hSpace);
      int i=y/(btnHeight+vSpace);
      return (col*i)+j;
      }
   protected void pointerPressed(int x,int y){
     keyState=PRESSED;
     int ax=x-x%(btnWidth+hSpace);
     int ay=y-y%(btnHeight+vSpace);
     repaint(ax,ay,btnWidth,btnHeight);
     }

   protected void pointerReleased(int x,int y){
       keyState=RELEASED;
       int ax=x-x%(btnWidth+hSpace);
       int ay=y-y%(btnHeight+vSpace);
       repaint(ax,ay,btnWidth,btnHeight);
       if(ckListener != null ){
          int index = getIndex(x,y);
          ckListener.actionPerformmed(this,keyLabel[index]);
       }
 }
 public void setCalcKeyboardListener(CalcKeyboardListener ckListener){
    this.ckListener=ckListener;
   }
}

#1


这是CalcForm.java里的程序代码:
import javax.microedition.lcdui.*;
public class CalcForm extends Form implements CalcKeyboardListener{
       private CalcScreen ShowArea;//计算器的显示区
       private CalcKeyboard ckeyboard;//计算器键盘

       private boolean hasNewOperand=false;
       private boolean numInputing=false;
       private double acc=0.0;
       private String operator="";
       private double operand=0.0;

       public CalcForm(){
              super("计算器");
              showArea = new CalcScreen();
              ckeyboard = new CalcKeyboard(4,5);
              ckeyboard.setCalcKeyboardListener(this);

              showArea.setLayout(
                     Item.LAYOUT_2|Item.LAYOUT_CENTER|Item.LAYOUT_NEWLINE_AFTER);
              append(showArea);
              append(new Spacer(this.getWidth(),5));
              ckeyboard.setLayout(Item.LAYOUT_2|Item.LAYOUT_CENTER);
              append(ckeyboard);

              reset();
              }

     public void actionPerformmed(CalcKeyboard btn,String symbol){
        if(symbol==CalcKeyboard.NUM_ZERO||
              symbol==CalcKeyboard.NUM_ONE||
              symbol==CalcKeyboard.NUM_TWO||
              symbol==CalcKeyboard.NUM_THREE||
              symbol==CalcKeyboard.NUM_FOUR||
              symbol==CalcKeyboard.NUM_FIVE||
              symbol==CalcKeyboard.NUM_SIX||
              symbol==CalcKeyboard.NUM_SEVEN||
              symbol==CalcKeyboard.NUM_EIGHT||
              symbol==CalcKeyboard.NUM_NINE) {
               inputNum(symbol);
              }
         else if(symbol==CalcKeyboard.SYMBOL_DOT
               && showArea.getText().indexOf(CalcKeyboard.SYMBOL_DOT)==-1){
                 inputNum(symbol);
               }
         else if(symbol == CalcKeyboard.BACKSPACE){
             String text=showArea.getText();
               if(text.length()>0){
                 text = text.substring(0,text.length()-1);
                 showArea.setText(text);
                  }
                 }
         else if(symbol==CalcKeyboard.CE){
             showArea.setText("0.");
             }
         else if(symbol==CalcKeyboard.C){
             reset();
             }
         else if(symbol==CalcKeyboard.ADD||symbol==CalcKeyboard.MINUS||
              symbol==CalcKeyboard.MULT||symbol==CalcKeyboard.DIVIDE||
                    symbol==CalcKeyboard.EQUALS){
              numInputing=false;
              String s=showArea.getText();
              double d=Double.parseDouble(s);
              jisuan(d,symbol);
              showArea.setText(String.valueOf(acc));
               }
          else if(symbol==CalcKeyboard.SYMBOL_MINUS){
              String str=showArea.getText();
               if(str.charAt(0)=='-'){
                  showArea.setText(str.substring(1,str.length()));
                 }
                else {
                 showArea.setText("-"+str);
                }
            }
        }
     private void jisuan(double exp,String oper){
              if(operator.equals("")){
               acc=exp;
               operand=exp;
              }
           else{
               if(hasNewOperand){
                   operand=exp;
                   if(operator.equals(CalcKeyboard.ADD)){
                      acc+=operand;
                      }
                    
                else if (operator.equals(CalcKeyboard.MINUS)){
                      acc-=operand;
                      }
                else if (operator.equals(CalcKeyboard.MULT)){
                      acc*=operand;
                      } 
                else if (operator.equals(CalcKeyboard.DIVIDE)){
                      acc/=operand;
                      }
                 }
                 
               else{
                  if(oper.equals(CalcKeyboard.EQUALS)){
                    if(operator.equals(CalcKeyboard.ADD)){
                       acc+=operand;
                       }
                     else if(operator.equals(CalcKeyboard.MINUS)){
                        acc-=operand;
                       }
                     else if(operator.equals(CalcKeyboard.MULT)){
                        acc*=operand;
                       }
                     else if(operator.equals(CalcKeyboard.DIVIDE)){
                       acc/=operand;
                       }
                     if(!oper.equals(CalcKeyboard.EQUALS)){
                        operator=oper;
                       }
                     }
                   }
                 }
               if(!oper.equals(CalcKeyboard.EQUALS)){
                   operator=oper;
                  }
                  hasNewOperand=false;
          }
          private void reset(){
               hasNewOperand=false;
               numInputing=false;
               acc=0.0;
               operator="";
               showArea.setText("0.");
              }

          private void inputNum(String str){
               if(numInputing){
              showArea.setText(showArea.getText()+str);
              }
           else{
              showArea.setText(str);
              numInputing=true;
               }
               hasNewOperand=true;
          }
}//类的

#2


这是CalcKeyboard .java的代码
import javax.microedition.lcdui.*;
public class CalcKeyboard extends CustomItem{
      public static final String BACKSPACE="<-";
      public static final String CE="CE";
      public static final String C="C";
      public static final String SYMBOL_MINUS="+/-";
      public static final String NUM_ZERO="0";
      public static final String NUM_ONE="1";
      public static final String NUM_TWO="2";
      public static final String NUM_THREE="3";
      public static final String NUM_FOUR="4";
      public static final String NUM_FIVE="5";
      public static final String NUM_SIX="6";
      public static final String NUM_SEVEN="7";
      public static final String NUM_EIGHT="8";
      public static final String NUM_NINE="9";
      public static final String SYMBOL_DOT=".";
      public static final String ADD="+";
      public static final String MINUS="-";
      public static final String MULT="*";
      public static final String DIVIDE="/";
      public static final String EQUALS="=";

      private static final String PRESSED=0;
      private static final String RELEASED=1;

      private CalcKeyboardListener ckListener;
      private Font textFont;
      private int col;
      private int row;
      private int btnWidth;
      private int btnHeight;
      private int hSpace=4;
      private int vSpace=4;
      private int keyState=RELEASED;
      private String[] keyLabel={
          BACKSPACE,CE,C,SYMBOL_MINUS,
          NUM_SEVEN,NUM_EIGHT,NUM_NINE,DIVIDE,
          NUM_FOUR,NUM_FIVE,NUM_SIX,MULT,
          NUM_ONE,NUM_TWO,NUM_THREE,MINUS,
          NUM_ZERO,SYMBOL_DOT,EQUALS,ADD
          };

      public CalcKeyboard(int col,int row){
         super(null);
         textFont=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE);
         this.col=col;
         this.row=row;
         btnHeight=textFont.getHeight()+4;
         btnWidth=btnHeight+10;
        }
      protected int getMinContentHeight(){
         return row*(btnHeight+vSpace)-vSpace;
        }
      protected int getMinContentWidth(){
         return col*(btnWidth+hSpace)-hSpace;
        }
      protected int getPrefContentHeight(int width){
         return getMinContentHeight();
        }
     protected int getPrefContentWidth(int height){
         return getMinContentWidth();
        }
     protected void paint(Graphics g,int w,int h){
        for(int i=0;i<keyLabel.length;i++){
            drawButton(g,keyLabel[i],i%col*(btnWidth+hSpace),i/col*                          

(btnHeight+vSpace),btnWidth,btnHeight);
           }
       }
    private void drawButton(Graphics g,String str, int x,int y,int w,int h){
        g.setColor(160,160,255);
        g.drawRect(x,y,w-1,h-1);
        if(keyState==RELEASED){
         g.setColor(240,240,255);
        }
        else if(keyState== PRESSED){
           g.setColor(210,210,255);
           }
        g.fillRect(x+2,y+2,w-4,h-4);
        g.setColor(0,0,0);
        g.setFont(textFont);
        g.drawString(str,x+w/2,y+h,Graphics.BOTTOM|Graphics.HCENTER);
       }
    private int getIndex(int x,int y){
      int j=x/(btnWidth+hSpace);
      int i=y/(btnHeight+vSpace);
      return (col*i)+j;
      }
   protected void pointerPressed(int x,int y){
     keyState=PRESSED;
     int ax=x-x%(btnWidth+hSpace);
     int ay=y-y%(btnHeight+vSpace);
     repaint(ax,ay,btnWidth,btnHeight);
     }

   protected void pointerReleased(int x,int y){
       keyState=RELEASED;
       int ax=x-x%(btnWidth+hSpace);
       int ay=y-y%(btnHeight+vSpace);
       repaint(ax,ay,btnWidth,btnHeight);
       if(ckListener != null ){
          int index = getIndex(x,y);
          ckListener.actionPerformmed(this,keyLabel[index]);
       }
 }
 public void setCalcKeyboardListener(CalcKeyboardListener ckListener){
    this.ckListener=ckListener;
   }
}