import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class TextFields extends JApplet {
    private JButton b1 = new JButton("Get Text"), b2 = new JButton("Set Text");

private JTextField t1 = new JTextField(30), t2 = new JTextField(30),
            t3 = new JTextField(30);

private String s = new String();

private UpperCaseDocument ucd = new UpperCaseDocument();
    Timer textTimer;
    TextTask task;

public void init() {
        t1.setDocument(ucd);
        ucd.addDocumentListener(new T1());
        b1.addActionListener(new B1());
        b2.addActionListener(new B2());
        DocumentListener dl = new T1();
        t1.addActionListener(new T1A());
        Container cp = getContentPane();
        cp.setLayout(new FlowLayout());
        cp.add(b1);
        cp.add(b2);
        cp.add(t1);
        cp.add(t2);
        cp.add(t3);

// 启动计时,用于拿1秒钟以内的文本变化
        textTimer = new Timer();
        task = new TextTask();
        task.textFields = this;
        textTimer.schedule(task, 1000, 2000);
        
    }

public void SetText(String text) {
        t3.setText(text);
        //task.cancel();
        //System.out.println("task.cancel()");
    }

public String GetText() {
        
        return t1.getText();
    }

class T1 implements DocumentListener {
        public void changedUpdate(DocumentEvent e) {
        }

long oldTimeMillis = 0;

public void insertUpdate(DocumentEvent e) {
            t2.setText(t1.getText());
            long newTimeMillis = System.currentTimeMillis();
            long timeDifference = newTimeMillis - oldTimeMillis;

if (oldTimeMillis != 0 && timeDifference > 500) {
                t3.setText( t1.getText());                             
            }

oldTimeMillis = newTimeMillis;
            
        }

public void removeUpdate(DocumentEvent e) {
            t2.setText(t1.getText());
            long newTimeMillis = System.currentTimeMillis();
            long timeDifference = newTimeMillis - oldTimeMillis;

if (oldTimeMillis != 0 && timeDifference > 800) {
                t3.setText(  t1.getText());
                
            }

oldTimeMillis = newTimeMillis;
            
        }

}

class T1A implements ActionListener {
        private int count = 0;

public void actionPerformed(ActionEvent e) {
            t3.setText("t1 Action Event " + count++);
        }
    }

class B1 implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (t1.getSelectedText() == null)
                s = t1.getText();
            else
                s = t1.getSelectedText();
            t1.setEditable(true);
        }
    }

class B2 implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            ucd.setUpperCase(false);
            t1.setText("Inserted by Button 2: " + s);
            ucd.setUpperCase(true);
            t1.setEditable(false);
        }
    }

public static void main(String[] args) {
        run(new TextFields(), 375, 325);
    }

public static void run(JApplet applet, int width, int height) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(applet);
        frame.setSize(width, height);
        applet.init();
        applet.start();
        frame.setVisible(true);

}

}

class UpperCaseDocument extends PlainDocument {
    private boolean upperCase = true;

public void setUpperCase(boolean flag) {
        upperCase = flag;
    }

public void insertString(int offset, String str, AttributeSet attSet)
            throws BadLocationException {
        if (upperCase)
            str = str.toUpperCase();
        super.insertString(offset, str, attSet);
    }

}

class TextTask extends TimerTask {

public String oldText = "";
    public TextFields textFields;

@Override
    public void run() {
        String newText = textFields.GetText();

if (oldText != "" && !newText.equals(oldText)) {
            textFields.SetText(newText);
            System.out.println(newText);
        }

oldText = newText;
    }

}

// /:~

JtextField的延时更新的更多相关文章

  1. 短时间内多个请求状态更新,导致react 不能及时响应问题总结

    个人总结 这段时间项目中遇到这样一个问题,旧项目中增加了一个聊天对话的模块,这是其他同学负责的部分,因为要有消息提醒,所以做了个轮询.消息提示因为是页头部分,所以每个模块都会引用到.这是背景. 现象 ...

  2. Redis系列(八)--缓存穿透、雪崩、更新策略

    1.缓存更新策略 1.LRU/LFU/FIFO算法剔除:例如maxmemory-policy 2.超时剔除,过期时间expire,对于一些用户可以容忍延时更新的数据,例如文章简介内容改了几个字 3.主 ...

  3. Count Colour_poj2777(线段树+位)

    POJ 2777 Count Color (线段树)   Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  4. vega prime 1.2 (视景仿真)

    Vega Prime 1.2 (视景仿真) MPI的视景仿真渲染工具Vega是世界上领先的应用于实时视景仿真.声音仿真和虚拟现实等领域的软件环境,它用来渲染战场仿真.娱乐.城市仿真.训练模拟器和计算可 ...

  5. 从一次异常中浅谈Hibernate的flush机制

    摘自http://www.niwozhi.net/demo_c70_i1482.html http://blog.itpub.net/1586/viewspace-829613/ 这是在一次事务提交时 ...

  6. Handoff使用指南 - 理论篇

    Handoff简介 Handoff是iOS 8 和 OS X v10.10中引入的功能,可以让同一个用户在多台设备间传递项目.In iOS 9 and OS X v10.11 支持了Spotlight ...

  7. mysql颠覆实战笔记(四)--商品系统设计(一):商品主表设计

    版权声明:笔记整理者亡命小卒热爱自由,崇尚分享.但是本笔记源自www.jtthink.com(程序员在囧途)沈逸老师的<web级mysql颠覆实战课程 >.如需转载请尊重老师劳动,保留沈逸 ...

  8. POJ 2777(线段树)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42507   Accepted: 12856 Des ...

  9. 强化学习(九)Deep Q-Learning进阶之Nature DQN

    在强化学习(八)价值函数的近似表示与Deep Q-Learning中,我们讲到了Deep Q-Learning(NIPS 2013)的算法和代码,在这个算法基础上,有很多Deep Q-Learning ...

随机推荐

  1. bzoj 3270 博物馆(高斯消元)

    [题意] 两人起始在s,t点,一人pi概率选择留在i点或等概率移动,问两人在每个房间相遇的概率. [思路] 把两个合并为一个状态,(a,b)表示两人所处的状态,设f[i]为两人处于i状态的概率.则有转 ...

  2. 实例化spring容器

    方法一:在类路径下寻找配置来实例化容器 ApplicationContext ctx = new String[]{"beans.xml"}); 方法二:在文件系统路径下寻找配置文 ...

  3. Python面向对象3

    一.内部类 内部类就是在类的内部定义的类,主要目的是为了更好的抽象现实世界. 二.魔术方法(构造函数和析构函数) #!usr/bin/python #coding:utf8 class Milo(): ...

  4. SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 SpringMVC 的 ...

  5. cocos2d-x 知识小结(1)zorder和tag

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  6. Axis2与Web项目整合

    一.说明: 上一篇介绍了通过使用Axis2来发布和调用WebService,但是是把WebService发布在Axis2提供的项目中,如果我们需要在自己的Web项目中来使用Axis2发布WebServ ...

  7. 简易的JQuery设置Cookie

    使用之前先引用这两个文件: 然后基本的功能代码如下: <div> <input id="txtDelValues" type="text" / ...

  8. CodeForces 711B Chris and Magic Square (暴力,水题)

    题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...

  9. springMVC+JAP整合彻底摆脱persistence.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  10. Windows x86 x64使用SetThreadContext注入shellcode的方式加载DLL

    一.前言 注入DLL的方式有很多,在R3就有远程线程CreateRemoteThread.SetWindowsHookEx.QueueUserApc.SetThreadContext 在R0可以使用a ...