java 重置文本区域内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15908663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:15:41  来源:igfitidea点击:

reset the text area contents

javaapplettextareatextfield

提问by praveena

I have this code.. here when i input number "6" in the textfield, text should be displayed in the textarea..but after that if i input any other number i want the textarea contents to be clear. But when I execute my code, the old contents of the textarea remain even when i input a different number. Please help!

我有这个代码..在这里,当我在文本字段中输入数字“6”时,文本应显示在文本区域中..但之后如果我输入任何其他数字,我希望文本区域内容清晰。但是当我执行我的代码时,即使我输入了不同的数字,textarea 的旧内容仍然存在。请帮忙!

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="front" width=500 height=500></applet> */
public class front extends Applet implements ActionListener {
  String msg="";
  TextArea text,text1;
  TextField txt;
  Button load, enter;

  public void init() {
    enter=new Button("Enter");
    load=new Button("Load");
    txt=new TextField(5);
    text=new TextArea(10,15);

    add(load);
    add(text);

    add(txt);
    add(enter);

    load.addActionListener(this);
    txt.addActionListener(this);
    enter.addActionListener(this);
  }

  public void actionPerformed(ActionEvent ae)
  {
    String str = ae.getActionCommand();
    if(str.equals("Load")) {
      msg = "You pressed Load";
    } else {
      if(txt.getText().toString().equals ("6")) {
        msg="Set the text for 6";
        text.setText("Text");
      } else {
        msg="Invalid number";
        text.setText("");
      }
    }
    repaint();
  }

  public void paint(Graphics g) {
    g.drawString(msg,350,250);
  }
}

回答by Nomesh Gajare

write your actionPerformed() method as follows

编写您的 actionPerformed() 方法如下

    public void actionPerformed(ActionEvent ae)
  {
    String str = ae.getActionCommand();
    if(str.equals("Load")) {
      msg = "You pressed Load";
    } else {
      if(txt.getText().toString().equals ("6")) 
         {
        **text.setText("");**
        msg="Set the text for 6";
        text.setText("Text");
         } 
         else {
        msg="Invalid number";
        text.setText("");
      }
    }
    repaint();
  }

the mistake was that you were not clearing the text field after writing to it! now it is cleared by using text.setText("");in ifcondition

错误是您在写入后没有清除文本字段!现在通过text.setText("");if条件下使用它被清除

hope this solves your problem!

希望这能解决您的问题!

回答by Vishal K

You should call super.paint(g)within paint(Graphics g)method:

您应该super.paint(g)paint(Graphics g)方法内调用:

public void paint(Graphics g) {
    super.paint(g);
    g.drawString(msg,350,250);
  }

回答by Sukhbir

Now, text.setText("");will not do anything and it will same as //text.setText("");

现在,text.setText("");不会做任何事情,它会和//text.setText("");

So Better Approach is to take help of ASCII code,

所以更好的方法是借助 ASCII 码,

For Null Character ASCII value is 0, in unicode we can write it as '\u0000'

对于空字符 ASCII 值为0,在 unicode 中我们可以将其写为'\u0000'

And Finally this Statement will surely work: text.setText(""+'\u0000');

最后这个声明肯定会奏效: text.setText(""+'\u0000');

Note: their is no method is textArea class to clear the area... So you have to do this on your own.

注意:他们没有方法是 textArea 类来清除区域......所以你必须自己做这件事。