gpt4 book ai didi

java - 重置文本区域内容

转载 作者:行者123 更新时间:2023-11-30 09:23:49 25 4
gpt4 key购买 nike

我有这段代码..在这里当我在文本字段中输入数字“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);
}
}

最佳答案

按如下方式编写您的 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();
}

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

希望这能解决您的问题!

关于java - 重置文本区域内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15908663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com