简体   繁体   English

在执行除草时,在与main不同的其他线程中启动swing gui以更新jtextarea

[英]Start swing gui in anouther thread different from main to update jtextarea while doing culculations

My program starts at the moment 我的程序此刻开始

          public static void main(String[] args){
          new Gui().setVisible(true);
         new Program();   }

I am using Jtextarea to display results. 我正在使用Jtextarea显示结果。 I have method in Gui display(String). 我在Gui display(String)中有方法。 it appends text to Jtextarea. 它将文本追加到Jtextarea。 I call display(String ) from Program to display result. 我从程序调用display(String)来显示结果。 I have methods in Program to do some work and call that method to display at the end of execution the Program method. 我在Program中有方法来做一些工作,并调用该方法以在执行Program方法后显示。 But if I call display(String) at the beginning of execution it will not display in Jtextarea untill method of the Program finishes. 但是,如果我在执行开始时调用display(String),它将不会显示在Jtextarea中,直到程序的l方法完成。 It not updating the Gui. 它没有更新桂。 I think I need to use invoklater and new runnable to start EDT for Gui but how to do it in main together with new Program. 我想我需要使用invoklater和new runnable来启动Gui的EDT,但主要如何与新程序一起使用。 So what whould be the solution for this problem? 那么,该问题的解决方案是谁呢? Thank you for your help. 谢谢您的帮助。

PS I did Googled but can't get it how to do it. PS我做了谷歌搜索,但无法做到这一点。 So this is the last resource 所以这是最后的资源

A big thing that everyone seems to be overlooking: This is an actual quote from the swing documentation for JTextArea.append : 每个人似乎都忽略了一件大事:这是JTextArea.append的swing文档中的实际报价:

This method is thread safe, although most Swing methods are not. 尽管大多数Swing方法都不安全,但是此方法是线程安全的。 Please see How to Use Threads for more information. 请参阅如何使用线程以获取更多信息。

So it seems that SwingUtilities.invokelater should have nothing to do with this. 因此,似乎SwingUtilities.invokelater应该与此无关。 I really don't know what's causing your problem. 我真的不知道是什么引起了您的问题。 If you post some code, I might be able to help more. 如果您发布一些代码,我也许可以提供更多帮助。

P. S: I would have put this as a comment, but I do not have the reputation to do so. P. S:我本来可以将此作为评论,但我没有这样做的声誉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM