java中停止运行的函数_停止在Java中输入JTextField后如何启动函数

小编典典

使用Swing Timer和a DocumentListener每次Document更新时,重置Timer

作为一个简单的例子…

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();

}

}

}

2020-11-01

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值