Java 多线程:如何让jframe更新线程的日志?

Java 多线程:如何让jframe更新线程的日志?,java,multithreading,multiprocessing,Java,Multithreading,Multiprocessing,我有一个名为JTextArea和JButton的主JFrame。 当我按下按钮时,一个线程会在屏幕后面做一些事情并记录日志。 当线程执行其工作时,日志将更新为JtextArea。 教程只向我展示了基本知识。所以我不知道该怎么做。 谢谢你的阅读 我的线程类: public class myThread implements Runnable{ private Thread t; public String getThreadName() { return ThreadName;

我有一个名为JTextArea和JButton的主JFrame。 当我按下按钮时,一个线程会在屏幕后面做一些事情并记录日志。 当线程执行其工作时,日志将更新为JtextArea。 教程只向我展示了基本知识。所以我不知道该怎么做。 谢谢你的阅读

我的线程类:

public class myThread  implements Runnable{
    private Thread t;

public String getThreadName() {
    return ThreadName;
}

public void setThreadName(String ThreadName) {
    this.ThreadName = ThreadName;
}

public void setIsDone(boolean isRunning) {
    this.isDone = isDone;
}

public boolean getIsDone() {
   return this.isDone;
}

private String Log;

public String getLog() {
    return Log;
}

public void setLog(String Log) {
    this.Log = Log;
}

private String ThreadName;  

public boolean isDone=false;

public myThread(String strThreadName)
{
    this.ThreadName=strThreadName;
    this.isDone=false;
}

@Override
public void run() {

    creatingFolerCreating(); 
}

private void  createingFolerCreating()
{
    String strResultFolder=this.Path+"\\"+"Result";
    this.strAFolder=strResultFolder+"\\"+"A";
    this.strBFolder=strResultFolder+"\\"+"B";
    boolean s=false;
    s=(new File(strResultFolder)).mkdir();if(!s)this.Log+="result Foleder is existed";
    s =(new File(strAFolder)).mkdir();if(!s)this.Log+="A Foleder is existed";
    s =(new File(strBFolder)).mkdir();if(!s)this.Log+="B Foleder is existed";      
} 


 public void start()
   {
       if(t==null)
       {
           t=new Thread(this,this.ThreadName);
           t.start();
           this.isDone=true;
       }
   }
}

我举了一个小例子,希望这对你有所帮助

public class Test {

    private JFrame frame;
    JTextArea textArea;
    Thread t, t1;
    MyRunnable runner, runner1;

    public static void main(String[] args) {
        Test test = new Test();
    }

    public Test() {
        frame = new JFrame("Test");
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent event) {
                frame.setVisible(false);
                runner.stop(); runner1.stop();
                t.interrupt();t1.interrupt();
                frame.removeWindowListener(this);
                frame.dispose();
                frame = null;
            }
        });
        Container pane = frame.getContentPane();
        pane.setLayout(new FlowLayout());
        textArea = new JTextArea();
        textArea.setName("textArea1");
        pane.add(textArea);
        frame.setSize(400, 300);
        frame.setVisible(true);

        startTask();
    }

    void startTask() {
        runner = new MyRunnable("Label Text 1", 2000L);
        t = new Thread(runner);
        t.start();
        runner1 = new MyRunnable("Label Text 2", 3000L);
        t1 = new Thread(runner1);
        t1.start();
    }

    class MyRunnable implements Runnable {

        private String name;
        private Long waitTime;
        private Boolean active;

        public MyRunnable(String tn, Long time) {
            name = tn;
            waitTime = time;
            active = Boolean.TRUE;
        }

        public void stop(){
            active = Boolean.FALSE;
        }

        public void run() {
            while (active) {
                try {
                    Thread.sleep(waitTime);
                } catch (InterruptedException ex) {
                }
                textArea.append(name + "\n");
                System.out.println(name);
            }
        }
    }

}

抱歉,我是多线程的新手。但在Ranable类中,如何调用textArea???没有任何东西可以连接textArea(JFrame)和线程。谢谢你帮助我。无论如何,谢谢你帮助我。我从你的例子中得到了很多想法。非常感谢。在myRunnable类的构造函数中,我将textArea传递到。非常感谢。请原谅我迟交的答复。关于你的问题,也许有点困惑,但其实很简单。正如您所看到的,该类位于表示应用程序的另一个类中,我的意思是它是容器类的本地类,这就是为什么Runnable类可以看到它容器类的成员。希望这有帮助