Java小程序编译得很好,但无法运行

Java小程序编译得很好,但无法运行,java,applet,Java,Applet,这个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 't

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

我强烈怀疑正在抛出
NumberFormatException


毕竟,每当小程序尝试自己绘制时(包括初始化之后),您都将运行以下代码:

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

我觉得这个小程序的总体设计不合适。为什么您要使用
拉绳
作为基本标签

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


然后应该将
Integer.parseInt
调用放入try/catch块,捕捉
NumberFormatException
。(也可以考虑使用<代码>编号格式>代码>而不是<代码>整型。PARSETIN < /代码>,但稍后您可以这样做……

< P>我强烈怀疑正在抛出一个<代码> No.MultFrimeExtExt/Cuff>。< /P>
毕竟,每当小程序尝试自己绘制时(包括初始化之后),您都将运行以下代码:

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

我觉得这个小程序的总体设计不合适。为什么您要使用
拉绳
作为基本标签

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


然后应该将
Integer.parseInt
调用放入try/catch块,捕捉
NumberFormatException
。(也可以考虑使用<代码>编号格式>代码>而不是<代码>整型。PARSETIN < /COD>,但稍后可以这样做……

主要问题是字符串不能正确绘制。有任何输入之前,小程序多次调用

paint
方法。因此,文本字段有空字符串。这是因为出现了
NumberFormatException
。检查文本中是否有空值。如果解析数字,则添加
try/catch
块。此外,将逻辑移动到使用
repaint
的方法
calculate
,并在输入时启动该方法。如果向文本字段添加一些侦听器,则可能会发生这种情况

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

主要的问题是琴弦不能正确地作画。有任何输入之前,小程序多次调用
paint
方法。因此,文本字段有空字符串。这是因为出现了
NumberFormatException
。检查文本中是否有空值。如果解析数字,则添加
try/catch
块。此外,将逻辑移动到使用
repaint
的方法
calculate
,并在输入时启动该方法。如果向文本字段添加一些侦听器,则可能会发生这种情况

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
  }
}
问题在于:

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

将无法解析
,并将
引发异常,如Jon Skeet所述

因此,修复可能是一个
try-catch
块(异常处理):

或者有一些初始可解析的
字符串

txt1 = new TextField("0"); 
txt2 = new TextField("0");
问题在于:

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

将无法解析
,并将
引发异常,如Jon Skeet所述

因此,修复可能是一个
try-catch
块(异常处理):

或者有一些初始可解析的
字符串

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

“小程序尝试绘制自身的任何时候”和“小程序尝试绘制自身的任何时候”以及“小程序尝试绘制自身的任何时候”和“小程序尝试绘制自身的任何时候”和“小程序尝试运行自身的任何事件:)调试小程序时,在中查看输出是至关重要的。不要在第三个千年使用AWT组件,使用Swing。不要在顶级容器中绘画,使用
画布
/
面板
JPanel
。除非必须将GUI塞进网页,否则不要编写小程序的代码-为框架编写/调试代码更容易。调试小程序时,查看页面中的输出非常重要。不要在第三个千年使用AWT组件,使用Swing。不要在顶级容器中绘画,使用
画布
/
面板
JPanel
。不要编写小程序,除非必须将GUI塞进网页中——为框架编写/调试代码更容易。