小编Sou*_*ish的帖子

使用setText清除TextField的内容在AWT中不起作用

TextField在使用setText()方法清除AWT中的内容时遇到问题.显然,setText("")没有清除TextField按下"重置"按钮的内容.这是我的计划:

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); …
Run Code Online (Sandbox Code Playgroud)

java awt

3
推荐指数
1
解决办法
4678
查看次数

标签 统计

awt ×1

java ×1