使用的setText在AWT不起作用清除文本字段的内容 [英] Clearing contents of TextField using setText does not work in AWT

查看:221
本文介绍了使用的setText在AWT不起作用清除文本字段的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用的setText()方法AWT结算文本字段的内容存在问题。显然,的setText()不清除文本字段上$ P $的pssing的重置的内容按钮。下面是我的程序:

I am having problems clearing contents of TextField in AWT using setText() method. Apparently, setText("") does not clear the contents of the TextField on pressing the 'Reset' button. Here's my program:

import java.awt.*;
import java.awt.event.*;

public class form extends Frame
{

    Label lbl = new Label("Name:");
    TextField tf = new TextField();
    Button btn = new Button("Reset");

    public form()
    {
        tf.setColumns(20);

        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });


        btn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
               tf.setText("");  //Problem occurs here. This does not clear the contents of the text field on pressing the 'Reset' button.

            }
        });


        add(lbl);
        add(tf);
        add(btn);

        setLayout(new FlowLayout());
        setSize(400,100);
        setVisible(true);
        setTitle("Form");

    }


    public static void main(String[] args) 
    {
        new form();
    }

}

有人可以告诉我在哪里出了错或暗示的方法吗?谢谢你。

Can someone please tell me where I went wrong or suggest an alternative? Thanks.

推荐答案

我看到的问题以及使用Java 8u11。我似乎记得这个被提起的一个已知的错误,但我似乎现在不能找到它。

I see the problem as well using Java 8u11. I seem to remember this being filed as a known bug, but I can't seem to find it now.

这是对我工作的解决方案是增加一个中间步骤:

A solution that works for me is to add an intermediate step:

public void actionPerformed(ActionEvent e) {
   tf.setText(" ");  
   tf.setText("");
}

我不知道为什么这是必要的,我认为这是与的setText()函数专门无视空字符串的错误。如果有人发现归档的bug也就有更多的信息。

I'm not sure why this is necessary, I think it's a bug with the setText() function specifically ignoring empty Strings. If somebody finds the filed bug there would be more information there.

这篇关于使用的setText在AWT不起作用清除文本字段的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆