1

这个 javaApplet有什么问题,即使我编译它没有问题它也没有运行。

import java.applet.*;//Importing java.applet
public class MyApplet extends Applet {
   TextField txt1, txt2;
   public void init(){//Initializing our applet
       txt1 = new TextField(""); //Creates a textfield 'txt1'
       txt2 = new TextField(""); //Creates a textfield 'txt2'
       setBackground(Color.CYAN);//Setting background color to CYAN
       add(txt1); //Adds textfield 'txt1' to your applet
       add(txt2); //Adds textfield 'txt2' to your applet
   }
   public void paint(Graphics obj){//Paint method to display our message
       String s1 =  txt1.getText(); //Fetching data from text field 1.
       String s2 =  txt2.getText(); //Fetching data from text field 2.
       int num1=0, num2 = 0, num3;   //Declaring 3 integer variables    
       num1 = Integer.parseInt(s1); //Parsing the string value of text field 1 to integer
       num2 = Integer.parseInt(s2); //Parsing the string value of text field 2 in integer
       num3 = num1 + num2;       //Performing addition
       String s3 = String.valueOf(num3); //Converting the result from integer to string
       obj.drawString("Result:", 40, 50);
       obj.drawString(s3, 50, 50);//To display the result
   }
}
4

3 回答 3

1

我强烈怀疑 aNumberFormatException被抛出。

毕竟,任何时候小程序试图绘制自己——包括初始化之后——你都会运行这个代码:

// Comments removed as they were more distracting than useful. You really
// *don't* need to comment variable declarations to say they're declarations...
String s1 =  txt1.getText();
String s2 =  txt2.getText();
int num1=0, num2 = 0, num3; 
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);

因此,当txt1.getText()返回一个空字符串时,它会在用户有机会键入任何内容之前返回,您将解析该空字符串,这将抛出一个NumberFormatException.

我觉得这个小程序的总体设计是不合适的。为什么要使用drawString本质上是标签的东西?

我会添加一个或两个Label控件 - 一个用于完整文本“结果:”和结果,或者一个用于“结果:”,另一个用于结果。然后您根本不需要覆盖paint()- 您可以添加处理程序以应对文本框内容更改- 毕竟,这是您唯一需要更改任何内容的时间。

然后,您应该将Integer.parseInt调用放入 try/catch 块中,捕获NumberFormatException. (您也可以考虑使用 aNumberFormat而不是Integer.parseInt,但您可以稍后再做...)

于 2012-08-18T08:09:59.240 回答
0

问题出在:

txt1 = new TextField("");
txt2 = new TextField("");

作为

String s1 =  txt1.getText(); 
String s2 =  txt2.getText();
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);

将无法解析"",并且会throw exception按照 Jon Skeet 的解释。

所以修复可能是一个try catch块(异常处理):

try{
    num1 = Integer.parseInt(s1);
    num2 = Integer.parseInt(s2);
   }catch(NumberFormatException ex){
       System.out.println(ex);
   }

或者有一些初始的可解析String

txt1 = new TextField("0"); 
txt2 = new TextField("0");
于 2012-08-18T08:52:28.990 回答
0

主要问题是无法正确绘制琴弦。paint在您有任何输入之前,applet 会多次调用该方法。因此,文本字段有空字符串。这是因为一个NumberFormatException. 检查文本是否有空值。如果您解析数字添加try/catch块。此外,将逻辑移至方法calculaterepaint在您进行输入时启动它。如果您向文本字段添加一些侦听器,则可能会发生这种情况。

import java.applet.*;
import java.awt.*;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;//Importing java.applet
public class MyApplet extends Applet
{
  TextField txt1, txt2;
  String s3;
  public void init()//Initializing our applet
  {
    txt1 = new TextField(""); //Creates a textfield 'txt1'
    txt2 = new TextField(""); //Creates a textfield 'txt2'
    txt1.addTextListener(new TextListener(){
      public void textValueChanged(TextEvent e) {
        calculate();
      }
    });
    txt2.addTextListener(new TextListener(){
      public void textValueChanged(TextEvent e) {
        calculate();
      }
    });
    setBackground(Color.CYAN);//Setting background color to CYAN
    add(txt1); //Adds textfield 'txt1' to your applet
    add(txt2); //Adds textfield 'txt2' to your applet
  }

  void calculate(){
    try {
      String s1 =  txt1.getText(); //Fetching data from text field 1.
      String s2 =  txt2.getText(); //Fetching data from text field 2.
      int num1=0, num2 = 0, num3;  //Declaring 3 integer variables
      num1 = Integer.parseInt(s1); //Parsing the string value of text field 1 to integer
      num2 = Integer.parseInt(s2); //Parsing the string value of text field 2 in integer
      num3 = num1 + num2;          //Performing addition
      s3 = String.valueOf(num3); //Converting the result from integer to string
      repaint();
    } catch (NumberFormatException nfe){}

  }
  public void paint(Graphics obj)//Paint method to display our message
  {
    super.paint(obj);
    obj.drawString("Result:", 100, 100);
    if (s3 != null)
      obj.drawString(s3, 150, 100);//To display the result
  }
}
于 2012-08-18T08:30:56.683 回答