我试图在一个textarea中输入的每个字母之间添加2秒的延迟,然后再打印/显示在另一个textarea上。
我使用过两个课程:
除了执行线程外,一切正常。
这里是GUI的代码:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class PrintDelay extends javax.swing.JFrame implements KeyListener{
int name;
JTextArea server = new JTextArea (10, 20);
JTextArea client = new JTextArea (10, 20);
public PrintDelay () {
super ("Typing...");
setSize (650, 220);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel ();
server.addKeyListener (this);
panel.add (server);
panel.add (client);
add(panel);
setVisible (true);
}
public static void main (String... args) {
new PrintDelay ();
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
new TwoSecondDelay (Integer.toString(name));
name++;
client.setText(server.getText());
}
}
这里是TwoSecondDelay的代码:
public class TwoSecondDelay implements Runnable{
String name;
Thread t;
TwoSecondDelay (String threadname) {
name = threadname;
t = new Thread (this, name);
t.start();
}
@Override
public void run() {
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
}
}
}
这些类也在各自的文件中。
答案 0 :(得分:3)
无需使用TwoSecondDelay
@NgModule