[英]How to create dynamic text in label in SWT java?
I am trying to write a code including a button and a label.我正在尝试编写包含按钮和标签的代码。
I want, when user click the button, the label shows TEXTA, then after three seconds it shows TEXTB.我想要,当用户单击按钮时,标签显示 TEXTA,然后在三秒钟后显示 TEXTB。 What I see is when I click the button, the label wait for 3 seconds and shows TEXTB.
我看到的是当我单击按钮时,标签等待 3 秒并显示 TEXTB。 Here is my code:
这是我的代码:
Label lblFindModem = new Label(shell, SWT.NONE);
lblFindModem.setFont(SWTResourceManager.getFont("Ubuntu", 13, SWT.NORMAL));
lblFindModem.setBounds(329, 164, 256, 28);
lblFindModem.setText("Modem is not Initialized");
Button btnFindModem = new Button(shell, SWT.NONE);
btnFindModem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
System.out.println("Someone Clicked the button");
lblFindModem.setText("Unplug the modem for 3 seconds...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lblFindModem.setText("Plug the modem again.");
}
});
You must never block the main SWT UI thread by calling things like Thread.sleep
.您绝不能通过调用诸如
Thread.sleep
东西来阻塞主 SWT UI 线程。 This will make the app completely unresponsive and nothing will happen until the end of the sleep.这将使应用程序完全无响应,并且在睡眠结束之前不会发生任何事情。 It is vital than the UI code returns to the main
Display.readAndDispatch
loop quickly.重要的是 UI 代码快速返回到主
Display.readAndDispatch
循环。
Instead you can use Display.timerExec
to execute some code after a delay:相反,您可以使用
Display.timerExec
在延迟后执行一些代码:
So replace your code所以替换你的代码
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lblFindModem.setText("Plug the modem again.");
with:和:
Display.getCurrent().timerExec(3000, () -> lblFindModem.setText("Plug the modem again."));
(code assumes you are using Java 8 or later) (代码假定您使用的是 Java 8 或更高版本)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.