java - Eclipse“小部件已处理”错误
问题描述
好的,所以我在这里使用 Eclipse Oxygen(我认为这很重要,因为它看起来像是 Eclipse 生成的错误)。我已经看过其他几篇关于这个特定错误的帖子,但没有一个对我有用/不是我已经尝试过的东西。所以我的目标是有一个while循环,让它每秒获取db声级并更新一个标签。
不幸的是,这并没有像连续 5 次那样奏效。首先,窗口没有打开,我意识到这是因为while循环阻止了窗口打开,所以我把它放到了一个线程中。然后就像 5 次尝试稍后修复它一样,我的代码中仍然出现“小部件已处置”错误。这是打开窗口的代码(这是我创建项目以来唯一更改的代码):
public void open() throws InterruptedException, LineUnavailableException {
Display display = Display.getDefault();
shlAudioAdjuster = new Shell();
shlAudioAdjuster.setSize(450, 300);
shlAudioAdjuster.setText("Audio Adjuster");
Label lblCurrentDecibelLevel = new Label(shlAudioAdjuster, SWT.NONE);
lblCurrentDecibelLevel.setFont(SWTResourceManager.getFont("Muli", 14, SWT.NORMAL));
ProgressBar progressBar = new ProgressBar(shlAudioAdjuster, SWT.NONE);
progressBar.setSelection(30);
progressBar.setBounds(143, 168, 170, 17);
shlAudioAdjuster.open();
shlAudioAdjuster.layout();
while (!shlAudioAdjuster.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
AudioFormat audioFormat = getAudioFormat();
TargetDataLine targetDataLine;
try {
targetDataLine = (TargetDataLine) AudioSystem.getTargetDataLine(audioFormat);
targetDataLine.open();
targetDataLine.start();
byte [] buffer = new byte[2000];
while (true) {
int bytesRead = targetDataLine.read(buffer,0,buffer.length);
int max;
if (bytesRead >=0) {
max = (short) (buffer[0] + (buffer[1] << 8));
for (int p=2;p<bytesRead-1;p+=2) {
short thisValue = (short) (buffer[p] + (buffer[p+1] << 8));
if (thisValue>max) max=thisValue;
}
progressBar.setSelection((int)(20 * Math.log10(max)));
}
}
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
并且该错误确认progressBar.setSelection((int)(20 * Math.log10(max)));
. 这是完整的错误:
org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(SWT.java:4533)
at org.eclipse.swt.SWT.error(SWT.java:4448)
at org.eclipse.swt.SWT.error(SWT.java:4419)
at org.eclipse.swt.widgets.Widget.error(Widget.java:482)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:354)
at org.eclipse.swt.widgets.ProgressBar.setSelection(ProgressBar.java:322)
at MainWindow.open(MainWindow.java:90)
at MainWindow.main(MainWindow.java:34)
PS 最后一行错误说 34 是该函数的调用者,第一行是进度条更改行。有任何想法吗?
解决方案
Shell 窗口仅在while (!shlAudioAdjuster.isDisposed())
循环运行时显示。一旦该循环退出,外壳中的所有控件都将被释放并且不能再使用。所以你不能把你的代码放在那个while循环之后。
您必须将播放声音的代码放在打开外壳时启动的单独线程中。但是您不能直接在后台线程中更新进度条,而是需要使用Display.asyncExec
在 UI 线程中运行进度条更新:
Display.getDefault().asyncExec(() -> progressBar.setSelection((int)(20 * Math.log10(max))));
您可能还想检查进度条是否已被释放并停止线程:
if (progressBar.isDisposed()) {
// TODO exit the thread
}
推荐阅读
- python - 如何运行没有定义参数的函数?
- c# - 如何从 Razor 页面访问视图组件的属性
- html - 如何使用 css 在 html 中设置 flex 内联?
- pandas - Pandas groupby,然后在另一列中找到每组的最大值
- octave - 在 Octave 中最小化具有有限变量的线性方程组
- r - R:为什么,如何避免:read.table 通过删除最后一个字符(冒号)将字符(字符串)转换为数字
- node.js - 在 Twilio IVR 中使用 Google Cloud Speech
- java - 使用spring jdbc查询多个表
- python - 为什么 pip 安装的依赖项不在 pipfile 中?
- python - aiohttp 服务器立即停止