使用Swing
Timer和a
documentListener每次
document更新时,重置
Timer
看看如何使用Swing计时器和侦听文档中的更改以了解更多详细信息
作为一个简单的例子…
import java.awt.EventQueue;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import javax.swing.Jframe;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.Timer;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.event.documentEvent;import javax.swing.event.documentListener;public class Test { public static void main(String[] args) { new Test(); } public Test() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } Jframe frame = new Jframe("Testing"); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { JTextField field = new JTextField(20); JLabel label = new JLabel("Waiting"); DeferreddocumentListener listener = new DeferreddocumentListener(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Execute your required functionality here... label.setText(label.getText() + "."); } }, true); field.getdocument().adddocumentListener(listener); field.addFocusListener(new FocusListener() { @Override public void focusGained(FocusEvent e) { listener.start(); } @Override public void focusLost(FocusEvent e) { listener.stop(); } }); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; add(field, gbc); add(label, gbc); } } public class DeferreddocumentListener implements documentListener { private final Timer timer; public DeferreddocumentListener(int timeOut, ActionListener listener, boolean repeats) { timer = new Timer(timeOut, listener); timer.setRepeats(repeats); } public void start() { timer.start(); } public void stop() { timer.stop(); } @Override public void insertUpdate(documentEvent e) { timer.restart(); } @Override public void removeUpdate(documentEvent e) { timer.restart(); } @Override public void changedUpdate(documentEvent e) { timer.restart(); } }}


