Java SWT 更改所选小部件上的标签文本在 Mac 上不起作用
Java SWT Changing Label text on widget selected is not working on Mac
在我的 java swt 应用程序中,我有以下代码。在按钮 select 上,我需要两次更改标签文本,一次是在 运行 线程之前,另一次是在线程完成之后。它适用于 windows 但在 Mac 上它不显示第一个文本。为什么这不适用于 Mac?
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
statusLabel.setText("Running...");
Thread background = new Thread() {
@Override
public void run() {
// Long running task
}
};
background.start();
try {
background.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
statusLabel.setText("Finished");
}
});
对 Thread.join
的调用正在阻塞 UI 线程,这将导致它停止响应。在此之前究竟更新了多少取决于每个平台上 SWT 实现的细节。
您应该在代码完成后从后台线程更新 UI。
类似于:
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
statusLabel.setText("Running...");
Thread background = new Thread() {
@Override
public void run() {
// Long running task
// Update UI from background thread
Display.getDefault().asyncExec(() -> statusLabel.setText("Finished"));
}
};
background.start();
}
});
在我的 java swt 应用程序中,我有以下代码。在按钮 select 上,我需要两次更改标签文本,一次是在 运行 线程之前,另一次是在线程完成之后。它适用于 windows 但在 Mac 上它不显示第一个文本。为什么这不适用于 Mac?
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
statusLabel.setText("Running...");
Thread background = new Thread() {
@Override
public void run() {
// Long running task
}
};
background.start();
try {
background.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
statusLabel.setText("Finished");
}
});
对 Thread.join
的调用正在阻塞 UI 线程,这将导致它停止响应。在此之前究竟更新了多少取决于每个平台上 SWT 实现的细节。
您应该在代码完成后从后台线程更新 UI。
类似于:
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
statusLabel.setText("Running...");
Thread background = new Thread() {
@Override
public void run() {
// Long running task
// Update UI from background thread
Display.getDefault().asyncExec(() -> statusLabel.setText("Finished"));
}
};
background.start();
}
});