0

私は MAC OS 用の SWT Java プロジェクトに取り組んでいます。SWT UI にラベルを追加して、現在の時刻を表示し、毎秒更新する必要があります。私はそれを試しましたすなわち

final Label lblNewLabel_1 = new Label(composite, SWT.CENTER);
FormData fd_lblNewLabel_1 = new FormData();
fd_lblNewLabel_1.left = new FormAttachment(btnNewButton_call, 10);
fd_lblNewLabel_1.bottom = new FormAttachment(100, -10);
fd_lblNewLabel_1.right = new FormAttachment(btnTransfer, -10);
fd_lblNewLabel_1.height = 20;
lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
    getDisplay().syncExec(new Runnable() {

            @Override
            public void run() {
                while(true){
                    lblNewLabel_1.setText(Calendar.getInstance().getTime().toString());

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }   
            }
        });

しかし、うまくいきません。それを手伝ってください。前もって感謝します。

4

3 に答える 3

3

別のスレッドから UI を更新しているわけではありません。UI スレッド自体を更新しています。

sleepUI スレッドで を実行すると、UI スレッドがペイントなどを実行できなくなるため、プログラムがハングしたように見えます。

ウィジェットを更新して 1 秒間スリープする を実行するように UI スレッドをスケジュールする代わりに、Runnable1 秒ごとにスリープRunnableし、ウィジェットを更新してすぐに終了するをスケジュールするスレッドが必要です。

例えば:

while(true)
{
    getDisplay().asyncExec(new Runnable() {
        lblNewLabel_1.setText(Calendar.getInstance().getTime().toString());
    });

    Thread.sleep(1000);
}
于 2013-04-16T15:03:49.407 に答える