Java Applet编译得很好但不运行

时间:2012-08-18 08:05:45

标签: java applet

这个java Applet出了什么问题,即使我编译它也没有问题它没有运行。

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
   }
}

3 个答案:

答案 0 :(得分:1)

我强烈怀疑是NumberFormatException被抛出。

毕竟,只要applet尝试绘制自己的 - 包括在初始化后立即 - 你将运行这段代码:

// 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

我觉得这个applet的一般设计是不合适的。为什么要将drawString用于基本标签?

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

然后,您应该将Integer.parseInt调用放入try / catch块,捕获NumberFormatException。 (您也可以考虑使用NumberFormat代替Integer.parseInt,但稍后可以这样做......)

答案 1 :(得分:0)

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

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
  }
}

答案 2 :(得分:0)

问题在于:

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

as

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

将无法按照Jon Skeet的说明解析""throw exception

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

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

OR有一些初始可解析的String

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