如果在java中单击按钮时如何删除textfields值,因为textfield.setText(“”);不起作用。
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==ok){
String temp=textField.getText();
textField.setText("hello "+ temp);
}
if(arg0.getSource()==cancel)
{
textField.setText(null);
}
}
答案 0 :(得分:1)
如果您在谈论班级JTextField
,则需要使用正确的方法setText("")
来删除文本字段中的文字。
也许你在与按钮链接的动作监听器中的问题,试试这个:
JButton button = new JButton("Clear TextField");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textfield.setText("");
//textfield.setText(null); // or try this
}
});