gpt4 book ai didi

java - keylistener 等待输入

转载 作者:行者123 更新时间:2023-11-29 03:02:33 24 4
gpt4 key购买 nike

我想使用 Swing 组件编写实时搜索。我正在使用 keyListener 来跟踪输入。基本上我不希望每次按下按钮时 keyListener 都采取行动,而是等待(一段时间)更多输入。每次按下按钮时都会刷新这段时间,并在最终超时时对输入进行评估(例如,在此期间没有按下任何按钮意味着输入已完成)。我如何在我的 keyListener 中实现它?

main方法的代码片段:

static JTextField nameTextField = new JTextField();

public static void main(String args[]) throws Exception {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(nameTextField, BorderLayout.NORTH);

nameTextField.addKeyListener(new KeyListener() {

public void keyTyped(KeyEvent keyEvent) {
//
}

@Override
public void keyPressed(KeyEvent e) {
//
}

@Override
public void keyReleased(KeyEvent e) {
if(waitForMoreInput(50)) {
doSomething(nameTextField.getText());
}
}
}
}
}
);


frame.setSize(250, 100);
frame.setVisible(true);
}

提前致谢

最佳答案

使用 DocumentListener 或 DocumentFilter 会更好,具体取决于您是想在文本完全注册到文本组件之前还是之后收听。

DocumentListener 会在文本发生更改时注册,无论是通过按键、通过复制和粘贴,还是通过删除文本。然后计时器将等待您希望对文本执行所需的任何操作。例如:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class DocListenerFoo extends JPanel {
private JTextField nameTextField = new JTextField(20);

public DocListenerFoo() {
add(new JLabel("Add Text:"));
add(nameTextField);

int timerDelay = 1000; // one second
nameTextField.getDocument().addDocumentListener(new MyDocListener(timerDelay));
}

private class MyDocListener implements DocumentListener {
private Timer docTimer;
private int timerDelay;

public MyDocListener(int timerDelay) {
this.timerDelay = timerDelay;
}

@Override
public void changedUpdate(DocumentEvent e) {
textChangedAction(e);
}

@Override
public void insertUpdate(DocumentEvent e) {
textChangedAction(e);
}

@Override
public void removeUpdate(DocumentEvent e) {
textChangedAction(e);
}

private void textChangedAction(DocumentEvent e) {
Document doc = e.getDocument();
try {
String text = doc.getText(0, doc.getLength());
if (docTimer != null && docTimer.isRunning()) {
docTimer.stop();
}

docTimer = new Timer(timerDelay, new TimerListener(text));
docTimer.setRepeats(false);
docTimer.start();
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}

}

private class TimerListener implements ActionListener {

private String text;

public TimerListener(String text) {
this.text = text;
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO do check on text here
System.out.println("Checking text here: " + text);
}

}

private static void createAndShowGui() {
DocListenerFoo mainPanel = new DocListenerFoo();

JFrame frame = new JFrame("DocListenerFoo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - keylistener 等待输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33942556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com