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

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

问题描述

我的 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() 方法为我的 jbutton 编写了我的 ActionListener,如下所示:

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.

对于 doInBackground(),使用publish(V... chunks) 将数据发送到process(列出块).您需要覆盖 process(List chunks).另请注意 process(List chunks)EDT.

doInBackground() 返回,done()EDT 并且您可以覆盖它以将其用于任何 GUI 更新.您还可以检索从 doInBackground() 使用 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 是你传递给的元素类型process(List chunks) 通过 publish(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 中的并发.

For more on this, please read Concurrency in Swing.

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

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