使组件冻结,直到一个组件完成其工作 [英] Swing components freezing until one component completes its job

查看:174
本文介绍了使组件冻结,直到一个组件完成其工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的JAVA Swing程序,它有一个JTextArea,三个JTextField和一个JButton。这个应用程序做的是当用户点击按钮时,它用文本行更新JTextArea,插入到JTextArea的文本行在for循环中准备,并且在JTextField中给出重复次数。



我的问题是当我点击启动JButton应用程序的所有组件冻结,我甚至不能关闭窗口,直到for循环完成它的工作。如何将此JTextField更新工作与表单中的其他任务分开?

解决方案

你可能在事件分派线程上做同样的工作。使用 SwingWorker $ b
$ b

下面的代码生成此截图:



 

static class MyWorker extends SwingWorker< String,String> {

private final JTextArea area;

MyWorker(JTextArea area){
this.area = area;
}

@Override
public String doInBackground(){
for(int i = 0; i <100; i ++){
try { Thread.sleep(10); } catch(InterruptedException e){}
publish(Processing ...+ i);
}
returnDone;
}
@Override
protected void process(List< String> chunks){
for(String c:chunks)area.insert(c +\\\
,0 );
}
@Override
protected void done(){
try {
area.insert(get()+\\\
,0);
} catch(Exception e){
e.printStackTrace();
}
}
}

示例 main

  public static void main )throws Exception {

final JTextArea area = new JTextArea();

JFrame frame = new JFrame(Test);

frame.add(new JButton(new AbstractAction(Execute){
@Override
public void actionPerformed(ActionEvent e){
new MyWorker .execute();
}
}),BorderLayout.NORTH);

frame.add(area,BorderLayout.CENTER);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,300);
frame.setVisible(true);
}


I have created a simple JAVA Swing program that has a JTextArea, three JTextFields and one JButton. What this application does is when the user clicks the button it updates the JTextArea with a text line, the text line inserted into the JTextArea is prepared in a for loop and number of repeat times is given in a JTextField.

My problem is when I click the start JButton all the components of the application are freeze, I can't even close the window until the for loop completes it's job. How can I separate this JTextField updating work from other tasks in the form?

解决方案

You are probably doing the work on the Event Dispatch Thread (the same thread as the GUI rendering is done). Use SwingWorker it will do the work in another thread instead.


Example

Code below produces this screenshot:

Example worker:

static class MyWorker extends SwingWorker<String, String> {

    private final JTextArea area;

    MyWorker(JTextArea area) {
        this.area = area;
    }

    @Override
    public String doInBackground() {
        for (int i = 0; i < 100; i++) {
            try { Thread.sleep(10); } catch (InterruptedException e) {}
            publish("Processing... " + i);
        }
        return "Done";
    }
    @Override
    protected void process(List<String> chunks) {
        for (String c : chunks) area.insert(c + "\n", 0);
    }
    @Override
    protected void done() {
        try {
            area.insert(get() + "\n", 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Example main:

public static void main(String[] args) throws Exception {

    final JTextArea area = new JTextArea();

    JFrame frame = new JFrame("Test");

    frame.add(new JButton(new AbstractAction("Execute") {            
        @Override
        public void actionPerformed(ActionEvent e) {
            new MyWorker(area).execute();
        }
    }), BorderLayout.NORTH);

    frame.add(area, BorderLayout.CENTER);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}

这篇关于使组件冻结,直到一个组件完成其工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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