Java 代码不是按键入的顺序执行的

Java 代码不是按键入的顺序执行的,java,multithreading,Java,Multithreading,在Thread.sleep(2000)出现之前,我很难让这段代码显示在GUI文本区域(输入)上 我需要文本区域显示“挂起”,然后执行GUI锁定/睡眠2秒您不应该直接调用运行方法,因为它不是以这种方式在单独的线程中执行的。 要启动线程,请使用thread.start(r)其中r是您的Runnable 您可以尝试一下,但这也可能不起作用,因为Swing需要特殊的并发管理。 看看r是一个可运行的对象而不是线程 Runnable r = new Runnable() { publ

在Thread.sleep(2000)出现之前,我很难让这段代码显示在GUI文本区域(输入)上


我需要文本区域显示“挂起”,然后执行GUI锁定/睡眠2秒

您不应该直接调用
运行
方法,因为它不是以这种方式在单独的线程中执行的。 要启动线程,请使用
thread.start(r)
其中
r
是您的
Runnable

您可以尝试一下,但这也可能不起作用,因为Swing需要特殊的并发管理。
看看

r是一个可运行的对象而不是线程
       Runnable r = new Runnable() {

    public void run() {
        try{
            Thread.sleep(2000);
            action = input.getText();
            action = erase(action);
            input.setText(action);
        }
        catch(InterruptedException e){
            System.out.println("Thread Interrupted" + e);
        }
    }
};

        if(e.getSource() == call){
        if(!calling){
            String temp = input.getText();
            action = called(temp);
            input.setText(temp + "\n" + action);
            System.out.println(""+e.getActionCommand());
            calling = true;
        } else{
            calling = false;//executes
            String temp = input.getText();//does not execute until AFTER r.run();
            input.setText(temp + "\n" + "Hanging Up...");//does not execute until AFTER r.run();
            r.run();
          }