Java Applet 编译得很好,但无法运行

问题描述 投票:0回答:3

这个 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
   }
}
java applet
3个回答
1
投票

我强烈怀疑正在抛出一个

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

我感觉这个小程序的整体设计不太合适。为什么要使用

drawString
来表示本质上是标签的内容?

我会添加一两个

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

然后您应该将

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


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 所解释的那样。

因此 fix 可能是一个

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");

0
投票

主要问题是琴弦无法正确绘制。在您进行任何输入之前,小程序会多次调用

paint
方法。因此,文本字段有空字符串。这是因为
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
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.