如何在线程Java中的Label中设置文本

时间:2017-01-13 10:26:55

标签: java multithreading label swt

您好我正在开发一个SWT Java应用程序。 我创建了两个类:第一个包含SWT包,我将在其中运行应用程序,第二个包含为线程扩展。我正在尝试在应用程序运行时修改线程中的标签。这是删除标签的代码:

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;


public class LogoQuiz {

    protected Shell shell = new Shell();
    public static Label lblNewLabel;
    Thread t1 = new Time();

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

    /**
     * Open the window.
     */

    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {

        shell.setSize(450, 300);
        shell.setText("SWT Application");

        Button btnNewButton = new Button(shell, SWT.NONE);
        btnNewButton.setBounds(91, 100, 75, 25);
        btnNewButton.setText("New Button");

        lblNewLabel = new Label(shell, SWT.NONE);
        lblNewLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseDoubleClick(MouseEvent e) {
                t1.start();
            }
        });
        lblNewLabel.setBounds(235, 169, 127, 53);
        lblNewLabel.setText("New Label");
    }
}

还有代码与线程:

 public class time extends Threads {
     public void run () {
         LogoQuiz.lblNewLabel.setText("Done");
     }
}

因此,当我运行应用程序并单击标签两次时,它会显示:“SWTException”。我正在用Eclipse开发。有人可以帮我解决这个例外吗?非常感谢!对不起,我的英语不好!

0 个答案:

没有答案