gpt4 book ai didi

java - 带有 Thread.sleep 的 DocumentListener

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:50:01 26 4
gpt4 key购买 nike

我想知道我什么时候在写,什么时候在删除,但是,在 0.5 秒的延迟之后,它会告诉我“你停止写/删除”但是,它只显示该消息,并且在删除或删除后半秒延迟。

如何正确使用 Thread.sleep(500);

我当前的源代码:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;


public class TextChangedFrame extends JFrame {

JTextField textField = new JTextField("Put your text here");
JLabel label = new JLabel("You have written: ");

public TextChangedFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 100);
setLayout(new BorderLayout());
getContentPane().add(textField, BorderLayout.CENTER);
getContentPane().add(label, BorderLayout.SOUTH);
textField.getDocument().addDocumentListener(new DocumentListener() {

public void insertUpdate(DocumentEvent e) {
label.setText("I'm writting: " + textField.getText());
try {
Thread.sleep(500);
} catch (InterruptedException ex) {

}
label.setText("I stopped writing");
}

public void removeUpdate(DocumentEvent e) {
label.setText("I'm deleting");
try {
Thread.sleep(500);
} catch (InterruptedException ex) {

}
label.setText("I stopped deleting");
}

public void changedUpdate(DocumentEvent e) {
}
});
}

public static void main(String[] args) {
TextChangedFrame frame = new TextChangedFrame();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

最佳答案

同样,使用 Swing Timer 来完成脏活。您所做的是每当您编辑或删除时,调用定时器上的重新启动以重新设置定时器并启动它。如果 Timer 正在运行,restart() 方法将停止它。

     public void insertUpdate(DocumentEvent e) {
label.setText(EDITING);
writeDeleteTimer.restart();
}

例如:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

@SuppressWarnings("serial")
public class TextChangedFrame extends JPanel {

public static final String STOPPED_EDITING = "No Longer Editing or Deleting";
private static final String EDITING = "Editing";
private static final String DELETING = "Deleting";
private static final int TIMER_DELAY = 500;
private static final int PREF_W = 400;
private static final int PREF_H = 100;
private JTextField textField = new JTextField("Put your text here");
private JLabel label = new JLabel("You have written: ");
private ActionListener timerListener = new TimerListener();
private Timer writeDeleteTimer = new Timer(TIMER_DELAY, timerListener);

public TextChangedFrame() {
setLayout(new BorderLayout());
add(textField, BorderLayout.CENTER);
add(label, BorderLayout.SOUTH);
textField.getDocument().addDocumentListener(new DocumentListener() {

public void insertUpdate(DocumentEvent e) {
label.setText(EDITING);
writeDeleteTimer.restart();
}

public void removeUpdate(DocumentEvent e) {
label.setText(DELETING);
writeDeleteTimer.restart();
}

public void changedUpdate(DocumentEvent e) {
}
});
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent evt) {
label.setText(STOPPED_EDITING);
Timer timer = (Timer) evt.getSource();
timer.stop();
}
}

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

JFrame frame = new JFrame("TextChangedFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_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();
}
});
}
}

答案已编辑:无需重新创建 Timer 对象。只需对其调用 restart(),因为如果当前 Timer 正在运行,它将停止它。

关于java - 带有 Thread.sleep 的 DocumentListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16362716/

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