2016-05-14 168 views
-3

我有一個名爲JTextArea和JButton的主JFrame。 當我按下按鈕時,線程會在屏幕後面執行某些操作並記錄日誌。 線程正在執行其工作時,日誌將更新爲JtextArea。 教程僅向我展示基本功能。所以我不知道該怎麼做。 感謝您的閱讀。多線程:如何讓jframe更新一個線程的日誌?

我的線程類:

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; 
     } 
    } 
} 

回答

0

我做了一個小例子,我希望這可以幫助您。

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); 
      } 
     } 
    } 

} 
+0

對不起,多線程的新手。但是在Rannable類中,如何調用textArea?沒有什麼可以連接textArea(JFrame)和線程。感謝您的幫助。 – Hanata

+0

無論如何感謝幫助我。我從你的例子中得到了很多想法。非常感謝。在myRunnable類的構造函數中,我將textArea傳入。非常感謝。 – Hanata

+0

請原諒我,因爲遲到的答案。 關於你的問題,也許有點困惑,但真的很簡單。正如你所看到的,這個類在另一個代表應用程序的類中,我的意思是它是本地的,這就是Runnable類可以看到它的成員容器類的原因。希望這個幫助 –