为什么在使用runnable时,我的Java SWT Gui应用程序不会更改Label?

时间:2016-04-06 06:06:58

标签: java user-interface swt runnable

所以我现在遇到了一个问题,因为一切正常,只是在尝试一些代码来创建一个简单的系统时间和日期设置。我有一个问题,我似乎无法修复。所以我已经确定了一个标签(不是JLabel),当我尝试通过runnable void运行它时,当我尝试设置文本以更新系统的确切时间时,似乎出错。例如Time.setText(time);时间将被识别为字符串,但它只是错误而且不起作用。

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;

public class HomeScreen {

protected Shell shell;
private Label Date;
private Label Time;
private Composite composite;


/**
 * Launch the application.
 * @param args
 */
public static void main(String[] args) {
            try {
                HomeScreen window = new HomeScreen();
                window.open();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

/**
 * Open the window.
 */
public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    Date();
    Time();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

/**
 * Create contents of the window.
 */
protected void createContents() {
    shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("Project Serenity");

    composite = new Composite(shell, SWT.NONE);
    composite.setBounds(0, 0, 470, 278);

    Time = new Label(composite, SWT.NONE);
    Time.setLocation(10, 192);
    Time.setSize(169, 31);
    Time.setText("Time");

    Date = new Label(composite, SWT.NONE);
    Date.setLocation(10, 217);
    Date.setSize(169, 38);
    Date.setText("Date");

}
public void Date() 
{
    Thread Date2 = new Thread()
    {
        @Override
        public void run()
        {   
            try {
                for(;;){
                    Calendar cal = new GregorianCalendar();
                    int day = cal.get(Calendar.DAY_OF_MONTH);
                    int month = cal.get(Calendar.MONTH);
                    int year = cal.get(Calendar.YEAR);
                    Time.setText(day + ", " + month + " " + year);          
                sleep(1000);
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    Date2.start();
}
public void Time() 
{
    Thread Time2 = new Thread()
    {
        @Override
        public void run()
        {
            try {
                for(;;){
                Calendar cal = new GregorianCalendar();
                int second = cal.get(Calendar.SECOND);
                int minute = cal.get(Calendar.MINUTE);
                int hour = cal.get(Calendar.HOUR);
                int am_pm = cal.get(Calendar.AM_PM);
                sleep(1000);
                Time.setText(hour + ":" + minute + ":" + second + " " + am_pm);
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    Time2.start();
}

}

Thx Pixl

1 个答案:

答案 0 :(得分:1)

必须在用户界面线程中完成对用户界面对象的所有访问。尝试在任何其他线程中访问它们将引发异常。

您可以使用asyncExec的{​​{1}}方法在UI线程中执行Display。所以不要只做:

Runnable

在后台线程中执行:

Time.setText(day + ", " + month + " " + year);          

以上是针对Java 8的,如果您使用的是旧版本的Java:

Display.getDefault().asyncExec(() -> Time.setText(day + ", " + month + " " + year));

注意:请学习使用Java命名约定 - 变量以小写字母开头,因此Display.getDefault().asyncExec(new Runnable() { @Override public void run { Time.setText(day + ", " + month + " " + year); } }); 应为Time

退出应用程序时,您也没有做任何事情来停止更新线程。主UI线程停止后访问控件会出错。您可以测试是否已释放time控件,该控件将告知您应用程序已停止。

Time