所以我在 GUI 中显示了一个 JTextField。我想要做的是当有人按下回车键时删除文本字段。我已经添加了 ActionListener。我只想知道如何自动更新它而不必最小化它或其他东西。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Window extends JFrame{
private JTextField TextField0;
private JTextField TextField1;
private JCheckBox CheckBox0;
//CONSTRUCTOR
public Window(){
super("Checkbox");
setLayout(new FlowLayout());
TextField0 = new JTextField("Add field",15);
add(TextField0);
CheckBox0 = new JCheckBox("");
HandlerClass handler = new HandlerClass();
TextField0.addActionListener(handler);
}
//Method: HandlerClass
public class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource()==TextField0){
CheckBox0.setText(String.format("%s",event.getActionCommand()));
}
}
}
}
编辑:我想通了伙计们!:D 谢谢!