Java SWT应用程序近实时显示更新的显示

Java SWT应用程序近实时显示更新的显示,java,swt,Java,Swt,问题: 您能给出SWT显示部件(更新窗口/外壳)的任何简单示例或说明吗?或者你能找到任何你认为最适合SWT应用程序开发的网站吗 背景: 我是SWT应用程序的新手,目前正在构建一个用于运行一些测试的应用程序 它有一个带有文本区域的主显示shell类,在用户单击run按钮后,文本区域会不断更新 run按钮启动另一个线程进程,该进程更新StartView类中的公共静态对象,如AtomicCounter 当前阶段 public class UnitTest { public static Di

问题:

您能给出SWT显示部件(更新窗口/外壳)的任何简单示例或说明吗?或者你能找到任何你认为最适合SWT应用程序开发的网站吗

背景:

我是SWT应用程序的新手,目前正在构建一个用于运行一些测试的应用程序

它有一个带有文本区域的主显示shell类,在用户单击run按钮后,文本区域会不断更新

run按钮启动另一个线程进程,该进程更新StartView类中的公共静态对象,如AtomicCounter

当前阶段

public class UnitTest {

    public static Display display;
    private Shell shell;
    public static AtomicInteger counter = new AtomicInteger(0);
    public static Text text;
    private TestThread test1 = null, test2 = null;

    public UnitTest()
    {
        display = Display.getDefault();
        this.shell = new Shell(display, SWT.CLOSE);
        this.shell.setSize(226, 120);

        text = new Text(shell, SWT.BORDER);
        text.setBounds(10, 10, 199, 19);

        Button btnStart = new Button(shell, SWT.NONE);
        btnStart.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent arg0) {
                test1 = new TestThread();
                test1.start();
                test2 = new TestThread();
                test2.start();
            }
        });
        btnStart.setBounds(10, 54, 94, 28);
        btnStart.setText("Start");

        Button btnStop = new Button(shell, SWT.NONE);
        btnStop.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent arg0) {
                test1.interrupt();
                test2.interrupt();
                counter.set(0);
            }
        });
        btnStop.setBounds(115, 54, 94, 28);
        btnStop.setText("Stop");

        this.shell.open();
        this.shell.layout();

        this.shell.addListener(SWT.Close, new Listener(){
            public void handleEvent(Event event)
            {
                shell.dispose();
            }
        });

        while(!this.shell.isDisposed())
        {
            if(!display.readAndDispatch())
            {
                //text.setText(""+counter.get());
                display.sleep();
            }
        }
    }

    public static void main(String[] args)
    {
        new UnitTest();
    }
}

class TestThread extends Thread
{   
    public void run()
    {
        try
        {
            int i = 0;
            while(i++ < 1000 && !this.isInterrupted() )
            {
                UnitTest.counter.getAndIncrement();
                try {
                    TestThread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    break;
                }

                if(UnitTest.display.isDisposed())
                    return;

                UnitTest.display.asyncExec(new Runnable() {
                    public void run() {
                      if (UnitTest.text.isDisposed())
                        return;
                      UnitTest.text.setText(""+UnitTest.counter.get());
                    }
                  });

            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            System.out.println("Existing thread...");
        }

    }
}
该程序似乎运行良好,但它不会实时更新文本区域。 嗯,我不能说是实时的,但它显示了一点延迟的信息。(我可以说它延迟了,因为我也在控制台上打印出来)

似乎我不太理解SWT的概念,所以我不想用它做任何事情

目标

A.无论B是否运行,启动和停止C的主显示类

B.线程化进程,该进程使用类的公共静态对象更新类的文本区域

C.执行其工作并更新

示例代码(工作代码)

公共类单元测试{
公共静态显示;
私人壳牌;
公共静态AtomicInteger计数器=新的AtomicInteger(0);
公共静态文本;
私有测试线程test1=null,test2=null;
公共单元测试()
{
display=display.getDefault();
this.shell=新shell(显示,SWT.CLOSE);
此.shell.setSize(226120);
text=新文本(shell、SWT.BORDER);
文本.背景(10,10,199,19);
按钮btnStart=新按钮(外壳,SWT.NONE);
btnStart.addSelectionListener(新的SelectionAdapter(){
@凌驾
公共无效widgetSelected(SelectionEvent arg0){
test1=新的TestThread();
test1.start();
test2=新的TestThread();
test2.start();
}
});
b起始立根(10,54,94,28);
btnStart.setText(“开始”);
按钮btnStop=新按钮(外壳,SWT.NONE);
btnStop.addSelectionListener(新的SelectionAdapter(){
@凌驾
公共无效widgetSelected(SelectionEvent arg0){
test1.中断();
test2.interrupt();
计数器设置(0);
}
});
btnStop.setBounds(115,54,94,28);
btnStop.setText(“停止”);
this.shell.open();
this.shell.layout();
this.shell.addListener(SWT.Close,new Listener()){
公共无效handleEvent(事件)
{
shell.dispose();
}
});
而(!this.shell.isDisposed())
{
如果(!display.readAndDispatch())
{
//text.setText(“+counter.get());
display.sleep();
}
}
}
公共静态void main(字符串[]args)
{
新单元测试();
}
}
类TestThread扩展线程
{   
公开募捐
{
尝试
{
int i=0;
而(i++<1000&&!this.isInterrupted())
{
UnitTest.counter.getAndIncrement();
试一试{
TestThread.sleep(2000年);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
打破
}
if(UnitTest.display.isDisposed())
返回;
UnitTest.display.asyncExec(新的Runnable(){
公开募捐{
if(UnitTest.text.isDisposed())
返回;
UnitTest.text.setText(“+UnitTest.counter.get());
}
});
}
}
捕获(例外e)
{
e、 printStackTrace();
}
最后
{
System.out.println(“现有线程…”);
}
}
}

您应该小心地使用来自不同线程的UI更新。请阅读以下内容:


谢谢您的链接。在(!this.shell.isDisposed())期间更新文本将使CPU增加到99%。在TestThread中更新文本不会像在while中那样增加CPU。但问题是,每当我想更新显示内容的任何组件时,是否必须为每个类都放置display.async?这也很有帮助。