如何从长方法内部更新Swing GUI? [英] How to update swing GUI from inside a long method?

查看:74
本文介绍了如何从长方法内部更新Swing GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swing的新手,目前正尝试在NetBeans中开发一个简单的GUI应用程序.

I'm new to Swing, and currently trying to develop a simple GUI application in NetBeans.

我想创建某种GUI日志记录系统,以将应用程序正在执行的当前操作写入TextArea.

I want to create some kind of a GUI logging system, to write the current action that's being performed by the application, into a TextArea.

作为一个简单的示例,我创建了一个JFrame表单,其中仅包含2个对象:开始"按钮和TextArea.

As a simple example, I created a JFrame form which contains only 2 objects: a "Start" button and a TextArea.

当按下开始"按钮时,它会调用某种冗长的方法,该方法需要花费一些时间(例如10秒)才能完成运行,并且在此方法运行时,我想将文本追加到TextArea使用这种冗长的方法(当然,我希望立即更新TextArea).

When the "Start" button is pressed, it invokes some kind of a lengthy method that should take some time (say, 10 seconds) to complete running, and while this method is running, I want to append text to the TextArea from withing this lengthy method (and of course I want the TextArea to be immediately updated).

我的问题是我找不到正确的方法.无论如何,我尝试执行此操作时,当我按下开始"按钮时,应用程序冻结了10秒钟,而没有按我的意愿更新TextArea.只有方法完成后,我才能看到TextArea的更新.

My problem is that I cannot find the proper way of doing that. Anyway I tried doing this, when I press on the "Start" button, the application freezes for 10 seconds, without updating the TextArea like I wanted. Only when the method finishes, I see the update to the TextArea.

这是示例代码:

private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
    try {
        for (int i = 0; i < 10; i++) {
           textArea.setText(i + "\n");
            Thread.sleep(1000);
        }
    } catch (Exception e) {}
}

在此示例中,我希望看到的是,一旦单击按钮,在接下来的10秒钟内,每秒就会在TextArea上添加一行新行,如下所示:

In this example what I expect to see is that once I click the button, for the next 10 seconds, a new line would be appended to the TextArea every second, like so:

1

2

3

4

...

但是我从这段代码中得到的真正结果是应用程序冻结了10秒钟,最后TextArea被更新并仅显示数字9.

But the real result that I get from this code is that the application freezes for 10 seconds, and finally the TextArea is updated and displays only the digit 9.

我尝试了许多不同的方法来做到这一点,主要是使用 SwingUtilities.invokeLater SwingWorker 方法,但没有一个对我有用.

I've tried many different methods of doing this right, mainly using the SwingUtilities.invokeLater and SwingWorker methods, but none of them worked for me.

对于找到正确方法的任何帮助,将不胜感激.

Any help with finding the right way of doing that would be greatly appreciated.

推荐答案

您将要使用SwingWorker-我建议您仔细阅读它,因为它对有.此实现应为您提供示例中所需的内容:

Like stated in other answers, you're going to want to use SwingWorker - I recommend reading up on it, as it is an incredibly useful tool to have. This implementation should give you what you're looking for in your sample:

您按钮的操作:

button.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
        new Worker().execute();
    }
});

您的摇摆工人:

public class Worker extends SwingWorker<String, String>{

    @Override
    protected String doInBackground() throws Exception {
        //This is what's called in the .execute method
        for(int i = 0; i < 10; i++){
            //This sends the results to the .process method
            publish(String.valueOf(i));
            Thread.sleep(1000);
        }
        return null;
    }

    protected void process(List<String> item) {
        //This updates the UI
        textArea.append(item + "\n");
    }
}

这篇关于如何从长方法内部更新Swing GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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