【发布时间】:2014-08-18 17:52:03
【问题描述】:
我在使用 setText() 方法在 AWT 中清除 TextField 的内容时遇到问题。显然,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);
add(btn);
setLayout(new FlowLayout());
setSize(400,100);
setVisible(true);
setTitle("Form");
}
public static void main(String[] args)
{
new form();
}
}
有人可以告诉我哪里出错或提出替代方案吗?谢谢。
【问题讨论】:
-
对我有用,但除外。键入内容单击重置,该字段为空。
-
我也看到了问题。您使用的是什么版本的 Java?我似乎记得看到这是一个已知的错误,让我看看我能不能找到...
-
我同时使用 Java 7 update 65 和 Java 8 update 11,但问题似乎都存在
-
为什么使用 AWT 组件而不是 Swing?请参阅 this answer 了解放弃 AWT 的许多充分理由。
-
@AndrewThompson,感谢您的链接。我应该仅将 AWT 用于创建表单应用程序作为我的要求以及组件。