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;
}
}
// /:~