JButton ActionListener - 仅在单击JButton之后才更新GUI [英] JButton ActionListener - GUI updates only after JButton is clicked

查看:103
本文介绍了JButton ActionListener - 仅在单击JButton之后才更新GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JButton ActionListener出现问题。我有一个在另一个类中定义的doTheCleaning()方法,在调用时会对我的GUI进行一系列更改。

I'm having a problem with my JButton ActionListener. I have a doTheCleaning() method defined in another class which when called makes series of changes to my GUI.

public void doTheCleaning(){
    //change image icon
    //had thread.sleep here
    //insert to text area
    //had thread.sleep here
    //etc
}

然后在另一个类中,我实例化了包含我的doTheCleaning()方法的类,并使用我的actionperformed()方法编写了ActionListener,用于我的jbutton,如下所示:

Then in another class, I instantiated the class containing my doTheCleaning() method and had my ActionListener written with my actionperformed() method for my jbutton written like this:

public void actionPerformed(ActionEvent e){
   //some code
   //newClass.doTheCleaning();
}

我知道如何做其余的事情,比如addActionListener()和东西所以不需要对此提出质疑。我担心的是,调用doTheCleaning()方法时执行的GUI中的所有更改仅在单击按钮后才会应用。发生这种情况时,我的标签和textarea中发生的更改之间的连续性未显示。如果我直接在我的测试器类中调用它,但在actionperformed方法中调用它只显示我的GUI的最终状态,代码工作正常。我需要先显示哪个元素已更改,然后是下一个元素,依此类推。

I know how to do the rest like addActionListener() and stuff so no need to question about that. My concern is that all the changes in my GUI that is performed when doTheCleaning() method is called applies only after the button is clicked. When this happens, the succession between the changes that happened in my labels and textarea were not shown. The code works fine if i called it directly in my tester class but calling it inside the actionperformed method shows only the final state of my GUI. I need to show which element changed first, then what's next, and so on.

当我需要在单击JButton时才需要进行这些更改时,我怎么能实现它?

How could I achieve it when I need these changes to occur only when I click the JButton?

* *我在java中使用GUI还不是很好。如果没有我给我的代码,你们明白了我的观点。但如果有必要,我可以。谢谢。

**I'm not so good with doing GUI in java yet. iIhope you guys understood my point without me giving my code. but I could if necessary. Thanks.

推荐答案

不要在 EDT ,否则GUI将无响应,您可能看不到GUI更新。您可以使用的最佳选择是 SwingWorker

Do not perform any intensive operations within EDT, otherwise the GUI will be unresponsive and you might not see the GUI updates. Best choice you can use is SwingWorker:


  • 覆盖 doInBackground() ,并在此方法中放置任何长操作,以便它将在一个单独的线程上运行,而不是 EDT

对于任何GUI创建或更改 doInBackground() ,使用 publish(V ... chunk) 将数据发送到 process(List< V>组块) 。您需要覆盖 进程(列出< V>块) 。另请注意 进程(列表< V>块) EDT

For any GUI creation or changing states of GUI components within doInBackground(), use publish(V... chunks) to send data to process(List<V> chunks). You need to override process(List<V> chunks). Also note that process(List<V> chunks) is executed on EDT.

doInBackground() 返回, done() EDT ,你可以覆盖它以用于任何GUI更新。您还可以检索从 http://docs.oracle.com/javase/6/docs/api/javax/swing /SwingWorker.html#get%28%29\"rel =nofollow noreferrer> get()

After doInBackground() returns, done() executes on EDT and you can override it to use it for any GUI updates. You can also retrieve the value returned from doInBackground() by using get().

请注意 SwingWorker< T,V> 通用,您需要指定类型。 T 是从 doInBackground() get() ,而 V 是您传递给 进程(列出< V> chunk) 通过 发布(V ...块)

Note that SwingWorker<T,V> is generic, and you need to specify the types. T is the type of object returned from doInBackground() and get(), while V is the type of elements you passed to process(List<V> chunks) via publish(V... chunks).

execute() 方法通过调用 doInBackground() 首先。

有关详情,请阅读 Swing中的并发

这篇关于JButton ActionListener - 仅在单击JButton之后才更新GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆